-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathautoSaveSession.test.ts
More file actions
59 lines (48 loc) · 1.65 KB
/
autoSaveSession.test.ts
File metadata and controls
59 lines (48 loc) · 1.65 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
/**
* @vitest-environment jsdom
*/
import '../utils/mock-internal-setTimeout';
import { EventType } from '@sentry-internal/rrweb';
import { afterEach, beforeAll, describe, expect, test, vi } from 'vitest';
import { saveSession } from '../../src/session/saveSession';
import type { RecordingEvent } from '../../src/types';
import { addEvent } from '../../src/util/addEvent';
import { resetSdkMock } from '../mocks/resetSdkMock';
vi.mock('../../src/session/saveSession', () => {
return {
saveSession: vi.fn(),
};
});
describe('Integration | autoSaveSession', () => {
beforeAll(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.clearAllMocks();
});
test.each([
['with stickySession=true', true, 1],
['with stickySession=false', false, 0],
])('%s', async (_: string, stickySession: boolean, addSummand: number) => {
const { replay } = await resetSdkMock({
replayOptions: {
stickySession,
},
});
// Initially called up to three times: once for start, then once for replay.updateSessionActivity & once for segmentId increase
expect(saveSession).toHaveBeenCalledTimes(addSummand * 3);
replay['_updateSessionActivity']();
expect(saveSession).toHaveBeenCalledTimes(addSummand * 4);
// In order for runFlush to actually do something, we need to add an event
const event = {
type: EventType.Custom,
data: {
tag: 'test custom',
},
timestamp: new Date().valueOf(),
} as RecordingEvent;
addEvent(replay, event);
await Promise.all([replay['_runFlush'](), vi.runAllTimersAsync()]);
expect(saveSession).toHaveBeenCalledTimes(addSummand * 5);
});
});