-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathUserProvider.test.tsx
More file actions
246 lines (197 loc) · 6.72 KB
/
UserProvider.test.tsx
File metadata and controls
246 lines (197 loc) · 6.72 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { usePostHog } from "posthog-js/react";
import Session from "supertokens-auth-react/recipe/session";
import "@testing-library/jest-dom";
import { render, waitFor } from "@testing-library/react";
import { UserProvider } from "./UserProvider";
// Mock PostHog
jest.mock("posthog-js/react");
const mockUsePostHog = usePostHog as jest.MockedFunction<typeof usePostHog>;
// Mock SuperTokens Session
jest.mock("supertokens-auth-react/recipe/session");
const mockSession = Session as jest.Mocked<typeof Session>;
// Mock AbsoluteOverflowLoader
jest.mock("@web/components/AbsoluteOverflowLoader", () => ({
AbsoluteOverflowLoader: () => <div>Loading...</div>,
}));
describe("UserProvider", () => {
const mockIdentify = jest.fn();
const mockGetAccessTokenPayloadSecurely = jest.fn();
beforeEach(() => {
jest.clearAllMocks();
// Default mock implementation
mockSession.getAccessTokenPayloadSecurely =
mockGetAccessTokenPayloadSecurely;
});
describe("PostHog Integration", () => {
it("should call posthog.identify when PostHog is enabled and user data is available", async () => {
const testUserId = "test-user-123";
const testEmail = "test@example.com";
// Mock session with userId and email
mockGetAccessTokenPayloadSecurely.mockResolvedValue({
sub: testUserId,
email: testEmail,
});
// Mock PostHog as enabled
mockUsePostHog.mockReturnValue({
identify: mockIdentify,
} as any);
render(
<UserProvider>
<div>Test Child</div>
</UserProvider>,
);
// Wait for async data fetch and PostHog identify to be called
await waitFor(() => {
expect(mockIdentify).toHaveBeenCalledWith(testEmail, {
email: testEmail,
userId: testUserId,
});
});
// Verify it was called exactly once
expect(mockIdentify).toHaveBeenCalledTimes(1);
});
it("should NOT call posthog.identify when PostHog is disabled", async () => {
const testUserId = "test-user-123";
const testEmail = "test@example.com";
// Mock session with userId and email
mockGetAccessTokenPayloadSecurely.mockResolvedValue({
sub: testUserId,
email: testEmail,
});
// Mock PostHog as disabled (returns undefined/null)
mockUsePostHog.mockReturnValue(undefined as any);
render(
<UserProvider>
<div>Test Child</div>
</UserProvider>,
);
// Wait a bit to ensure no identify call happens
await waitFor(() => {
expect(mockGetAccessTokenPayloadSecurely).toHaveBeenCalled();
});
// Verify identify was never called
expect(mockIdentify).not.toHaveBeenCalled();
});
it("should NOT call posthog.identify when email is missing from session", async () => {
const testUserId = "test-user-123";
// Mock session with userId but NO email
mockGetAccessTokenPayloadSecurely.mockResolvedValue({
sub: testUserId,
// email is missing
});
// Mock PostHog as enabled
mockUsePostHog.mockReturnValue({
identify: mockIdentify,
} as any);
render(
<UserProvider>
<div>Test Child</div>
</UserProvider>,
);
// Wait for data fetch
await waitFor(() => {
expect(mockGetAccessTokenPayloadSecurely).toHaveBeenCalled();
});
// Verify identify was not called because email is missing
expect(mockIdentify).not.toHaveBeenCalled();
});
it("should NOT call posthog.identify when userId is missing", async () => {
const testEmail = "test@example.com";
// Mock session with email but NO userId (sub)
mockGetAccessTokenPayloadSecurely.mockResolvedValue({
email: testEmail,
// sub is missing
});
// Mock PostHog as enabled
mockUsePostHog.mockReturnValue({
identify: mockIdentify,
} as any);
render(
<UserProvider>
<div>Test Child</div>
</UserProvider>,
);
// The component should show loading state because userId is null
// and identify should never be called
await waitFor(() => {
expect(mockGetAccessTokenPayloadSecurely).toHaveBeenCalled();
});
expect(mockIdentify).not.toHaveBeenCalled();
});
it("should handle posthog.identify not being a function gracefully", async () => {
const testUserId = "test-user-123";
const testEmail = "test@example.com";
// Mock session with userId and email
mockGetAccessTokenPayloadSecurely.mockResolvedValue({
sub: testUserId,
email: testEmail,
});
// Mock PostHog with identify not being a function
mockUsePostHog.mockReturnValue({
identify: null,
} as any);
// Should not throw an error
expect(() => {
render(
<UserProvider>
<div>Test Child</div>
</UserProvider>,
);
}).not.toThrow();
await waitFor(() => {
expect(mockGetAccessTokenPayloadSecurely).toHaveBeenCalled();
});
// Verify identify was not called
expect(mockIdentify).not.toHaveBeenCalled();
});
it("should render children after user data is loaded", async () => {
const testUserId = "test-user-123";
const testEmail = "test@example.com";
mockGetAccessTokenPayloadSecurely.mockResolvedValue({
sub: testUserId,
email: testEmail,
});
mockUsePostHog.mockReturnValue({
identify: mockIdentify,
} as any);
const { getByText } = render(
<UserProvider>
<div>Test Child Content</div>
</UserProvider>,
);
// Initially should show loading
expect(getByText("Loading...")).toBeInTheDocument();
// After data loads, should show children
await waitFor(() => {
expect(getByText("Test Child Content")).toBeInTheDocument();
});
});
it("should handle session fetch errors gracefully", async () => {
const consoleErrorSpy = jest
.spyOn(console, "error")
.mockImplementation(() => {});
// Mock session to throw an error
mockGetAccessTokenPayloadSecurely.mockRejectedValue(
new Error("Session error"),
);
mockUsePostHog.mockReturnValue({
identify: mockIdentify,
} as any);
render(
<UserProvider>
<div>Test Child</div>
</UserProvider>,
);
// Should log error
await waitFor(() => {
expect(consoleErrorSpy).toHaveBeenCalledWith(
"Failed to get user because:",
expect.any(Error),
);
});
// Should not call identify
expect(mockIdentify).not.toHaveBeenCalled();
consoleErrorSpy.mockRestore();
});
});
});