Skip to content

Commit

Permalink
add more cypress tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mariarykova committed Dec 21, 2023
1 parent 9b449ab commit 7a31ae5
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 4 deletions.
22 changes: 22 additions & 0 deletions web/leaderboard/cypress/e2e/page.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,26 @@ describe("Home Page Tests", () => {
cy.get('[class*="header"]').should("exist");
cy.contains("h1", "Fluidity Leaderboard").should("exist");
});

it("change filters on click all time", () => {
cy.get("[data-cy=all-btn]").click();
cy.get("[data-cy=title]").contains("ALL TIME");
});

it("open Connect Wallet Modal", () => {
cy.get('[class*="connected_wallet"]').should("exist");
cy.get('[class*="connected_wallet"]').click();
cy.wait(10000);
cy.get('[class*="connect-wallet-outer-container"]').should("exist");
});

it("Confirms the number of adresses in the table", () => {
cy.visit("http://localhost:3001");
cy.request("GET", "api/users").then((response) => {
expect(response.status).to.eq(200);
expect(response).to.have.property("headers");
expect(response).to.have.property("duration");
expect(response.body).to.have.length(3);
});
});
});
12 changes: 12 additions & 0 deletions web/leaderboard/cypress/e2e/profile.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
describe("Connect Wallet Modal Open Test", () => {
beforeEach(() => {
cy.visit("http://localhost:3001");
});

it("open Connect Wallet Modal", () => {
cy.get('[class*="connected_wallet"]').should("exist");
cy.get('[class*="connected_wallet"]').click();
cy.wait(10000);
cy.get('[class*="connect-wallet-outer-container"]').should("exist");
});
});
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { ConnectedWalletModal } from "./ConnectedWalletModal";
import React from "react";

const addressSize = 12 / 2;
const trimAddress = (address: string) => {
const leftSide = address.substring(0, addressSize);

const rightSide = address.substring(
address.length - addressSize,
address.length
);

return `${leftSide}..${rightSide}`;
};

describe("ConnectedWalletModal Component Tests", () => {
it("Renders the modal when visible is true", () => {
const props = {
visible: true,
address: "0x0x3a568E31540E2179b4833b21C6a45710b569D595",
close: cy.stub().as("close"),
disconnect: cy.stub().as("disconnect"),
};

cy.mount(<ConnectedWalletModal {...props} />);
cy.get('[class*="show-modal"]').should("exist");
});

it("Does not render the modal when visible is false", () => {
const props = {
visible: false,
address: "0x0x3a568E31540E2179b4833b21C6a45710b569D595",
close: cy.stub().as("close"),
disconnect: cy.stub().as("disconnect"),
};

cy.mount(<ConnectedWalletModal {...props} />);

cy.get('[class*="show-modal"]').should("not.exist");
});

it("Displays the address correctly", () => {
const props = {
visible: true,
address: "0x0x3a568E31540E2179b4833b21C6a45710b569D595",
close: cy.stub().as("close"),
disconnect: cy.stub().as("disconnect"),
};
cy.mount(<ConnectedWalletModal {...props} />);
cy.contains(trimAddress(props.address)).should("be.visible");
});

it("Calls the disconnect function on disconnect button click", () => {
const props = {
visible: true,
address: "0x0x3a568E31540E2179b4833b21C6a45710b569D595",
close: cy.stub().as("close"),
disconnect: cy.stub().as("disconnect"),
};

cy.mount(<ConnectedWalletModal {...props} />);
cy.get('[class*="disconnect-wallet-btn-in-modal"]').click();
cy.get("@disconnect").should("have.been.calledOnce");
});

it("Calls the close function on close (cross) button click", () => {
const props = {
visible: true,
address: "0x0x3a568E31540E2179b4833b21C6a45710b569D595",
close: cy.stub().as("close"),
disconnect: cy.stub().as("disconnect"),
};

cy.mount(<ConnectedWalletModal {...props} />);
cy.get('[class*="modal-cancel-btn"]').click();
cy.get("@close").should("have.been.calledOnce");
});
});
44 changes: 44 additions & 0 deletions web/leaderboard/src/app/components/Dropdown/dropdown.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from "react";
import { DropdownOptions } from "./index";
import "@fluidity-money/surfing";
import "./Dropdown.module.scss";

