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
|
import api from '~/api';
import { DEFER_DURATION, TRACKING_CAP_KEY, TRACKING_CAP_LENGTH } from '~/diffs/constants';
import { queueRedisHllEvents } from '~/diffs/utils/queue_events';
jest.mock('~/api', () => ({
trackRedisHllUserEvent: jest.fn(),
}));
beforeAll(() => {
localStorage.clear();
});
describe('diffs events queue', () => {
describe('queueRedisHllEvents', () => {
it('does not dispatch the event immediately', () => {
queueRedisHllEvents(['know_event']);
expect(api.trackRedisHllUserEvent).not.toHaveBeenCalled();
});
it('does dispatch the event after the defer duration', () => {
queueRedisHllEvents(['know_event']);
jest.advanceTimersByTime(DEFER_DURATION + 1);
expect(api.trackRedisHllUserEvent).toHaveBeenCalled();
expect(localStorage.getItem(TRACKING_CAP_KEY)).toBe(null);
});
it('increase defer duration based on the provided events count', () => {
let deferDuration = DEFER_DURATION + 1;
const events = ['know_event_a', 'know_event_b', 'know_event_c'];
queueRedisHllEvents(events);
expect(api.trackRedisHllUserEvent).not.toHaveBeenCalled();
events.forEach((event, index) => {
jest.advanceTimersByTime(deferDuration);
expect(api.trackRedisHllUserEvent).toHaveBeenLastCalledWith(event);
deferDuration *= index + 1;
});
});
describe('with tracking cap verification', () => {
const currentTimestamp = Date.now();
beforeEach(() => {
localStorage.clear();
});
it('dispatches the event if cap value is not found', () => {
queueRedisHllEvents(['know_event'], { verifyCap: true });
jest.advanceTimersByTime(DEFER_DURATION + 1);
expect(api.trackRedisHllUserEvent).toHaveBeenCalled();
expect(localStorage.getItem(TRACKING_CAP_KEY)).toBe(currentTimestamp.toString());
});
it('dispatches the event if cap value is less than limit', () => {
localStorage.setItem(TRACKING_CAP_KEY, 1);
queueRedisHllEvents(['know_event'], { verifyCap: true });
jest.advanceTimersByTime(DEFER_DURATION + 1);
expect(api.trackRedisHllUserEvent).toHaveBeenCalled();
expect(localStorage.getItem(TRACKING_CAP_KEY)).toBe(currentTimestamp.toString());
});
it('does not dispatch the event if cap value is greater than limit', () => {
localStorage.setItem(TRACKING_CAP_KEY, currentTimestamp - (TRACKING_CAP_LENGTH + 1));
queueRedisHllEvents(['know_event'], { verifyCap: true });
jest.advanceTimersByTime(DEFER_DURATION + 1);
expect(api.trackRedisHllUserEvent).toHaveBeenCalled();
});
});
});
});
|