Skip to content

Commit a6cf1b4

Browse files
✅ adjust tests impacted by the delayed View first update
1 parent b36ed90 commit a6cf1b4

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
@@ -130,6 +130,8 @@ describe('view lifecycle', () => {
130130
it('should send a final view update', () => {
131131
const { lifeCycle, getViewUpdateCount, getViewUpdate } = viewTest
132132

133+
clock.tick(0) // wait for the first view update to be sent
134+
133135
expect(getViewUpdateCount()).toBe(1)
134136

135137
lifeCycle.notify(LifeCycleEventType.SESSION_EXPIRED)
@@ -157,12 +159,12 @@ describe('view lifecycle', () => {
157159
lifeCycle.notify(LifeCycleEventType.SESSION_EXPIRED)
158160

159161
expect(getViewEndCount()).toBe(1)
160-
expect(getViewUpdateCount()).toBe(2)
162+
expect(getViewUpdateCount()).toBe(1)
161163

162164
lifeCycle.notify(LifeCycleEventType.SESSION_EXPIRED)
163165

164166
expect(getViewEndCount()).toBe(1)
165-
expect(getViewUpdateCount()).toBe(2)
167+
expect(getViewUpdateCount()).toBe(1)
166168
})
167169
})
168170

@@ -300,11 +302,11 @@ describe('view lifecycle', () => {
300302
it('should trigger a view update on unloading', () => {
301303
const { lifeCycle, getViewUpdateCount } = viewTest
302304

303-
expect(getViewUpdateCount()).toEqual(1)
305+
expect(getViewUpdateCount()).toEqual(0)
304306

305307
lifeCycle.notify(LifeCycleEventType.PAGE_MAY_EXIT, { reason: PageExitReason.UNLOADING })
306308

307-
expect(getViewUpdateCount()).toEqual(2)
309+
expect(getViewUpdateCount()).toEqual(1)
308310
})
309311

310312
it('should not create a new view when ending the view on unloading', () => {
@@ -349,18 +351,22 @@ describe('view lifecycle', () => {
349351

350352
describe('view loading type', () => {
351353
it('should collect initial view type as "initial_load"', () => {
354+
const clock = mockClock()
352355
const { getViewUpdate } = setupViewTest()
356+
clock.tick(0)
353357

354358
expect(getViewUpdate(0).loadingType).toEqual(ViewLoadingType.INITIAL_LOAD)
355359
})
356360

357361
it('should collect view type as "route_change" after a view change', () => {
362+
const clock = mockClock()
358363
const { getViewUpdate, startView } = setupViewTest()
359364

360365
startView()
366+
clock.tick(0)
361367

362-
expect(getViewUpdate(1).loadingType).toEqual(ViewLoadingType.INITIAL_LOAD)
363-
expect(getViewUpdate(2).loadingType).toEqual(ViewLoadingType.ROUTE_CHANGE)
368+
expect(getViewUpdate(0).loadingType).toEqual(ViewLoadingType.INITIAL_LOAD)
369+
expect(getViewUpdate(1).loadingType).toEqual(ViewLoadingType.ROUTE_CHANGE)
364370
})
365371
})
366372

@@ -379,6 +385,7 @@ describe('view metrics', () => {
379385
pending('CLS web vital not supported')
380386
}
381387
const { getViewUpdateCount, getViewUpdate } = setupViewTest()
388+
clock.tick(0)
382389

383390
expect(getViewUpdateCount()).toEqual(1)
384391
expect(getViewUpdate(0).initialViewMetrics).toEqual({})
@@ -422,8 +429,7 @@ describe('view metrics', () => {
422429
describe('initial view metrics', () => {
423430
it('updates should be throttled', () => {
424431
const { getViewUpdateCount, getViewUpdate } = setupViewTest()
425-
expect(getViewUpdateCount()).toEqual(1)
426-
expect(getViewUpdate(0).initialViewMetrics).toEqual({})
432+
expect(getViewUpdateCount()).toEqual(0)
427433

428434
clock.tick(THROTTLE_VIEW_UPDATE_PERIOD - 1)
429435

@@ -480,6 +486,7 @@ describe('view metrics', () => {
480486

481487
beforeEach(() => {
482488
const { getViewUpdateCount, getViewUpdate, startView } = setupViewTest()
489+
clock.tick(0)
483490

484491
expect(getViewUpdateCount()).toEqual(1)
485492

@@ -490,6 +497,7 @@ describe('view metrics', () => {
490497
viewDuration = relativeNow()
491498

492499
startView()
500+
clock.tick(0)
493501

494502
expect(getViewUpdateCount()).toEqual(3)
495503

@@ -542,18 +550,22 @@ describe('view metrics', () => {
542550

543551
describe('view is active', () => {
544552
it('should set initial view as active', () => {
553+
const clock = mockClock()
545554
const { getViewUpdate } = setupViewTest()
555+
clock.tick(0)
546556

547557
expect(getViewUpdate(0).isActive).toBe(true)
548558
})
549559

550560
it('should set old view as inactive and new one as active after a route change', () => {
561+
const clock = mockClock()
551562
const { getViewUpdate, startView } = setupViewTest()
552563

553564
startView()
565+
clock.tick(0)
554566

555-
expect(getViewUpdate(1).isActive).toBe(false)
556-
expect(getViewUpdate(2).isActive).toBe(true)
567+
expect(getViewUpdate(0).isActive).toBe(false)
568+
expect(getViewUpdate(1).isActive).toBe(true)
557569
})
558570
})
559571

@@ -571,6 +583,7 @@ describe('view custom timings', () => {
571583
const { getViewUpdate, startView, addTiming } = viewTest
572584

573585
startView()
586+
clock.tick(0) // wait for the first view update to be sent
574587

575588
const currentViewId = getViewUpdate(2).id
576589
clock.tick(20)
@@ -697,12 +710,14 @@ describe('start view', () => {
697710

698711
it('should start a new view', () => {
699712
const { getViewUpdateCount, getViewUpdate, startView } = setupViewTest()
713+
clock.tick(0)
700714

701715
expect(getViewUpdateCount()).toBe(1)
702716
const initialViewId = getViewUpdate(0).id
703717

704718
clock.tick(10)
705719
startView()
720+
clock.tick(0)
706721

707722
expect(getViewUpdateCount()).toBe(3)
708723

@@ -722,10 +737,11 @@ describe('start view', () => {
722737
startView()
723738
startView({ name: 'foo' })
724739
startView({ name: 'bar' })
740+
clock.tick(0)
725741

726-
expect(getViewUpdate(2).name).toBeUndefined()
727-
expect(getViewUpdate(4).name).toBe('foo')
728-
expect(getViewUpdate(6).name).toBe('bar')
742+
expect(getViewUpdate(1).name).toBeUndefined()
743+
expect(getViewUpdate(2).name).toBe('foo')
744+
expect(getViewUpdate(3).name).toBe('bar')
729745
})
730746

731747
it('should have service and version', () => {
@@ -734,20 +750,21 @@ describe('start view', () => {
734750
startView()
735751
startView({ service: 'service 1', version: 'version 1' })
736752
startView({ service: 'service 2', version: 'version 2' })
753+
clock.tick(0)
737754

738-
expect(getViewUpdate(2)).toEqual(
755+
expect(getViewUpdate(1)).toEqual(
739756
jasmine.objectContaining({
740757
service: undefined,
741758
version: undefined,
742759
})
743760
)
744-
expect(getViewUpdate(4)).toEqual(
761+
expect(getViewUpdate(2)).toEqual(
745762
jasmine.objectContaining({
746763
service: 'service 1',
747764
version: 'version 1',
748765
})
749766
)
750-
expect(getViewUpdate(6)).toEqual(
767+
expect(getViewUpdate(3)).toEqual(
751768
jasmine.objectContaining({
752769
service: 'service 2',
753770
version: 'version 2',
@@ -759,7 +776,9 @@ describe('start view', () => {
759776
const { getViewUpdate, startView } = setupViewTest()
760777

761778
startView({ service: null, version: null })
762-
expect(getViewUpdate(2)).toEqual(
779+
clock.tick(0)
780+
781+
expect(getViewUpdate(1)).toEqual(
763782
jasmine.objectContaining({
764783
service: undefined,
765784
version: undefined,
@@ -772,6 +791,7 @@ describe('start view', () => {
772791

773792
clock.tick(100)
774793
startView({ name: 'foo' }, relativeToClocks(50 as RelativeTime))
794+
clock.tick(0)
775795

776796
expect(getViewUpdate(1).duration).toBe(50 as Duration)
777797
expect(getViewUpdate(2).startClocks.relative).toBe(50 as RelativeTime)
@@ -806,7 +826,7 @@ describe('view event count', () => {
806826

807827
changeLocation('/bar')
808828

809-
expect(getViewUpdate(getViewUpdateCount() - 2).eventCounts.actionCount).toBe(1)
829+
expect(getViewUpdate(getViewUpdateCount() - 1).eventCounts.actionCount).toBe(1)
810830
})
811831

812832
it('should be updated for 5 min after view end', () => {
@@ -861,17 +881,20 @@ describe('view event count', () => {
861881
const { getViewUpdate, startView } = setupViewTest()
862882

863883
startView({ context: { foo: 'bar' } })
864-
expect(getViewUpdate(2).context).toEqual({ foo: 'bar' })
884+
clock.tick(0)
885+
expect(getViewUpdate(1).context).toEqual({ foo: 'bar' })
865886
})
866887

867888
it('should replace current context set on view event', () => {
868889
const { getViewUpdate, startView } = setupViewTest()
869890

870891
startView({ context: { foo: 'bar' } })
871-
expect(getViewUpdate(2).context).toEqual({ foo: 'bar' })
892+
clock.tick(0)
893+
expect(getViewUpdate(1).context).toEqual({ foo: 'bar' })
872894

873895
startView({ context: { bar: 'baz' } })
874-
expect(getViewUpdate(4).context).toEqual({ bar: 'baz' })
896+
clock.tick(0)
897+
expect(getViewUpdate(3).context).toEqual({ bar: 'baz' })
875898
})
876899

877900
it('should set view context with setViewContext', () => {
@@ -906,35 +929,43 @@ describe('view event count', () => {
906929

907930
startView()
908931
setViewName('foo')
909-
expect(getViewUpdate(3).name).toEqual('foo')
932+
clock.tick(0)
933+
934+
expect(getViewUpdate(1).name).toEqual('foo')
910935
})
911936

912937
it('should set a defined view name', () => {
913938
const { getViewUpdate, startView, setViewName } = setupViewTest()
914939

915940
startView({ name: 'initial view name' })
916941
setViewName('foo')
917-
expect(getViewUpdate(3).name).toEqual('foo')
942+
clock.tick(0)
943+
944+
expect(getViewUpdate(1).name).toEqual('foo')
918945
})
919946
})
920947
})
921948

922949
describe('service and version', () => {
923950
it('should come from the init configuration by default', () => {
951+
const clock = mockClock()
924952
const { getViewUpdate } = setupViewTest({ partialConfig: { service: 'service', version: 'version' } })
953+
clock.tick(0)
925954

926955
expect(getViewUpdate(0).service).toEqual('service')
927956
expect(getViewUpdate(0).version).toEqual('version')
928957
})
929958

930959
it('should come from the view option if defined', () => {
960+
const clock = mockClock()
931961
const { getViewUpdate } = setupViewTest({
932962
partialConfig: { service: 'service', version: 'version' },
933963
initialViewOptions: {
934964
service: 'view service',
935965
version: 'view version',
936966
},
937967
})
968+
clock.tick(0)
938969

939970
expect(getViewUpdate(0).service).toEqual('view service')
940971
expect(getViewUpdate(0).version).toEqual('view version')
@@ -943,6 +974,7 @@ describe('service and version', () => {
943974

944975
describe('BFCache views', () => {
945976
it('should create a new "bf_cache" view when restoring from the BFCache', () => {
977+
const clock = mockClock()
946978
const { getViewCreateCount, getViewEndCount, getViewUpdate, getViewUpdateCount } = setupViewTest({
947979
partialConfig: { trackBfcacheViews: true },
948980
})
@@ -953,6 +985,7 @@ describe('BFCache views', () => {
953985
const event = createNewEvent('pageshow', { persisted: true })
954986

955987
window.dispatchEvent(event)
988+
clock.tick(0)
956989

957990
expect(getViewEndCount()).toBe(1)
958991
expect(getViewCreateCount()).toBe(2)

0 commit comments

Comments
 (0)