forked from MergeFi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWalletContext.test.tsx
More file actions
104 lines (83 loc) · 2.67 KB
/
Copy pathWalletContext.test.tsx
File metadata and controls
104 lines (83 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { render, screen, waitFor, act } from "@testing-library/react";
import { WalletProvider, useWallet } from "./WalletContext";
const WALLET_KEY = "mergefi_wallet_address";
jest.mock("@/context/AuthContext", () => ({
useAuth: () => ({ user: null, refresh: jest.fn() }),
}));
jest.mock("@/lib/wallet", () => ({
connectWallet: jest.fn(),
}));
// WalletContext imports apiRequest for profile linking on connect(), which
// pulls in src/lib/config.ts's env validation at module-load time — not
// exercised by these cross-tab tests, so it's mocked out like @/lib/wallet.
jest.mock("@/lib/api", () => ({
apiRequest: jest.fn(),
}));
function TestConsumer() {
const { address } = useWallet();
return <div data-testid="address">{address ?? "disconnected"}</div>;
}
function dispatchWalletStorageEvent(newValue: string | null) {
window.dispatchEvent(
new StorageEvent("storage", {
key: WALLET_KEY,
newValue,
storageArea: window.localStorage,
}),
);
}
beforeEach(() => {
window.localStorage.clear();
});
describe("WalletContext — cross-tab sync (issue #84)", () => {
it("adopts an address connected in another tab", async () => {
render(
<WalletProvider>
<TestConsumer />
</WalletProvider>,
);
await waitFor(() =>
expect(screen.getByTestId("address")).toHaveTextContent("disconnected"),
);
act(() => {
dispatchWalletStorageEvent("GABC123FROMANOTHERTAB");
});
expect(screen.getByTestId("address")).toHaveTextContent("GABC123FROMANOTHERTAB");
});
it("clears the address when disconnected in another tab", async () => {
window.localStorage.setItem(WALLET_KEY, "GABC123ALREADYCONNECTED");
render(
<WalletProvider>
<TestConsumer />
</WalletProvider>,
);
await waitFor(() =>
expect(screen.getByTestId("address")).toHaveTextContent("GABC123ALREADYCONNECTED"),
);
act(() => {
dispatchWalletStorageEvent(null);
});
expect(screen.getByTestId("address")).toHaveTextContent("disconnected");
});
it("ignores storage events for unrelated keys", async () => {
window.localStorage.setItem(WALLET_KEY, "GABC123ALREADYCONNECTED");
render(
<WalletProvider>
<TestConsumer />
</WalletProvider>,
);
await waitFor(() =>
expect(screen.getByTestId("address")).toHaveTextContent("GABC123ALREADYCONNECTED"),
);
act(() => {
window.dispatchEvent(
new StorageEvent("storage", {
key: "mergefi_token",
newValue: null,
storageArea: window.localStorage,
}),
);
});
expect(screen.getByTestId("address")).toHaveTextContent("GABC123ALREADYCONNECTED");
});
});