debian-mirror-gitlab/spec/frontend/diffs/utils/queue_events_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

72 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-10-27 15:23:28 +05:30
import api from '~/api';
2022-07-16 23:28:13 +05:30
import { DEFER_DURATION, TRACKING_CAP_KEY, TRACKING_CAP_LENGTH } from '~/diffs/constants';
2021-10-27 15:23:28 +05:30
import { queueRedisHllEvents } from '~/diffs/utils/queue_events';
jest.mock('~/api', () => ({
trackRedisHllUserEvent: jest.fn(),
}));
2022-07-16 23:28:13 +05:30
beforeAll(() => {
localStorage.clear();
});
2021-10-27 15:23:28 +05:30
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();
2022-07-16 23:28:13 +05:30
expect(localStorage.getItem(TRACKING_CAP_KEY)).toBe(null);
2021-10-27 15:23:28 +05:30
});
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;
});
});
2022-07-16 23:28:13 +05:30
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();
});
});
2021-10-27 15:23:28 +05:30
});
});