Skip to content

Commit d115c91

Browse files
✅ prepare tests for delayed View first update
1 parent cb2682f commit d115c91

2 files changed

Lines changed: 65 additions & 26 deletions

File tree

packages/rum-core/src/boot/startRum.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,13 @@ function startRumStub(
108108

109109
describe('rum session', () => {
110110
let serverRumEvents: RumEvent[]
111+
let clock: Clock
111112
let lifeCycle: LifeCycle
112113
let sessionManager: RumSessionManagerMock
113114

114115
beforeEach(() => {
115116
lifeCycle = new LifeCycle()
117+
clock = mockClock()
116118
sessionManager = createRumSessionManagerMock().setId('42')
117119
const domMutationObservable = new Observable<RumMutationRecord[]>()
118120
const windowOpenObservable = new Observable<void>()
@@ -135,6 +137,7 @@ describe('rum session', () => {
135137
})
136138

137139
it('when the session is renewed, a new view event should be sent', () => {
140+
clock.tick(0)
138141
expect(serverRumEvents.length).toEqual(1)
139142
expect(serverRumEvents[0].type).toEqual('view')
140143
expect(serverRumEvents[0].session.id).toEqual('42')
@@ -144,6 +147,7 @@ describe('rum session', () => {
144147

145148
sessionManager.setId('43')
146149
lifeCycle.notify(LifeCycleEventType.SESSION_RENEWED)
150+
clock.tick(0)
147151

148152
expect(serverRumEvents.length).toEqual(3)
149153

@@ -223,7 +227,6 @@ describe('rum events url', () => {
223227

224228
let changeLocation: (to: string) => void
225229
let lifeCycle: LifeCycle
226-
let clock: Clock
227230
let serverRumEvents: RumEvent[]
228231
let stop: () => void
229232

@@ -259,18 +262,20 @@ describe('rum events url', () => {
259262
})
260263

261264
it('should keep the same URL when updating a view ended by a URL change', () => {
265+
const clock = mockClock()
262266
setupViewUrlTest()
263267
serverRumEvents.length = 0
264268

265269
changeLocation('/bar')
270+
clock.tick(0)
266271

267272
expect(serverRumEvents.length).toEqual(2)
268273
expect(serverRumEvents[0].view.url).toEqual('http://foo.com/')
269274
expect(serverRumEvents[1].view.url).toEqual('http://foo.com/bar')
270275
})
271276

272277
it('should attach the url corresponding to the start of the event', () => {
273-
clock = mockClock()
278+
const clock = mockClock()
274279
const { notifyPerformanceEntries } = mockPerformanceObserver()
275280

276281
setupViewUrlTest()
@@ -297,13 +302,14 @@ describe('rum events url', () => {
297302
})
298303

299304
it('should keep the same URL when updating an ended view', () => {
300-
clock = mockClock()
305+
const clock = mockClock()
301306
const { triggerOnLoad } = mockDocumentReadyState()
302307
setupViewUrlTest()
303308

304309
clock.tick(VIEW_DURATION)
305310

306311
changeLocation('/bar')
312+
clock.tick(0)
307313

308314
serverRumEvents.length = 0
309315

packages/rum-core/src/domain/view/trackViews.spec.ts

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ describe('view lifecycle', () => {
121121
it('should send a final view update', () => {
122122
const { lifeCycle, getViewUpdateCount, getViewUpdate } = viewTest
123123

124+
clock.tick(0) // wait for the first view update to be sent
125+
124126
expect(getViewUpdateCount()).toBe(1)
125127

126128
lifeCycle.notify(LifeCycleEventType.SESSION_EXPIRED)
@@ -148,12 +150,12 @@ describe('view lifecycle', () => {
148150
lifeCycle.notify(LifeCycleEventType.SESSION_EXPIRED)
149151

150152
expect(getViewEndCount()).toBe(1)
151-
expect(getViewUpdateCount()).toBe(2)
153+
expect(getViewUpdateCount()).toBe(1)
152154

153155
lifeCycle.notify(LifeCycleEventType.SESSION_EXPIRED)
154156

155157
expect(getViewEndCount()).toBe(1)
156-
expect(getViewUpdateCount()).toBe(2)
158+
expect(getViewUpdateCount()).toBe(1)
157159
})
158160
})
159161

@@ -291,11 +293,11 @@ describe('view lifecycle', () => {
291293
it('should trigger a view update on unloading', () => {
292294
const { lifeCycle, getViewUpdateCount } = viewTest
293295

294-
expect(getViewUpdateCount()).toEqual(1)
296+
expect(getViewUpdateCount()).toEqual(0)
295297

296298
lifeCycle.notify(LifeCycleEventType.PAGE_MAY_EXIT, { reason: PageExitReason.UNLOADING })
297299

298-
expect(getViewUpdateCount()).toEqual(2)
300+
expect(getViewUpdateCount()).toEqual(1)
299301
})
300302

301303
it('should not create a new view when ending the view on unloading', () => {
@@ -340,18 +342,22 @@ describe('view lifecycle', () => {
340342

341343
describe('view loading type', () => {
342344
it('should collect initial view type as "initial_load"', () => {
345+
const clock = mockClock()
343346
const { getViewUpdate } = setupViewTest()
347+
clock.tick(0)
344348

345349
expect(getViewUpdate(0).loadingType).toEqual(ViewLoadingType.INITIAL_LOAD)
346350
})
347351

348352
it('should collect view type as "route_change" after a view change', () => {
353+
const clock = mockClock()
349354
const { getViewUpdate, startView } = setupViewTest()
350355

351356
startView()
357+
clock.tick(0)
352358

353-
expect(getViewUpdate(1).loadingType).toEqual(ViewLoadingType.INITIAL_LOAD)
354-
expect(getViewUpdate(2).loadingType).toEqual(ViewLoadingType.ROUTE_CHANGE)
359+
expect(getViewUpdate(0).loadingType).toEqual(ViewLoadingType.INITIAL_LOAD)
360+
expect(getViewUpdate(1).loadingType).toEqual(ViewLoadingType.ROUTE_CHANGE)
355361
})
356362
})
357363

@@ -370,6 +376,7 @@ describe('view metrics', () => {
370376
pending('CLS web vital not supported')
371377
}
372378
const { getViewUpdateCount, getViewUpdate } = setupViewTest()
379+
clock.tick(0)
373380

374381
expect(getViewUpdateCount()).toEqual(1)
375382
expect(getViewUpdate(0).initialViewMetrics).toEqual({})
@@ -413,8 +420,7 @@ describe('view metrics', () => {
413420
describe('initial view metrics', () => {
414421
it('updates should be throttled', () => {
415422
const { getViewUpdateCount, getViewUpdate } = setupViewTest()
416-
expect(getViewUpdateCount()).toEqual(1)
417-
expect(getViewUpdate(0).initialViewMetrics).toEqual({})
423+
expect(getViewUpdateCount()).toEqual(0)
418424

419425
clock.tick(THROTTLE_VIEW_UPDATE_PERIOD - 1)
420426

@@ -471,6 +477,7 @@ describe('view metrics', () => {
471477

472478
beforeEach(() => {
473479
const { getViewUpdateCount, getViewUpdate, startView } = setupViewTest()
480+
clock.tick(0)
474481

475482
expect(getViewUpdateCount()).toEqual(1)
476483

@@ -481,6 +488,7 @@ describe('view metrics', () => {
481488
viewDuration = relativeNow()
482489

483490
startView()
491+
clock.tick(0)
484492

485493
expect(getViewUpdateCount()).toEqual(3)
486494

@@ -533,18 +541,22 @@ describe('view metrics', () => {
533541

534542
describe('view is active', () => {
535543
it('should set initial view as active', () => {
544+
const clock = mockClock()
536545
const { getViewUpdate } = setupViewTest()
546+
clock.tick(0)
537547

538548
expect(getViewUpdate(0).isActive).toBe(true)
539549
})
540550

541551
it('should set old view as inactive and new one as active after a route change', () => {
552+
const clock = mockClock()
542553
const { getViewUpdate, startView } = setupViewTest()
543554

544555
startView()
556+
clock.tick(0)
545557

546-
expect(getViewUpdate(1).isActive).toBe(false)
547-
expect(getViewUpdate(2).isActive).toBe(true)
558+
expect(getViewUpdate(0).isActive).toBe(false)
559+
expect(getViewUpdate(1).isActive).toBe(true)
548560
})
549561
})
550562

@@ -562,6 +574,7 @@ describe('view custom timings', () => {
562574
const { getViewUpdate, startView, addTiming } = viewTest
563575

564576
startView()
577+
clock.tick(0) // wait for the first view update to be sent
565578

566579
const currentViewId = getViewUpdate(2).id
567580
clock.tick(20)
@@ -688,12 +701,14 @@ describe('start view', () => {
688701

689702
it('should start a new view', () => {
690703
const { getViewUpdateCount, getViewUpdate, startView } = setupViewTest()
704+
clock.tick(0)
691705

692706
expect(getViewUpdateCount()).toBe(1)
693707
const initialViewId = getViewUpdate(0).id
694708

695709
clock.tick(10)
696710
startView()
711+
clock.tick(0)
697712

698713
expect(getViewUpdateCount()).toBe(3)
699714

@@ -713,10 +728,11 @@ describe('start view', () => {
713728
startView()
714729
startView({ name: 'foo' })
715730
startView({ name: 'bar' })
731+
clock.tick(0)
716732

717-
expect(getViewUpdate(2).name).toBeUndefined()
718-
expect(getViewUpdate(4).name).toBe('foo')
719-
expect(getViewUpdate(6).name).toBe('bar')
733+
expect(getViewUpdate(1).name).toBeUndefined()
734+
expect(getViewUpdate(2).name).toBe('foo')
735+
expect(getViewUpdate(3).name).toBe('bar')
720736
})
721737

722738
it('should have service and version', () => {
@@ -725,20 +741,21 @@ describe('start view', () => {
725741
startView()
726742
startView({ service: 'service 1', version: 'version 1' })
727743
startView({ service: 'service 2', version: 'version 2' })
744+
clock.tick(0)
728745

729-
expect(getViewUpdate(2)).toEqual(
746+
expect(getViewUpdate(1)).toEqual(
730747
jasmine.objectContaining({
731748
service: undefined,
732749
version: undefined,
733750
})
734751
)
735-
expect(getViewUpdate(4)).toEqual(
752+
expect(getViewUpdate(2)).toEqual(
736753
jasmine.objectContaining({
737754
service: 'service 1',
738755
version: 'version 1',
739756
})
740757
)
741-
expect(getViewUpdate(6)).toEqual(
758+
expect(getViewUpdate(3)).toEqual(
742759
jasmine.objectContaining({
743760
service: 'service 2',
744761
version: 'version 2',
@@ -750,7 +767,9 @@ describe('start view', () => {
750767
const { getViewUpdate, startView } = setupViewTest()
751768

752769
startView({ service: null, version: null })
753-
expect(getViewUpdate(2)).toEqual(
770+
clock.tick(0)
771+
772+
expect(getViewUpdate(1)).toEqual(
754773
jasmine.objectContaining({
755774
service: undefined,
756775
version: undefined,
@@ -763,6 +782,7 @@ describe('start view', () => {
763782

764783
clock.tick(100)
765784
startView({ name: 'foo' }, relativeToClocks(50 as RelativeTime))
785+
clock.tick(0)
766786

767787
expect(getViewUpdate(1).duration).toBe(50 as Duration)
768788
expect(getViewUpdate(2).startClocks.relative).toBe(50 as RelativeTime)
@@ -797,7 +817,7 @@ describe('view event count', () => {
797817

798818
changeLocation('/bar')
799819

800-
expect(getViewUpdate(getViewUpdateCount() - 2).eventCounts.actionCount).toBe(1)
820+
expect(getViewUpdate(getViewUpdateCount() - 1).eventCounts.actionCount).toBe(1)
801821
})
802822

803823
it('should be updated for 5 min after view end', () => {
@@ -852,17 +872,20 @@ describe('view event count', () => {
852872
const { getViewUpdate, startView } = setupViewTest()
853873

854874
startView({ context: { foo: 'bar' } })
855-
expect(getViewUpdate(2).context).toEqual({ foo: 'bar' })
875+
clock.tick(0)
876+
expect(getViewUpdate(1).context).toEqual({ foo: 'bar' })
856877
})
857878

858879
it('should replace current context set on view event', () => {
859880
const { getViewUpdate, startView } = setupViewTest()
860881

861882
startView({ context: { foo: 'bar' } })
862-
expect(getViewUpdate(2).context).toEqual({ foo: 'bar' })
883+
clock.tick(0)
884+
expect(getViewUpdate(1).context).toEqual({ foo: 'bar' })
863885

864886
startView({ context: { bar: 'baz' } })
865-
expect(getViewUpdate(4).context).toEqual({ bar: 'baz' })
887+
clock.tick(0)
888+
expect(getViewUpdate(3).context).toEqual({ bar: 'baz' })
866889
})
867890

868891
it('should set view context with setViewContext', () => {
@@ -897,35 +920,43 @@ describe('view event count', () => {
897920

898921
startView()
899922
setViewName('foo')
900-
expect(getViewUpdate(3).name).toEqual('foo')
923+
clock.tick(0)
924+
925+
expect(getViewUpdate(1).name).toEqual('foo')
901926
})
902927

903928
it('should set a defined view name', () => {
904929
const { getViewUpdate, startView, setViewName } = setupViewTest()
905930

906931
startView({ name: 'initial view name' })
907932
setViewName('foo')
908-
expect(getViewUpdate(3).name).toEqual('foo')
933+
clock.tick(0)
934+
935+
expect(getViewUpdate(1).name).toEqual('foo')
909936
})
910937
})
911938
})
912939

913940
describe('service and version', () => {
914941
it('should come from the init configuration by default', () => {
942+
const clock = mockClock()
915943
const { getViewUpdate } = setupViewTest({ partialConfig: { service: 'service', version: 'version' } })
944+
clock.tick(0)
916945

917946
expect(getViewUpdate(0).service).toEqual('service')
918947
expect(getViewUpdate(0).version).toEqual('version')
919948
})
920949

921950
it('should come from the view option if defined', () => {
951+
const clock = mockClock()
922952
const { getViewUpdate } = setupViewTest({
923953
partialConfig: { service: 'service', version: 'version' },
924954
initialViewOptions: {
925955
service: 'view service',
926956
version: 'view version',
927957
},
928958
})
959+
clock.tick(0)
929960

930961
expect(getViewUpdate(0).service).toEqual('view service')
931962
expect(getViewUpdate(0).version).toEqual('view version')
@@ -934,6 +965,7 @@ describe('service and version', () => {
934965

935966
describe('BFCache views', () => {
936967
it('should create a new "bf_cache" view when restoring from the BFCache', () => {
968+
const clock = mockClock()
937969
const { getViewCreateCount, getViewEndCount, getViewUpdate, getViewUpdateCount } = setupViewTest({
938970
partialConfig: { trackBfcacheViews: true },
939971
})
@@ -944,6 +976,7 @@ describe('BFCache views', () => {
944976
const event = createNewEvent('pageshow', { persisted: true })
945977

946978
window.dispatchEvent(event)
979+
clock.tick(0)
947980

948981
expect(getViewEndCount()).toBe(1)
949982
expect(getViewCreateCount()).toBe(2)

0 commit comments

Comments
 (0)