@@ -6,9 +6,11 @@ import { BASE_TIMESTAMP, RecordMock } from '@test';
66import { PerformanceEntryResource } from '@test/fixtures/performanceEntry/resource' ;
77import { resetSdkMock } from '@test/mocks' ;
88import { DomHandler , MockTransportSend } from '@test/types' ;
9+ import { EventType } from 'rrweb' ;
910
1011import { Replay } from '../../src' ;
1112import { MAX_SESSION_LIFE , REPLAY_SESSION_KEY , VISIBILITY_CHANGE_TIMEOUT } from '../../src/session/constants' ;
13+ import { RecordingEvent } from '../../src/types' ;
1214import { useFakeTimers } from '../utils/use-fake-timers' ;
1315
1416useFakeTimers ( ) ;
@@ -18,6 +20,77 @@ async function advanceTimers(time: number) {
1820 await new Promise ( process . nextTick ) ;
1921}
2022
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+
2194describe ( 'Replay' , ( ) => {
2295 let replay : Replay ;
2396 let mockRecord : RecordMock ;
@@ -38,7 +111,7 @@ describe('Replay', () => {
38111 ( { mockRecord, mockTransportSend, domHandler, replay, spyCaptureException } = await resetSdkMock ( {
39112 sessionSampleRate : 1.0 ,
40113 errorSampleRate : 0.0 ,
41- stickySession : true ,
114+ stickySession : false ,
42115 } ) ) ;
43116
44117 jest . spyOn ( replay , 'flush' ) ;
@@ -68,23 +141,6 @@ describe('Replay', () => {
68141 replay . stop ( ) ;
69142 } ) ;
70143
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-
88144 it ( 'should have a session after setup' , ( ) => {
89145 expect ( replay . session ) . toMatchObject ( {
90146 lastActivity : BASE_TIMESTAMP ,
0 commit comments