Skip to content

Commit 9bad403

Browse files
authored
Merge pull request #545 from fengyunfengyun1/cypress-test-to-cover-wallet-creation
feature/cypress-test-to-cover-wallet-creation
2 parents 8610697 + 7f5b369 commit 9bad403

File tree

5 files changed

+87
-2
lines changed

5 files changed

+87
-2
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// apps/web/cypress/e2e/create-wallet.cy.ts
2+
import { SELECTORS } from "../support/wallet-constants";
3+
4+
let walletData: {
5+
routes: { wallets: string };
6+
mocks: { initialList: any[]; createdWallet: { id: string; name: string } };
7+
};
8+
9+
describe("Wallet - Create flow (integration, all requests mocked)", () => {
10+
before(() => {
11+
cy.fixture("wallet.json").then(data => {
12+
walletData = data;
13+
});
14+
});
15+
16+
beforeEach(() => {
17+
cy.intercept("GET", "/api/wallets*", {
18+
statusCode: 200,
19+
body: walletData?.mocks?.initialList ?? [],
20+
}).as("getWallets");
21+
22+
cy.visit(walletData.routes.wallets, {
23+
onBeforeLoad(win) {
24+
win.sessionStorage.setItem(
25+
"token",
26+
JSON.stringify("fake-token-for-tests"),
27+
);
28+
},
29+
});
30+
});
31+
32+
it("happy path: jump -> input wallet name -> create", () => {
33+
const name = walletData.mocks.createdWallet.name;
34+
35+
cy.getByData(SELECTORS.walletCreateOpen).click();
36+
37+
cy.getByData(SELECTORS.walletNameInput).clear().type(name);
38+
39+
cy.intercept("POST", "/api/wallets", req => {
40+
expect(req.body).to.have.property("name", name);
41+
req.reply({ statusCode: 201, body: walletData.mocks.createdWallet });
42+
}).as("createWallet");
43+
44+
cy.getByData(SELECTORS.walletCreateSubmitButton).click();
45+
46+
cy.contains(name).should("exist");
47+
});
48+
49+
it("client-side duplicate check: shows helper text and disables submit", () => {
50+
const dupName = walletData.mocks.createdWallet.name;
51+
52+
cy.getByData(SELECTORS.walletCreateOpen).click();
53+
cy.getByData(SELECTORS.walletNameInput).type(dupName);
54+
cy.getByData(SELECTORS.walletCreateSubmitButton).click();
55+
cy.contains(dupName).should("exist");
56+
57+
cy.getByData(SELECTORS.walletCreateOpen).click();
58+
cy.getByData(SELECTORS.walletNameInput).type(dupName);
59+
cy.getByData(SELECTORS.errorHelperText).should("contain.text", "unique");
60+
cy.getByData(SELECTORS.walletCreateSubmitButton).should("be.disabled");
61+
});
62+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"routes": { "wallets": "/wallet" },
3+
"mocks": {
4+
"initialList": [],
5+
"createdWallet": { "id": "wallet_123", "name": "My Test Wallet" }
6+
},
7+
"messages": { "duplicate": "Wallet name should be unique." }
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const SELECTORS = {
2+
walletCreateOpen: "wallet-create-open",
3+
walletNameInput: "wallet-name-input",
4+
walletCreateSubmitButton: "wallet-create-submit",
5+
walletList: "wallet-list",
6+
errorHelperText: "error-helper-text",
7+
} as const;
8+
9+
export function loadWalletFixture() {
10+
return cy.fixture("wallet.json").as("walletData");
11+
}

apps/web/src/app/(protected)/wallet/page.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default function WalletPage() {
4242
<Box sx={{ p: 2 }}>
4343
<Box display="flex" justifyContent="space-between" alignItems="center">
4444
<Button
45+
data-test="wallet-create-open"
4546
variant="text"
4647
startIcon={<AddIcon />}
4748
onClick={() => setIsCreateOpen(true)}
@@ -62,9 +63,11 @@ export default function WalletPage() {
6263
Your Wallets
6364
</Typography>
6465

65-
<Stack spacing={0.5}>
66+
<Stack spacing={0.5} data-test="wallet-list">
6667
{wallets.map((wallet, idx) => (
67-
<WalletItem key={idx} {...wallet} />
68+
<div key={idx} data-test={`wallet-list-item-${idx}`}>
69+
<WalletItem {...wallet} />
70+
</div>
6871
))}
6972
</Stack>
7073

apps/web/src/components/WalletCreateDrawer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ const WalletCreateDrawer: React.FC<WalletCreateDrawerProps> = ({
107107
variant="contained"
108108
disabled={!name.trim() || isDuplicate}
109109
onClick={handleCreate}
110+
data-test="wallet-create-submit"
110111
sx={{
111112
mt: 1.5,
112113
":disabled": { backgroundColor: "#E0E0E0", color: "#9E9E9E" },

0 commit comments

Comments
 (0)