Skip to content

Commit b828899

Browse files
committed
add tests
1 parent 50bc12c commit b828899

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

packages/replay/src/util/addEvent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ async function _addEvent(
8888
}
8989
}
9090

91-
function shouldAddEvent(replay: ReplayContainer, event: RecordingEvent): boolean {
91+
/** Exported only for tests. */
92+
export function shouldAddEvent(replay: ReplayContainer, event: RecordingEvent): boolean {
9293
if (!replay.eventBuffer || replay.isPaused() || !replay.isEnabled()) {
9394
return false;
9495
}

packages/replay/test/unit/util/addEvent.test.ts

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'jsdom-worker';
22

33
import { BASE_TIMESTAMP } from '../..';
4-
import { REPLAY_MAX_EVENT_BUFFER_SIZE } from '../../../src/constants';
4+
import { MAX_REPLAY_DURATION, REPLAY_MAX_EVENT_BUFFER_SIZE, SESSION_IDLE_PAUSE_DURATION } from '../../../src/constants';
55
import type { EventBufferProxy } from '../../../src/eventBuffer/EventBufferProxy';
6-
import { addEvent } from '../../../src/util/addEvent';
6+
import { addEvent, shouldAddEvent } from '../../../src/util/addEvent';
77
import { getTestEventIncremental } from '../../utils/getTestEvent';
88
import { setupReplayContainer } from '../../utils/setupReplayContainer';
99
import { useFakeTimers } from '../../utils/use-fake-timers';
@@ -57,4 +57,65 @@ describe('Unit | util | addEvent', () => {
5757

5858
expect(replay.isEnabled()).toEqual(false);
5959
});
60+
61+
describe('shouldAddEvent', () => {
62+
beforeEach(() => {
63+
jest.setSystemTime(BASE_TIMESTAMP);
64+
});
65+
66+
it('returns true by default', () => {
67+
const replay = setupReplayContainer({});
68+
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });
69+
70+
expect(shouldAddEvent(replay, event)).toEqual(true);
71+
});
72+
73+
it('returns false when paused', () => {
74+
const replay = setupReplayContainer({});
75+
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });
76+
77+
replay.pause();
78+
79+
expect(shouldAddEvent(replay, event)).toEqual(false);
80+
});
81+
82+
it('returns false when disabled', async () => {
83+
const replay = setupReplayContainer({});
84+
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });
85+
86+
await replay.stop();
87+
88+
expect(shouldAddEvent(replay, event)).toEqual(false);
89+
});
90+
91+
it('returns false if there is no eventBuffer', () => {
92+
const replay = setupReplayContainer({});
93+
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });
94+
95+
replay.eventBuffer = null;
96+
97+
expect(shouldAddEvent(replay, event)).toEqual(false);
98+
});
99+
100+
it('returns false when event is too old', () => {
101+
const replay = setupReplayContainer({});
102+
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP - SESSION_IDLE_PAUSE_DURATION - 1 });
103+
104+
expect(shouldAddEvent(replay, event)).toEqual(false);
105+
});
106+
107+
it('returns false if event is too long after initial timestamp', () => {
108+
const replay = setupReplayContainer({});
109+
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP + MAX_REPLAY_DURATION + 1 });
110+
111+
expect(shouldAddEvent(replay, event)).toEqual(false);
112+
});
113+
114+
it('returns true if event is withing max duration after after initial timestamp', () => {
115+
const replay = setupReplayContainer({});
116+
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP + MAX_REPLAY_DURATION - 1 });
117+
118+
expect(shouldAddEvent(replay, event)).toEqual(true);
119+
});
120+
});
60121
});

0 commit comments

Comments
 (0)