@@ -2,51 +2,83 @@ import type { RumConfiguration, ViewCreatedEvent } from '@datadog/browser-rum-co
22import { LifeCycle , LifeCycleEventType } from '@datadog/browser-rum-core'
33import type { TimeStamp } from '@datadog/browser-core'
44import { isIE , noop } from '@datadog/browser-core'
5+ import { mockExperimentalFeatures , mockRequestIdleCallback } from '@datadog/browser-core/test'
56import type { BrowserRecord } from '../../types'
7+ import { ExperimentalFeature } from '../../../../core/src/tools/experimentalFeatures'
68import { startFullSnapshots } from './startFullSnapshots'
79import { createElementsScrollPositions } from './elementsScrollPositions'
810import type { ShadowRootsController } from './shadowRootsController'
911
1012describe ( 'startFullSnapshots' , ( ) => {
1113 const viewStartClock = { relative : 1 , timeStamp : 1 as TimeStamp }
1214 let lifeCycle : LifeCycle
13- let fullSnapshotCallback : jasmine . Spy < ( records : BrowserRecord [ ] ) => void >
15+ let fullSnapshotPendingCallback : jasmine . Spy < ( ) => void >
16+ let fullSnapshotReadyCallback : jasmine . Spy < ( records : BrowserRecord [ ] ) => void >
1417
1518 beforeEach ( ( ) => {
1619 if ( isIE ( ) ) {
1720 pending ( 'IE not supported' )
1821 }
1922
2023 lifeCycle = new LifeCycle ( )
21- fullSnapshotCallback = jasmine . createSpy ( )
24+ mockExperimentalFeatures ( [ ExperimentalFeature . ASYNC_FULL_SNAPSHOT ] )
25+ fullSnapshotPendingCallback = jasmine . createSpy ( 'fullSnapshotPendingCallback' )
26+ fullSnapshotReadyCallback = jasmine . createSpy ( 'fullSnapshotReadyCallback' )
27+
2228 startFullSnapshots (
2329 createElementsScrollPositions ( ) ,
2430 { } as ShadowRootsController ,
2531 lifeCycle ,
2632 { } as RumConfiguration ,
2733 noop ,
28- fullSnapshotCallback
34+ fullSnapshotPendingCallback ,
35+ fullSnapshotReadyCallback
2936 )
3037 } )
3138
3239 it ( 'takes a full snapshot when startFullSnapshots is called' , ( ) => {
33- expect ( fullSnapshotCallback ) . toHaveBeenCalledTimes ( 1 )
40+ expect ( fullSnapshotReadyCallback ) . toHaveBeenCalledTimes ( 1 )
3441 } )
3542
3643 it ( 'takes a full snapshot when the view changes' , ( ) => {
44+ const { triggerIdleCallbacks } = mockRequestIdleCallback ( )
45+
46+ lifeCycle . notify ( LifeCycleEventType . VIEW_CREATED , {
47+ startClocks : viewStartClock ,
48+ } as Partial < ViewCreatedEvent > as any )
49+
50+ triggerIdleCallbacks ( )
51+
52+ expect ( fullSnapshotPendingCallback ) . toHaveBeenCalledTimes ( 1 )
53+ expect ( fullSnapshotReadyCallback ) . toHaveBeenCalledTimes ( 2 )
54+ } )
55+
56+ it ( 'cancels the full snapshot if another view is created before it can it happens' , ( ) => {
57+ const { triggerIdleCallbacks } = mockRequestIdleCallback ( )
58+
59+ lifeCycle . notify ( LifeCycleEventType . VIEW_CREATED , {
60+ startClocks : viewStartClock ,
61+ } as Partial < ViewCreatedEvent > as any )
62+
3763 lifeCycle . notify ( LifeCycleEventType . VIEW_CREATED , {
3864 startClocks : viewStartClock ,
3965 } as Partial < ViewCreatedEvent > as any )
4066
41- expect ( fullSnapshotCallback ) . toHaveBeenCalledTimes ( 2 )
67+ triggerIdleCallbacks ( )
68+ expect ( fullSnapshotPendingCallback ) . toHaveBeenCalledTimes ( 2 )
69+ expect ( fullSnapshotReadyCallback ) . toHaveBeenCalledTimes ( 2 )
4270 } )
4371
4472 it ( 'full snapshot related records should have the view change date' , ( ) => {
73+ const { triggerIdleCallbacks } = mockRequestIdleCallback ( )
74+
4575 lifeCycle . notify ( LifeCycleEventType . VIEW_CREATED , {
4676 startClocks : viewStartClock ,
4777 } as Partial < ViewCreatedEvent > as any )
4878
49- const records = fullSnapshotCallback . calls . mostRecent ( ) . args [ 0 ]
79+ triggerIdleCallbacks ( )
80+
81+ const records = fullSnapshotReadyCallback . calls . mostRecent ( ) . args [ 0 ]
5082 expect ( records [ 0 ] . timestamp ) . toEqual ( 1 )
5183 expect ( records [ 1 ] . timestamp ) . toEqual ( 1 )
5284 expect ( records [ 2 ] . timestamp ) . toEqual ( 1 )
0 commit comments