forked from MergeFi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthContext.test.tsx
More file actions
131 lines (110 loc) · 3.67 KB
/
Copy pathAuthContext.test.tsx
File metadata and controls
131 lines (110 loc) · 3.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { render, screen, waitFor, act } from "@testing-library/react";
import { AuthProvider, useAuth } from "./AuthContext";
import { TOKEN_KEY } from "@/lib/auth";
import { apiRequest } from "@/lib/api";
import type { AuthUser } from "@/types";
jest.mock("@/lib/api", () => ({
apiRequest: jest.fn(),
}));
const mockApiRequest = apiRequest as jest.MockedFunction<typeof apiRequest>;
const PROFILE: AuthUser = {
id: "user-1",
username: "alice",
displayName: "Alice",
avatarUrl: null,
roles: [],
stellarAddress: null,
};
function TestConsumer() {
const { user, loading } = useAuth();
if (loading) return <div data-testid="state">loading</div>;
return <div data-testid="state">{user ? `signed-in:${user.username}` : "signed-out"}</div>;
}
function dispatchTokenStorageEvent(newValue: string | null) {
window.dispatchEvent(
new StorageEvent("storage", {
key: TOKEN_KEY,
newValue,
storageArea: window.localStorage,
}),
);
}
beforeEach(() => {
window.localStorage.clear();
mockApiRequest.mockReset();
});
describe("AuthContext — cross-tab sync (issue #84)", () => {
it("signs the user out when another tab clears the token", async () => {
window.localStorage.setItem(TOKEN_KEY, "token-a");
mockApiRequest.mockImplementation(async (path: string) => {
if (path === "/auth/me") return { userId: "user-1", username: "alice" };
return PROFILE;
});
render(
<AuthProvider>
<TestConsumer />
</AuthProvider>,
);
await waitFor(() =>
expect(screen.getByTestId("state")).toHaveTextContent("signed-in:alice"),
);
act(() => {
dispatchTokenStorageEvent(null);
});
await waitFor(() =>
expect(screen.getByTestId("state")).toHaveTextContent("signed-out"),
);
});
it("re-resolves the session when another tab sets a new token", async () => {
mockApiRequest.mockImplementation(async (path: string) => {
if (path === "/auth/me") return { userId: "user-1", username: "alice" };
return PROFILE;
});
render(
<AuthProvider>
<TestConsumer />
</AuthProvider>,
);
await waitFor(() =>
expect(screen.getByTestId("state")).toHaveTextContent("signed-out"),
);
act(() => {
// A real browser's storage event fires only in other tabs, after that
// tab's own localStorage.setItem() has already applied to the shared,
// same-origin backing store — so the write is reflected here too.
window.localStorage.setItem(TOKEN_KEY, "token-from-other-tab");
dispatchTokenStorageEvent("token-from-other-tab");
});
await waitFor(() =>
expect(screen.getByTestId("state")).toHaveTextContent("signed-in:alice"),
);
expect(mockApiRequest).toHaveBeenCalledWith("/auth/me");
});
it("ignores storage events for unrelated keys", async () => {
window.localStorage.setItem(TOKEN_KEY, "token-a");
mockApiRequest.mockImplementation(async (path: string) => {
if (path === "/auth/me") return { userId: "user-1", username: "alice" };
return PROFILE;
});
render(
<AuthProvider>
<TestConsumer />
</AuthProvider>,
);
await waitFor(() =>
expect(screen.getByTestId("state")).toHaveTextContent("signed-in:alice"),
);
const callCountBefore = mockApiRequest.mock.calls.length;
act(() => {
window.dispatchEvent(
new StorageEvent("storage", {
key: "mergefi_wallet_address",
newValue: "GABC...",
storageArea: window.localStorage,
}),
);
});
expect(mockApiRequest.mock.calls.length).toBe(callCountBefore);
expect(screen.getByTestId("state")).toHaveTextContent("signed-in:alice");
});
});