|
1
|
import React from "react" |
|
2
|
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { cleanup, fireEvent, render } from "@testing-library/react" |
|
4
|
|
|
5
|
// Mock dependencies |
|
6
|
const mockTrackWithSegment = vi.fn() |
|
7
|
const mockTrackWithPostHog = vi.fn() |
|
8
|
const mockUsePathname = vi.fn(() => "/test-path") |
|
9
|
|
|
10
|
const mockUseReoDevAnalytics = vi.fn() |
|
11
|
|
|
12
|
const mockUsePostHogAnalytics = vi.fn(() => ({ |
|
13
|
track: mockTrackWithPostHog, |
|
14
|
})) |
|
15
|
|
|
16
|
const mockUseSegmentAnalytics = vi.fn((options: unknown) => ({ |
|
17
|
track: mockTrackWithSegment, |
|
18
|
})) |
|
19
|
|
|
20
|
vi.mock("@/providers/Analytics/providers/segment", () => ({ |
|
21
|
useSegmentAnalytics: (options: unknown) => mockUseSegmentAnalytics(options), |
|
22
|
})) |
|
23
|
|
|
24
|
vi.mock("@/providers/Analytics/providers/posthog", () => ({ |
|
25
|
usePostHogAnalytics: () => mockUsePostHogAnalytics(), |
|
26
|
})) |
|
27
|
|
|
28
|
vi.mock("@/providers/Analytics/providers/reo-dev", () => ({ |
|
29
|
useReoDevAnalytics: (options: unknown) => mockUseReoDevAnalytics(options), |
|
30
|
})) |
|
31
|
|
|
32
|
vi.mock("next/navigation", () => ({ |
|
33
|
usePathname: () => mockUsePathname(), |
|
34
|
})) |
|
35
|
|
|
36
|
import { AnalyticsProvider, useAnalytics } from "../index" |
|
37
|
|
|
38
|
const TestComponent = () => { |
|
39
|
const { track } = useAnalytics() |
|
40
|
return ( |
|
41
|
<div> |
|
42
|
<button |
|
43
|
data-testid="track-event" |
|
44
|
onClick={() => |
|
45
|
track({ |
|
46
|
event: { |
|
47
|
event: "test-event", |
|
48
|
options: { custom: "data" }, |
|
49
|
}, |
|
50
|
}) |
|
51
|
} |
|
52
|
> |
|
53
|
Track Event |
|
54
|
</button> |
|
55
|
<button |
|
56
|
data-testid="track-with-callback" |
|
57
|
onClick={() => |
|
58
|
track({ |
|
59
|
event: { |
|
60
|
event: "callback-event", |
|
61
|
callback: () => { |
|
62
|
const elm = document.getElementById("callback-result") |
|
63
|
if (elm) { |
|
64
|
elm.textContent = "callback-called" |
|
65
|
} |
|
66
|
}, |
|
67
|
}, |
|
68
|
}) |
|
69
|
} |
|
70
|
> |
|
71
|
Track With Callback |
|
72
|
</button> |
|
73
|
<button |
|
74
|
data-testid="track-with-segment" |
|
75
|
onClick={() => |
|
76
|
track({ |
|
77
|
event: { |
|
78
|
event: "segment-event", |
|
79
|
tracker: "segment", |
|
80
|
}, |
|
81
|
}) |
|
82
|
} |
|
83
|
> |
|
84
|
Track With Segment |
|
85
|
</button> |
|
86
|
<button |
|
87
|
data-testid="track-with-posthog" |
|
88
|
onClick={() => |
|
89
|
track({ |
|
90
|
event: { |
|
91
|
event: "posthog-event", |
|
92
|
tracker: "posthog", |
|
93
|
}, |
|
94
|
}) |
|
95
|
} |
|
96
|
> |
|
97
|
Track With PostHog |
|
98
|
</button> |
|
99
|
<button |
|
100
|
data-testid="track-with-both" |
|
101
|
onClick={() => |
|
102
|
track({ |
|
103
|
event: { |
|
104
|
event: "both-event", |
|
105
|
tracker: ["segment", "posthog"], |
|
106
|
}, |
|
107
|
}) |
|
108
|
} |
|
109
|
> |
|
110
|
Track With Both |
|
111
|
</button> |
|
112
|
<div id="callback-result" data-testid="callback-result"></div> |
|
113
|
</div> |
|
114
|
) |
|
115
|
} |
|
116
|
|
|
117
|
beforeEach(() => { |
|
118
|
vi.clearAllMocks() |
|
119
|
mockUsePathname.mockReturnValue("/test-path") |
|
120
|
mockTrackWithSegment.mockResolvedValue(undefined) |
|
121
|
mockTrackWithPostHog.mockResolvedValue(undefined) |
|
122
|
mockUseSegmentAnalytics.mockReturnValue({ |
|
123
|
track: mockTrackWithSegment, |
|
124
|
}) |
|
125
|
mockUseReoDevAnalytics.mockReturnValue(undefined) |
|
126
|
|
|
127
|
// Mock document.title |
|
128
|
Object.defineProperty(document, "title", { |
|
129
|
writable: true, |
|
130
|
configurable: true, |
|
131
|
value: "Test Page Title", |
|
132
|
}) |
|
133
|
|
|
134
|
// Mock window.navigator.userAgent |
|
135
|
Object.defineProperty(window.navigator, "userAgent", { |
|
136
|
writable: true, |
|
137
|
configurable: true, |
|
138
|
value: "Mozilla/5.0 Test User Agent", |
|
139
|
}) |
|
140
|
|
|
141
|
vi.useFakeTimers() |
|
142
|
}) |
|
143
|
|
|
144
|
afterEach(() => { |
|
145
|
cleanup() |
|
146
|
vi.restoreAllMocks() |
|
147
|
}) |
|
148
|
|
|
149
|
describe("rendering", () => { |
|
150
|
test("renders children", () => { |
|
151
|
const { container } = render( |
|
152
|
<AnalyticsProvider> |
|
153
|
<div>Test</div> |
|
154
|
</AnalyticsProvider> |
|
155
|
) |
|
156
|
expect(container).toHaveTextContent("Test") |
|
157
|
}) |
|
158
|
}) |
|
159
|
|
|
160
|
describe("useAnalytics hook", () => { |
|
161
|
test("queues events when track is called", async () => { |
|
162
|
const { getByTestId } = render( |
|
163
|
<AnalyticsProvider> |
|
164
|
<TestComponent /> |
|
165
|
</AnalyticsProvider> |
|
166
|
) |
|
167
|
|
|
168
|
fireEvent.click(getByTestId("track-event")) |
|
169
|
|
|
170
|
vi.runAllTimers() |
|
171
|
|
|
172
|
expect(mockTrackWithPostHog).toHaveBeenCalled() |
|
173
|
}) |
|
174
|
|
|
175
|
test("uses posthog as default tracker", async () => { |
|
176
|
const { getByTestId } = render( |
|
177
|
<AnalyticsProvider> |
|
178
|
<TestComponent /> |
|
179
|
</AnalyticsProvider> |
|
180
|
) |
|
181
|
|
|
182
|
fireEvent.click(getByTestId("track-event")) |
|
183
|
|
|
184
|
vi.runAllTimers() |
|
185
|
|
|
186
|
expect(mockTrackWithPostHog).toHaveBeenCalledWith( |
|
187
|
expect.objectContaining({ |
|
188
|
event: "test-event", |
|
189
|
}) |
|
190
|
) |
|
191
|
expect(mockTrackWithSegment).not.toHaveBeenCalled() |
|
192
|
}) |
|
193
|
|
|
194
|
test("enriches event options with url, label, and os", async () => { |
|
195
|
const { getByTestId } = render( |
|
196
|
<AnalyticsProvider> |
|
197
|
<TestComponent /> |
|
198
|
</AnalyticsProvider> |
|
199
|
) |
|
200
|
|
|
201
|
fireEvent.click(getByTestId("track-event")) |
|
202
|
vi.runAllTimers() |
|
203
|
|
|
204
|
expect(mockTrackWithPostHog).toHaveBeenCalledWith( |
|
205
|
expect.objectContaining({ |
|
206
|
event: "test-event", |
|
207
|
options: expect.objectContaining({ |
|
208
|
url: "/test-path", |
|
209
|
label: "Test Page Title", |
|
210
|
os: "Mozilla/5.0 Test User Agent", |
|
211
|
custom: "data", |
|
212
|
}), |
|
213
|
}) |
|
214
|
) |
|
215
|
}) |
|
216
|
|
|
217
|
test("calls callback immediately even when event is queued", () => { |
|
218
|
const { getByTestId } = render( |
|
219
|
<AnalyticsProvider> |
|
220
|
<TestComponent /> |
|
221
|
</AnalyticsProvider> |
|
222
|
) |
|
223
|
|
|
224
|
fireEvent.click(getByTestId("track-with-callback")) |
|
225
|
vi.runAllTimers() |
|
226
|
// Callback should be called immediately, not waiting for async processing |
|
227
|
expect(getByTestId("callback-result")).toHaveTextContent("callback-called") |
|
228
|
}) |
|
229
|
|
|
230
|
test("tracks with segment when tracker is segment", async () => { |
|
231
|
const { getByTestId } = render( |
|
232
|
<AnalyticsProvider> |
|
233
|
<TestComponent /> |
|
234
|
</AnalyticsProvider> |
|
235
|
) |
|
236
|
|
|
237
|
fireEvent.click(getByTestId("track-with-segment")) |
|
238
|
vi.runAllTimers() |
|
239
|
|
|
240
|
expect(mockTrackWithSegment).toHaveBeenCalledWith( |
|
241
|
expect.objectContaining({ |
|
242
|
event: "segment-event", |
|
243
|
}) |
|
244
|
) |
|
245
|
expect(mockTrackWithPostHog).not.toHaveBeenCalled() |
|
246
|
}) |
|
247
|
|
|
248
|
test("tracks with posthog when tracker is posthog", async () => { |
|
249
|
const { getByTestId } = render( |
|
250
|
<AnalyticsProvider> |
|
251
|
<TestComponent /> |
|
252
|
</AnalyticsProvider> |
|
253
|
) |
|
254
|
|
|
255
|
fireEvent.click(getByTestId("track-with-posthog")) |
|
256
|
vi.runAllTimers() |
|
257
|
|
|
258
|
expect(mockTrackWithPostHog).toHaveBeenCalledWith( |
|
259
|
expect.objectContaining({ |
|
260
|
event: "posthog-event", |
|
261
|
}) |
|
262
|
) |
|
263
|
expect(mockTrackWithSegment).not.toHaveBeenCalled() |
|
264
|
}) |
|
265
|
|
|
266
|
test("tracks with both trackers when tracker is array", async () => { |
|
267
|
const { getByTestId } = render( |
|
268
|
<AnalyticsProvider> |
|
269
|
<TestComponent /> |
|
270
|
</AnalyticsProvider> |
|
271
|
) |
|
272
|
|
|
273
|
fireEvent.click(getByTestId("track-with-both")) |
|
274
|
vi.runAllTimers() |
|
275
|
|
|
276
|
expect(mockTrackWithSegment).toHaveBeenCalledWith( |
|
277
|
expect.objectContaining({ |
|
278
|
event: "both-event", |
|
279
|
}) |
|
280
|
) |
|
281
|
expect(mockTrackWithPostHog).toHaveBeenCalledWith( |
|
282
|
expect.objectContaining({ |
|
283
|
event: "both-event", |
|
284
|
}) |
|
285
|
) |
|
286
|
}) |
|
287
|
|
|
288
|
test("processes events in batches", async () => { |
|
289
|
const TestComponentWithMultipleEvents = () => { |
|
290
|
const { track } = useAnalytics() |
|
291
|
return ( |
|
292
|
<div> |
|
293
|
<button |
|
294
|
data-testid="track-multiple" |
|
295
|
onClick={() => { |
|
296
|
// Queue 12 events (more than batch size of 5) |
|
297
|
for (let i = 0; i < 12; i++) { |
|
298
|
track({ |
|
299
|
event: { |
|
300
|
event: `event-${i}`, |
|
301
|
}, |
|
302
|
}) |
|
303
|
} |
|
304
|
}} |
|
305
|
> |
|
306
|
Track Multiple |
|
307
|
</button> |
|
308
|
</div> |
|
309
|
) |
|
310
|
} |
|
311
|
|
|
312
|
const { getByTestId } = render( |
|
313
|
<AnalyticsProvider> |
|
314
|
<TestComponentWithMultipleEvents /> |
|
315
|
</AnalyticsProvider> |
|
316
|
) |
|
317
|
|
|
318
|
fireEvent.click(getByTestId("track-multiple")) |
|
319
|
|
|
320
|
expect(vi.getTimerCount()).toBe(3) |
|
321
|
vi.runAllTimers() |
|
322
|
expect(mockTrackWithPostHog).toHaveBeenCalledTimes(12) |
|
323
|
}) |
|
324
|
|
|
325
|
test("preserves custom options when enriching event", async () => { |
|
326
|
const TestComponentWithCustomOptions = () => { |
|
327
|
const { track } = useAnalytics() |
|
328
|
return ( |
|
329
|
<button |
|
330
|
data-testid="track-custom" |
|
331
|
onClick={() => |
|
332
|
track({ |
|
333
|
event: { |
|
334
|
event: "custom-event", |
|
335
|
options: { |
|
336
|
customKey: "customValue", |
|
337
|
nested: { |
|
338
|
key: "value", |
|
339
|
}, |
|
340
|
}, |
|
341
|
}, |
|
342
|
}) |
|
343
|
} |
|
344
|
> |
|
345
|
Track Custom |
|
346
|
</button> |
|
347
|
) |
|
348
|
} |
|
349
|
|
|
350
|
const { getByTestId } = render( |
|
351
|
<AnalyticsProvider> |
|
352
|
<TestComponentWithCustomOptions /> |
|
353
|
</AnalyticsProvider> |
|
354
|
) |
|
355
|
|
|
356
|
fireEvent.click(getByTestId("track-custom")) |
|
357
|
vi.runAllTimers() |
|
358
|
|
|
359
|
expect(mockTrackWithPostHog).toHaveBeenCalledWith( |
|
360
|
expect.objectContaining({ |
|
361
|
event: "custom-event", |
|
362
|
}) |
|
363
|
) |
|
364
|
}) |
|
365
|
|
|
366
|
test("updates pathname when it changes", async () => { |
|
367
|
const { getByTestId, rerender } = render( |
|
368
|
<AnalyticsProvider> |
|
369
|
<TestComponent /> |
|
370
|
</AnalyticsProvider> |
|
371
|
) |
|
372
|
|
|
373
|
// Track first event |
|
374
|
fireEvent.click(getByTestId("track-event")) |
|
375
|
vi.runAllTimers() |
|
376
|
|
|
377
|
expect(mockTrackWithPostHog).toHaveBeenCalledWith( |
|
378
|
expect.objectContaining({ |
|
379
|
options: expect.objectContaining({ |
|
380
|
url: "/test-path", |
|
381
|
}), |
|
382
|
}) |
|
383
|
) |
|
384
|
|
|
385
|
// Update pathname |
|
386
|
mockUsePathname.mockReturnValue("/new-path") |
|
387
|
mockTrackWithPostHog.mockClear() |
|
388
|
|
|
389
|
// Re-render to trigger pathname change |
|
390
|
rerender( |
|
391
|
<AnalyticsProvider> |
|
392
|
<TestComponent /> |
|
393
|
</AnalyticsProvider> |
|
394
|
) |
|
395
|
|
|
396
|
// Track second event |
|
397
|
fireEvent.click(getByTestId("track-event")) |
|
398
|
vi.runAllTimers() |
|
399
|
|
|
400
|
expect(mockTrackWithPostHog).toHaveBeenCalledWith( |
|
401
|
expect.objectContaining({ |
|
402
|
options: expect.objectContaining({ |
|
403
|
url: "/new-path", |
|
404
|
}), |
|
405
|
}) |
|
406
|
) |
|
407
|
}) |
|
408
|
|
|
409
|
test("throws error when used outside provider", () => { |
|
410
|
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {}) |
|
411
|
|
|
412
|
expect(() => { |
|
413
|
render(<TestComponent />) |
|
414
|
}).toThrow("useAnalytics must be used within a AnalyticsProvider") |
|
415
|
|
|
416
|
consoleSpy.mockRestore() |
|
417
|
}) |
|
418
|
}) |
|
419
|
|
|
420
|
describe("provider props", () => { |
|
421
|
test("passes segmentWriteKey to useSegmentAnalytics", () => { |
|
422
|
render( |
|
423
|
<AnalyticsProvider segmentWriteKey="test-segment-key"> |
|
424
|
<div>Test</div> |
|
425
|
</AnalyticsProvider> |
|
426
|
) |
|
427
|
|
|
428
|
expect(mockUseSegmentAnalytics).toHaveBeenCalledWith( |
|
429
|
expect.objectContaining({ |
|
430
|
segmentWriteKey: "test-segment-key", |
|
431
|
setEventsQueue: expect.any(Function), |
|
432
|
}) |
|
433
|
) |
|
434
|
}) |
|
435
|
|
|
436
|
test("passes reoDevKey to useReoDevAnalytics", () => { |
|
437
|
render( |
|
438
|
<AnalyticsProvider reoDevKey="test-reo-key"> |
|
439
|
<div>Test</div> |
|
440
|
</AnalyticsProvider> |
|
441
|
) |
|
442
|
|
|
443
|
expect(mockUseReoDevAnalytics).toHaveBeenCalledWith({ |
|
444
|
reoDevKey: "test-reo-key", |
|
445
|
}) |
|
446
|
}) |
|
447
|
}) |