-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbatch.test.tsx
More file actions
212 lines (160 loc) · 5.39 KB
/
Copy pathbatch.test.tsx
File metadata and controls
212 lines (160 loc) · 5.39 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
import { render } from "@testing-library/react";
import { createTracker } from "..";
import { anyFn, sleep } from "./utils";
const initFn = vi.fn();
const clickFn = vi.fn();
const flushFn = vi.fn();
const pageViewFn = vi.fn();
const FLUSH_INTERVAL = 500;
const [Track] = createTracker({
init: initFn,
DOMEvents: {
onClick: clickFn,
},
pageView: {
onPageView: pageViewFn,
},
batch: {
enable: true,
thresholdSize: 5,
interval: FLUSH_INTERVAL,
onFlush: flushFn,
},
});
describe("batching behavior", () => {
it("should flush the batch when the threshold size is reached", async () => {
const clickParams = { a: 1 };
const page = render(
<Track.Provider initialContext={{}}>
<Track.Click params={clickParams}>
<button type="button">click</button>
</Track.Click>
</Track.Provider>,
);
expect(flushFn).not.toHaveBeenCalled();
clickFn.mockImplementation((params) => params);
const button = page.getByText("click");
button.click();
expect(flushFn).not.toHaveBeenCalled();
button.click();
button.click();
button.click();
button.click();
await sleep(1);
expect(clickFn).toHaveBeenCalledTimes(5);
expect(flushFn).toHaveBeenNthCalledWith(
1,
[clickParams, clickParams, clickParams, clickParams, clickParams],
false,
);
});
it("should flush the batch when the interval is reached", async () => {
const clickParams = { a: 1 };
const page = render(
<Track.Provider initialContext={{}}>
<Track.Click params={clickParams}>
<button type="button">click</button>
</Track.Click>
</Track.Provider>,
);
clickFn.mockImplementation((params) => params);
const button = page.getByText("click");
button.click();
button.click();
button.click();
expect(flushFn).not.toHaveBeenCalled();
await sleep(FLUSH_INTERVAL);
expect(clickFn).toHaveBeenCalledTimes(3);
expect(flushFn).toHaveBeenNthCalledWith(1, [clickParams, clickParams, clickParams], false);
});
it("should flush the batch when the page is unmounted", async () => {
const clickParams = { a: 1 };
const page = render(
<Track.Provider initialContext={{}}>
<Track.Click params={clickParams}>
<button type="button">click</button>
</Track.Click>
</Track.Provider>,
);
clickFn.mockImplementation((params) => params);
const button = page.getByText("click");
button.click();
button.click();
expect(flushFn).not.toHaveBeenCalled();
await sleep(1);
page.unmount();
expect(flushFn).toHaveBeenNthCalledWith(1, [clickParams, clickParams], false);
});
it("should flush the batch when the browser is closed", async () => {
const clickParams = { a: 1 };
const page = render(
<Track.Provider initialContext={{}}>
<Track.Click params={clickParams}>
<button type="button">click</button>
</Track.Click>
</Track.Provider>,
);
clickFn.mockImplementation((params) => params);
const button = page.getByText("click");
button.click();
button.click();
await sleep(1);
expect(flushFn).not.toHaveBeenCalled();
document.dispatchEvent(new Event("beforeunload"));
expect(flushFn).toHaveBeenCalledWith([clickParams, clickParams], true);
});
it("should wait for init function to be resolved before interval starts", async () => {
const clickParams = { a: 1 };
initFn.mockImplementationOnce(() => sleep(300));
const page = render(
<Track.Provider initialContext={{}}>
<Track.Click params={clickParams}>
<button type="button">click</button>
</Track.Click>
</Track.Provider>,
);
const button = page.getByText("click");
button.click();
button.click();
// clickFn should not have been called yet
expect(clickFn).not.toHaveBeenCalled();
// wait for init function to be resolved
await sleep(300);
// clickFn should have been called now
expect(clickFn).toHaveBeenCalledTimes(2);
expect(flushFn).not.toHaveBeenCalled();
// wait for interval
await sleep(FLUSH_INTERVAL);
// flushFn should have been called now
expect(flushFn).toHaveBeenCalled();
});
});
describe("event function race condition", () => {
it("should assure the event function's order when the event function", async () => {
const event1 = () => sleep(500);
const event2 = () => sleep(300);
pageViewFn.mockImplementationOnce(event1);
clickFn.mockImplementationOnce(event2);
const page = render(
<Track.Provider initialContext={{}}>
<Track.Click params={{ b: 1 }}>
<button type="button">click</button>
</Track.Click>
<Track.PageView params={{ a: 1 }} />
</Track.Provider>,
);
page.getByText("click").click();
await sleep(1);
// pageViewFn is not resolved yet. So, clickFn is not called yet.
expect(pageViewFn).toHaveBeenCalled();
expect(pageViewFn).not.toHaveResolved();
expect(clickFn).not.toHaveBeenCalled();
await sleep(500); // wait for pageViewFn to be resolved
// pageViewFn is resolved now. So, clickFn is called now.
expect(pageViewFn).toHaveResolved();
expect(clickFn).toHaveBeenCalledWith({ b: 1 }, {}, anyFn);
expect(clickFn).not.toHaveResolved();
await sleep(300); // wait for clickFn to be resolved
expect(clickFn).toHaveResolved();
});
});