11jest . mock ( './../../src/util/isInternal' , ( ) => ( {
22 isInternal : jest . fn ( ( ) => true ) ,
33} ) ) ;
4+
45import { BASE_TIMESTAMP , RecordMock } from '@test' ;
56import { PerformanceEntryResource } from '@test/fixtures/performanceEntry/resource' ;
67import { resetSdkMock } from '@test/mocks' ;
78import { DomHandler , MockTransportSend } from '@test/types' ;
9+ import { EventType } from 'rrweb' ;
810
911import { Replay } from '../../src' ;
1012import { MAX_SESSION_LIFE , REPLAY_SESSION_KEY , VISIBILITY_CHANGE_TIMEOUT } from '../../src/session/constants' ;
13+ import { RecordingEvent } from '../../src/types' ;
1114import { useFakeTimers } from '../utils/use-fake-timers' ;
1215
1316useFakeTimers ( ) ;
@@ -17,6 +20,77 @@ async function advanceTimers(time: number) {
1720 await new Promise ( process . nextTick ) ;
1821}
1922
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+
2094describe ( 'Replay' , ( ) => {
2195 let replay : Replay ;
2296 let mockRecord : RecordMock ;
@@ -37,7 +111,7 @@ describe('Replay', () => {
37111 ( { mockRecord, mockTransportSend, domHandler, replay, spyCaptureException } = await resetSdkMock ( {
38112 sessionSampleRate : 1.0 ,
39113 errorSampleRate : 0.0 ,
40- stickySession : true ,
114+ stickySession : false ,
41115 } ) ) ;
42116
43117 jest . spyOn ( replay , 'flush' ) ;
@@ -69,23 +143,6 @@ describe('Replay', () => {
69143 replay . stop ( ) ;
70144 } ) ;
71145
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-
89146 it ( 'should have a session after setup' , ( ) => {
90147 expect ( replay . session ) . toMatchObject ( {
91148 lastActivity : BASE_TIMESTAMP ,
0 commit comments