1
1
jest . mock ( './../../src/util/isInternal' , ( ) => ( {
2
2
isInternal : jest . fn ( ( ) => true ) ,
3
3
} ) ) ;
4
+
4
5
import { BASE_TIMESTAMP , RecordMock } from '@test' ;
5
6
import { PerformanceEntryResource } from '@test/fixtures/performanceEntry/resource' ;
6
7
import { resetSdkMock } from '@test/mocks' ;
7
8
import { DomHandler , MockTransportSend } from '@test/types' ;
9
+ import { EventType } from 'rrweb' ;
8
10
9
11
import { Replay } from '../../src' ;
10
12
import { MAX_SESSION_LIFE , REPLAY_SESSION_KEY , VISIBILITY_CHANGE_TIMEOUT } from '../../src/session/constants' ;
13
+ import { RecordingEvent } from '../../src/types' ;
11
14
import { useFakeTimers } from '../utils/use-fake-timers' ;
12
15
13
16
useFakeTimers ( ) ;
@@ -17,6 +20,77 @@ async function advanceTimers(time: number) {
17
20
await new Promise ( process . nextTick ) ;
18
21
}
19
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
+
20
94
describe ( 'Replay' , ( ) => {
21
95
let replay : Replay ;
22
96
let mockRecord : RecordMock ;
@@ -37,7 +111,7 @@ describe('Replay', () => {
37
111
( { mockRecord, mockTransportSend, domHandler, replay, spyCaptureException } = await resetSdkMock ( {
38
112
sessionSampleRate : 1.0 ,
39
113
errorSampleRate : 0.0 ,
40
- stickySession : true ,
114
+ stickySession : false ,
41
115
} ) ) ;
42
116
43
117
jest . spyOn ( replay , 'flush' ) ;
@@ -69,23 +143,6 @@ describe('Replay', () => {
69
143
replay . stop ( ) ;
70
144
} ) ;
71
145
72
- it ( 'calls rrweb.record with custom options' , async ( ) => {
73
- ( { mockRecord, mockTransportSend, domHandler, replay } = await resetSdkMock ( {
74
- ignoreClass : 'sentry-test-ignore' ,
75
- } ) ) ;
76
- expect ( mockRecord . mock . calls [ 0 ] [ 0 ] ) . toMatchInlineSnapshot ( `
77
- Object {
78
- "blockClass": "sentry-block",
79
- "blockSelector": "[data-sentry-block],img,image,svg,path,rect,area,video,object,picture,embed,map,audio",
80
- "emit": [Function],
81
- "ignoreClass": "sentry-test-ignore",
82
- "maskAllInputs": true,
83
- "maskTextClass": "sentry-mask",
84
- "maskTextSelector": "*",
85
- }
86
- ` ) ;
87
- } ) ;
88
-
89
146
it ( 'should have a session after setup' , ( ) => {
90
147
expect ( replay . session ) . toMatchObject ( {
91
148
lastActivity : BASE_TIMESTAMP ,
0 commit comments