Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Sep 11, 2023
1 parent e9931f4 commit 460595a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/replay/src/util/addEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ async function _addEvent(
}
}

function shouldAddEvent(replay: ReplayContainer, event: RecordingEvent): boolean {
/** Exported only for tests. */
export function shouldAddEvent(replay: ReplayContainer, event: RecordingEvent): boolean {
if (!replay.eventBuffer || replay.isPaused() || !replay.isEnabled()) {
return false;
}
Expand Down
65 changes: 63 additions & 2 deletions packages/replay/test/unit/util/addEvent.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'jsdom-worker';

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

expect(replay.isEnabled()).toEqual(false);
});

describe('shouldAddEvent', () => {
beforeEach(() => {
jest.setSystemTime(BASE_TIMESTAMP);
});

it('returns true by default', () => {
const replay = setupReplayContainer({});
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });

expect(shouldAddEvent(replay, event)).toEqual(true);
});

it('returns false when paused', () => {
const replay = setupReplayContainer({});
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });

replay.pause();

expect(shouldAddEvent(replay, event)).toEqual(false);
});

it('returns false when disabled', async () => {
const replay = setupReplayContainer({});
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });

await replay.stop();

expect(shouldAddEvent(replay, event)).toEqual(false);
});

it('returns false if there is no eventBuffer', () => {
const replay = setupReplayContainer({});
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });

replay.eventBuffer = null;

expect(shouldAddEvent(replay, event)).toEqual(false);
});

it('returns false when event is too old', () => {
const replay = setupReplayContainer({});
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP - SESSION_IDLE_PAUSE_DURATION - 1 });

expect(shouldAddEvent(replay, event)).toEqual(false);
});

it('returns false if event is too long after initial timestamp', () => {
const replay = setupReplayContainer({});
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP + MAX_REPLAY_DURATION + 1 });

expect(shouldAddEvent(replay, event)).toEqual(false);
});

it('returns true if event is withing max duration after after initial timestamp', () => {
const replay = setupReplayContainer({});
const event = getTestEventIncremental({ timestamp: BASE_TIMESTAMP + MAX_REPLAY_DURATION - 1 });

expect(shouldAddEvent(replay, event)).toEqual(true);
});
});
});

0 comments on commit 460595a

Please sign in to comment.