describe("DropdownOptions Component", () => {
let setSortedByItem: any;
let setOpenDropdown: any;
let sortData: any;

beforeEach(() => {
setSortedByItem = cy.stub();
setOpenDropdown = cy.stub();
sortData = cy.stub();

cy.mount(
<DropdownOptions
setSortedByItem={setSortedByItem}
setOpenDropdown={setOpenDropdown}
sortData={sortData}
/>
);
});

it("renders correctly", () => {
cy.get("[class*='dropdown_options']").should("exist");
cy.get("[class*='dropdown_options'] ul li").should("have.length", 3);
});

it("handles option click correctly", () => {
const optionTitles = ["volume", "rewards", "number"];

optionTitles.forEach((title, index) => {
cy.get("[class*='dropdown_options'] ul li")
.eq(index)
.click()
.then(() => {
expect(setSortedByItem).to.be.calledWith(optionTitles[index]);
expect(setOpenDropdown).to.be.calledWith(false);
expect(sortData).to.be.calledWith(optionTitles[index].toLowerCase());
});
});
});
});
8 changes: 8 additions & 0 deletions web/leaderboard/src/app/components/Footer/footer.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from "react";
import Footer from "./index";

describe("<Footer />", () => {
it("mounts <Footer />", () => {
cy.mount(<Footer />);
});
});
5 changes: 3 additions & 2 deletions web/leaderboard/src/app/components/Profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const Profile = () => {
/>
) : (
<GeneralButton
data-cy="connect-wallet-btn"
type={isConnected || isConnecting ? "transparent" : "primary"}
size={"medium"}
handleClick={() =>
Expand All @@ -43,15 +44,15 @@ export const Profile = () => {
</GeneralButton>
)}

<Modal id="connect-wallet" visible={walletModalVisibility}>
<Modal visible={walletModalVisibility}>
<div className={styles.cover}>
<ConnectWalletModal
visible={walletModalVisibility}
close={() => setWalletModalVisibility(false)}
/>
</div>
</Modal>
<Modal id="connect-wallet" visible={connectedWalletModalVisibility}>
<Modal id="connected-wallet" visible={connectedWalletModalVisibility}>
<div className={styles.cover}>
<ConnectedWalletModal
visible={connectedWalletModalVisibility}
Expand Down
8 changes: 8 additions & 0 deletions web/leaderboard/src/app/components/Profile/profile.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from "react";
import { Profile } from "./index";

describe("<Profile />", () => {
it("mounts <Profile />", () => {
cy.mount(<Profile />);
});
});
4 changes: 2 additions & 2 deletions web/leaderboard/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export default function Home() {
Leaderboard
</Heading>
</div>
<Text>
<Text data-cy="title">
This leaderboard shows your rank among other users
{filterIndex === 0 ? " per" : " for"}
&nbsp;
Expand Down Expand Up @@ -355,7 +355,7 @@ export default function Home() {
: `${styles.btn} ${styles.btn_highlited}`
}
>
<Text code size="sm">
<Text code size="sm" data-cy="all-btn">
ALL TIME
</Text>
</GeneralButton>
Expand Down
40 changes: 40 additions & 0 deletions web/leaderboard/src/pages/api/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { NextApiRequest, NextApiResponse } from "next";

type User = {
address: string;
number_of_transactions: number;
rank: number;
volume: number;
yield_earned: number;
};

export default function users(
req: NextApiRequest,
res: NextApiResponse<User[]>
) {
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
return res.json([
{
address: "0x21f2275f26611fba1d486153b5d2d78164568435",
number_of_transactions: 21898,
rank: 2388,
volume: 255.13345799999914,
yield_earned: 18.985949367059586,
},
{
address: "0x1cb94adfd3314d48ca8145b2c6983419257c0486",
number_of_transactions: 12520,
rank: 3682,
volume: 894699.873285003,
yield_earned: 482.0982366909158,
},
{
address: "0xe776ffdab7b40147fc0b8e93676eb444fb3650b6",
number_of_transactions: 7135,
rank: 4260,
volume: 240194.4372060101,
yield_earned: 920.81961067,
},
]);
}

0 comments on commit 7a31ae5

Please sign in to comment.