-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcreateTracker.test.tsx
More file actions
263 lines (215 loc) · 7.29 KB
/
Copy pathcreateTracker.test.tsx
File metadata and controls
263 lines (215 loc) · 7.29 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { render, renderHook } from "@testing-library/react";
import { vi } from "vitest";
import { createTracker } from "..";
import { sleep } from "./utils";
const initFn = vi.fn();
const sendFn = vi.fn();
const clickFn = vi.fn();
const focusFn = vi.fn();
const impressionFn = vi.fn();
const pageViewFn = vi.fn();
const anyFn = expect.any(Function);
const [Track, useTracker] = createTracker({
init: initFn,
send: sendFn,
DOMEvents: {
onClick: clickFn,
onFocus: focusFn,
},
impression: {
onImpression: impressionFn,
},
pageView: {
onPageView: pageViewFn,
},
});
describe("init", async () => {
it("init function should be called when the Tracker.Provider is mounted", () => {
render(
<Track.Provider initialContext={{}}>
<div>test</div>
</Track.Provider>,
);
expect(initFn).toHaveBeenCalledOnce();
});
it("init function should always be resolved first before any other functions run", async () => {
initFn.mockImplementationOnce(() => sleep(500));
const context = { userId: "id" };
const clickParams = { a: 1 };
const pageViewParams = { page: "/home" };
const page = render(
<Track.Provider initialContext={context}>
<div>test</div>
<Track.Click params={clickParams}>
<button type="button">click</button>
</Track.Click>
<Track.PageView params={pageViewParams} />
</Track.Provider>,
);
page.getByText("click").click();
expect(clickFn).not.toHaveBeenCalled();
expect(pageViewFn).not.toHaveBeenCalled();
await sleep(500);
expect(clickFn).toHaveBeenCalledWith(clickParams, context, anyFn);
expect(pageViewFn).toHaveBeenCalledWith(pageViewParams, context, anyFn);
});
});
describe("events", () => {
it("events.onClick should be called when the element inside Track.Click is clicked", async () => {
const context = { userId: "id" };
const clickParams = { a: 1 };
const page = render(
<Track.Provider initialContext={context}>
<div>test</div>
<Track.Click params={clickParams}>
<button type="button">click</button>
</Track.Click>
</Track.Provider>,
);
page.getByText("click").click();
await sleep(1);
expect(clickFn).toHaveBeenCalledWith(clickParams, context, anyFn);
});
it("events.onClick can be called manually by using useTracker hook", async () => {
const context = { userId: "id" };
const clickParams = { a: 1 };
const ButtonWithTracker = () => {
const tracker = useTracker();
return (
<button type="button" onClick={() => tracker.events.onClick(clickParams)}>
click
</button>
);
};
const page = render(
<Track.Provider initialContext={context}>
<div>test</div>
<ButtonWithTracker />
</Track.Provider>,
);
page.getByText("click").click();
await sleep(1);
expect(clickFn).toHaveBeenCalledWith(clickParams, context, anyFn);
});
it("any DOM event such as onFoucs can be called declaratively using Tracker.Event", async () => {
const context = { userId: "id" };
const focusEventParams = { a: 1 };
const page = render(
<Track.Provider initialContext={context}>
<div>test</div>
<Track.DOMEvent type="onFocus" params={focusEventParams}>
<input />
</Track.DOMEvent>
</Track.Provider>,
);
page.getByRole("textbox").focus();
await sleep(1);
expect(focusFn).toHaveBeenCalledWith(focusEventParams, context, anyFn);
});
});
describe("page view", () => {
it("page view should be called when the page is loaded", async () => {
const context = { userId: "id" };
const pageViewParams = { a: 1 };
render(
<Track.Provider initialContext={context}>
<div>test</div>
<Track.PageView params={pageViewParams} />
</Track.Provider>,
);
await sleep(1);
expect(pageViewFn).toHaveBeenCalledWith(pageViewParams, context, anyFn);
});
it("should not call onPageView again when the page rerenders", async () => {
const context = { userId: "id" };
const pageViewParams = { a: 1 };
const Page = () => {
return (
<Track.Provider initialContext={context}>
<div>test</div>
<Track.PageView params={pageViewParams} />
</Track.Provider>
);
};
const page = render(<Page />);
page.rerender(<Page />);
await sleep(1);
expect(pageViewFn).toHaveBeenCalledOnce();
});
});
describe("set context", () => {
it("new context should be set when the Tracker.SetContext is mounted", async () => {
const context = { userId: "id" };
const newContext = { userId: "newId" };
const clickParams = { a: 1 };
const pageViewParams = { a: 1 };
const page = render(
<Track.Provider initialContext={context}>
<div>test</div>
<Track.SetContext context={newContext} />
<Track.Click params={clickParams}>
<button type="button">click</button>
</Track.Click>
<Track.PageView params={pageViewParams} />
</Track.Provider>,
);
page.getByText("click").click();
await sleep(1);
expect(pageViewFn).toHaveBeenNthCalledWith(1, pageViewParams, newContext, anyFn);
});
it("Track.SetContext can set new context using previous context", async () => {
const context = { userId: "id" };
const clickParams = { a: 1 };
const page = render(
<Track.Provider initialContext={context}>
<div>test</div>
<Track.Click params={clickParams}>
<button type="button">click</button>
</Track.Click>
<Track.SetContext
context={(prev: { userId: string }) => ({
...prev,
test: true,
})}
/>
</Track.Provider>,
);
page.getByText("click").click();
await sleep(1);
expect(clickFn).toHaveBeenNthCalledWith(1, clickParams, { ...context, test: true }, anyFn);
});
it("can set context using useTracker hook", async () => {
const context = { userId: "id" };
const newContext = { userId: "newId" };
const clickParams = { a: 1 };
const { result } = renderHook(() => useTracker(), {
wrapper: ({ children }) => <Track.Provider initialContext={context}>{children}</Track.Provider>,
});
result.current.setContext(newContext);
result.current.events.onClick(clickParams);
await sleep(1);
expect(clickFn).toHaveBeenNthCalledWith(1, clickParams, newContext, anyFn);
});
it("can set context in init function", async () => {
const context = { userId: "id", test: false };
clickFn.mockImplementationOnce((_, context) => {
return context;
});
pageViewFn.mockImplementationOnce((_, __, setContext) => {
setContext((prev: { userId: string; test: boolean }) => ({ ...prev, test: true }));
});
const page = render(
<Track.Provider initialContext={context}>
<Track.Click params={{ a: 1 }}>
<button type="button">click</button>
</Track.Click>
<Track.PageView params={{ b: 1 }} />
</Track.Provider>,
);
page.getByText("click").click();
await sleep(1);
expect(pageViewFn).toHaveBeenCalledWith({ b: 1 }, { userId: "id", test: false }, anyFn);
// expect context to have been updated in the pageViewFn
expect(clickFn).toHaveReturnedWith({ userId: "id", test: true });
});
});