@@ -6,9 +6,11 @@ import { BASE_TIMESTAMP, RecordMock } from '@test';
6
6
import { PerformanceEntryResource } from '@test/fixtures/performanceEntry/resource' ;
7
7
import { resetSdkMock } from '@test/mocks' ;
8
8
import { DomHandler , MockTransportSend } from '@test/types' ;
9
+ import { EventType } from 'rrweb' ;
9
10
10
11
import { Replay } from '../../src' ;
11
12
import { MAX_SESSION_LIFE , REPLAY_SESSION_KEY , VISIBILITY_CHANGE_TIMEOUT } from '../../src/session/constants' ;
13
+ import { RecordingEvent } from '../../src/types' ;
12
14
import { useFakeTimers } from '../utils/use-fake-timers' ;
13
15
14
16
useFakeTimers ( ) ;
@@ -18,6 +20,77 @@ async function advanceTimers(time: number) {
18
20
await new Promise ( process . nextTick ) ;
19
21
}
20
22
23
+ describe ( 'Replay with custom mock' , ( ) => {
24
+ afterEach ( ( ) => {
25
+ jest . clearAllMocks ( ) ;
26
+ } ) ;
27
+
28
+ it ( 'calls rrweb.record with custom options' , async ( ) => {
29
+ const { mockRecord } = await resetSdkMock ( {
30
+ ignoreClass : 'sentry-test-ignore' ,
31
+ stickySession : false ,
32
+ sessionSampleRate : 1.0 ,
33
+ errorSampleRate : 0.0 ,
34
+ } ) ;
35
+ expect ( mockRecord . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
36
+ Object {
37
+ "blockClass": "sentry-block",
38
+ "blockSelector": "[data-sentry-block],img,image,svg,path,rect,area,video,object,picture,embed,map,audio",
39
+ "emit": [Function],
40
+ "ignoreClass": "sentry-test-ignore",
41
+ "maskAllInputs": true,
42
+ "maskTextClass": "sentry-mask",
43
+ "maskTextSelector": "*",
44
+ }
45
+ ` ) ;
46
+ } ) ;
47
+
48
+ describe ( 'auto save session' , ( ) => {
49
+ test . each ( [
50
+ [ 'with stickySession=true' , true , 1 ] ,
51
+ [ 'with stickySession=false' , false , 0 ] ,
52
+ ] ) ( '%s' , async ( _ : string , stickySession : boolean , addSummand : number ) => {
53
+ let saveSessionSpy ;
54
+
55
+ jest . mock ( '../../src/session/saveSession' , ( ) => {
56
+ saveSessionSpy = jest . fn ( ) ;
57
+
58
+ return {
59
+ saveSession : saveSessionSpy ,
60
+ } ;
61
+ } ) ;
62
+
63
+ const { replay } = await resetSdkMock ( {
64
+ stickySession,
65
+ sessionSampleRate : 1.0 ,
66
+ errorSampleRate : 0.0 ,
67
+ } ) ;
68
+
69
+ // Initially called up to three times: once for start, then once for replay.updateSessionActivity & once for segmentId increase
70
+ expect ( saveSessionSpy ) . toHaveBeenCalledTimes ( addSummand * 3 ) ;
71
+
72
+ replay . updateSessionActivity ( ) ;
73
+
74
+ expect ( saveSessionSpy ) . toHaveBeenCalledTimes ( addSummand * 4 ) ;
75
+
76
+ // In order for runFlush to actually do something, we need to add an event
77
+ const event = {
78
+ type : EventType . Custom ,
79
+ data : {
80
+ tag : 'test custom' ,
81
+ } ,
82
+ timestamp : new Date ( ) . valueOf ( ) ,
83
+ } as RecordingEvent ;
84
+
85
+ replay . addEvent ( event ) ;
86
+
87
+ await replay . runFlush ( ) ;
88
+
89
+ expect ( saveSessionSpy ) . toHaveBeenCalledTimes ( addSummand * 5 ) ;
90
+ } ) ;
91
+ } ) ;
92
+ } ) ;
93
+
21
94
describe ( 'Replay' , ( ) => {
22
95
let replay : Replay ;
23
96
let mockRecord : RecordMock ;
@@ -38,7 +111,7 @@ describe('Replay', () => {
38
111
( { mockRecord, mockTransportSend, domHandler, replay, spyCaptureException } = await resetSdkMock ( {
39
112
sessionSampleRate : 1.0 ,
40
113
errorSampleRate : 0.0 ,
41
- stickySession : true ,
114
+ stickySession : false ,
42
115
} ) ) ;
43
116
44
117
jest . spyOn ( replay , 'flush' ) ;
@@ -68,23 +141,6 @@ describe('Replay', () => {
68
141
replay . stop ( ) ;
69
142
} ) ;
70
143
71
- it ( 'calls rrweb.record with custom options' , async ( ) => {
72
- ( { mockRecord, mockTransportSend, domHandler, replay } = await resetSdkMock ( {
73
- ignoreClass : 'sentry-test-ignore' ,
74
- } ) ) ;
75
- expect ( mockRecord . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
76
- Object {
77
- "blockClass": "sentry-block",
78
- "blockSelector": "[data-sentry-block],img,image,svg,path,rect,area,video,object,picture,embed,map,audio",
79
- "emit": [Function],
80
- "ignoreClass": "sentry-test-ignore",
81
- "maskAllInputs": true,
82
- "maskTextClass": "sentry-mask",
83
- "maskTextSelector": "*",
84
- }
85
- ` ) ;
86
- } ) ;
87
-
88
144
it ( 'should have a session after setup' , ( ) => {
89
145
expect ( replay . session ) . toMatchObject ( {
90
146
lastActivity : BASE_TIMESTAMP ,
0 commit comments