@@ -23,12 +23,14 @@ describe('record', () => {
2323 let emitSpy : jasmine . Spy < ( record : RawRecord ) => void >
2424 let waitEmitCalls : ( expectedCallsCount : number , callback : ( ) => void ) => void
2525 let expectNoExtraEmitCalls : ( done : ( ) => void ) => void
26+ let useNewMutationObserver : boolean
2627
2728 beforeEach ( ( ) => {
2829 if ( isIE ( ) ) {
2930 pending ( 'IE not supported' )
3031 }
3132
33+ useNewMutationObserver = false
3234 emitSpy = jasmine . createSpy ( )
3335 ; ( { waitAsyncCalls : waitEmitCalls , expectNoExtraAsyncCall : expectNoExtraEmitCalls } = collectAsyncCalls ( emitSpy ) )
3436 ; ( { sandbox, input } = createDOMSandbox ( ) )
@@ -52,7 +54,7 @@ describe('record', () => {
5254 expect ( records . filter ( ( record ) => record . type === RecordType . FullSnapshot ) . length ) . toEqual ( 1 )
5355 } )
5456
55- it ( 'is safe to checkout during async callbacks' , ( done ) => {
57+ it ( 'is safe to checkout during async callbacks (old mutation observer) ' , ( done ) => {
5658 startRecording ( )
5759
5860 const p = document . createElement ( 'p' )
@@ -123,6 +125,72 @@ describe('record', () => {
123125 } )
124126 } )
125127
128+ it ( 'is safe to checkout during async callbacks (new mutation observer)' , ( done ) => {
129+ useNewMutationObserver = true
130+ startRecording ( )
131+
132+ const p = document . createElement ( 'p' )
133+ const span = document . createElement ( 'span' )
134+
135+ setTimeout ( ( ) => {
136+ sandbox . appendChild ( p )
137+ p . appendChild ( span )
138+ sandbox . removeChild ( document . querySelector ( 'input' ) ! )
139+ } , 0 )
140+
141+ setTimeout ( ( ) => {
142+ span . innerText = 'test'
143+ recordApi . takeFullSnapshot ( )
144+ } , 10 )
145+
146+ setTimeout ( ( ) => {
147+ p . removeChild ( span )
148+ sandbox . appendChild ( span )
149+ } , 10 )
150+
151+ waitEmitCalls ( 9 , ( ) => {
152+ const records = getEmittedRecords ( )
153+ expect ( records [ 0 ] . type ) . toBe ( RecordType . Meta )
154+ expect ( records [ 1 ] . type ) . toBe ( RecordType . Focus )
155+
156+ expect ( records [ 2 ] . type ) . toBe ( RecordType . FullSnapshot )
157+
158+ expect ( records [ 3 ] . type ) . toBe ( RecordType . IncrementalSnapshot )
159+
160+ const { validate : validateMutationPayload , expectNewNode, expectInitialNode } = createMutationPayloadValidator (
161+ ( records [ 2 ] as FullSnapshotRecord ) . data . node
162+ )
163+
164+ const p = expectNewNode ( { type : NodeType . Element , tagName : 'p' } )
165+ const span = expectNewNode ( { type : NodeType . Element , tagName : 'span' } )
166+ const text = expectNewNode ( { type : NodeType . Text , textContent : 'test' } )
167+ const sandbox = expectInitialNode ( { idAttribute : 'sandbox' } )
168+
169+ validateMutationPayload ( ( records [ 3 ] as IncrementalSnapshotRecord ) . data as MutationData , {
170+ adds : [ { parent : sandbox , node : p . withChildren ( span ) } ] ,
171+ removes : [ { node : expectInitialNode ( { tag : 'input' } ) , parent : sandbox } ] ,
172+ } )
173+
174+ expect ( records [ 4 ] . type ) . toBe ( RecordType . IncrementalSnapshot )
175+ validateMutationPayload ( ( records [ 4 ] as IncrementalSnapshotRecord ) . data as MutationData , {
176+ adds : [ { parent : span , node : text } ] ,
177+ } )
178+
179+ expect ( records [ 5 ] . type ) . toBe ( RecordType . Meta )
180+ expect ( records [ 6 ] . type ) . toBe ( RecordType . Focus )
181+
182+ expect ( records [ 7 ] . type ) . toBe ( RecordType . FullSnapshot )
183+
184+ expect ( records [ 8 ] . type ) . toBe ( RecordType . IncrementalSnapshot )
185+ validateMutationPayload ( ( records [ 8 ] as IncrementalSnapshotRecord ) . data as MutationData , {
186+ adds : [ { parent : sandbox , node : span . withChildren ( text ) } ] ,
187+ removes : [ { parent : p , node : span } ] ,
188+ } )
189+
190+ expectNoExtraEmitCalls ( done )
191+ } )
192+ } )
193+
126194 it ( 'captures stylesheet rules' , ( done ) => {
127195 startRecording ( )
128196
@@ -260,6 +328,7 @@ describe('record', () => {
260328 function startRecording ( ) {
261329 recordApi = record ( {
262330 emit : emitSpy ,
331+ useNewMutationObserver,
263332 } )
264333 }
265334
0 commit comments