Skip to content

Commit 0b06411

Browse files
✨ [RUM-10144] apply context defined just after init to View event generated during init (#3597)
1 parent 264463f commit 0b06411

7 files changed

Lines changed: 223 additions & 223 deletions

File tree

packages/browser-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ export * from './tools/utils/numberUtils'
179179
export * from './tools/utils/byteUtils'
180180
export * from './tools/utils/objectUtils'
181181
export * from './tools/utils/functionUtils'
182+
export * from './tools/queueMicrotask'
182183
export * from './tools/serialisation/jsonStringify'
183184
export * from './tools/serialisation/stringify'
184185
export * from './tools/utils/stringUtils'

packages/browser-core/test/emulate/mockClock.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { TimeStamp, RelativeTime } from '@datadog/js-core/time'
2+
import { queueMicrotask } from '../../src/tools/queueMicrotask'
23
import { registerCleanupTask } from '../registerCleanupTask'
4+
import { replaceMockable } from '../replaceMockable'
35

46
export type Clock = ReturnType<typeof mockClock>
57

@@ -15,6 +17,17 @@ export function mockClock() {
1517

1618
registerCleanupTask(() => jasmine.clock().uninstall())
1719

20+
const pendingMicroTasks: Array<() => void> = []
21+
replaceMockable(queueMicrotask, (callback) => {
22+
pendingMicroTasks.push(callback)
23+
})
24+
25+
function flushPendingMicroTasks() {
26+
while (pendingMicroTasks.length > 0) {
27+
pendingMicroTasks.shift()!()
28+
}
29+
}
30+
1831
return {
1932
/**
2033
* Returns a RelativeTime representing the time it was X milliseconds after the `mockClock()`
@@ -26,7 +39,11 @@ export function mockClock() {
2639
* invokation (the start of the test).
2740
*/
2841
timeStamp: (duration: number) => (timeStampStart + duration) as TimeStamp,
29-
tick: (ms: number) => jasmine.clock().tick(ms),
42+
tick: (ms: number) => {
43+
flushPendingMicroTasks()
44+
jasmine.clock().tick(ms)
45+
flushPendingMicroTasks()
46+
},
3047
setDate: (date: Date) => jasmine.clock().mockDate(date),
3148
}
3249
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,13 @@ describe('session expiration lifecycle', () => {
9292

9393
describe('rum session', () => {
9494
let serverRumEvents: RumEvent[]
95+
let clock: Clock
9596
let lifeCycle: LifeCycle
9697
let sessionManager: SessionManagerMock
9798

9899
beforeEach(() => {
99100
lifeCycle = new LifeCycle()
101+
clock = mockClock()
100102
sessionManager = createSessionManagerMock().setId('42')
101103

102104
serverRumEvents = collectServerEvents(lifeCycle)
@@ -106,22 +108,24 @@ describe('rum session', () => {
106108
})
107109

108110
it('when the session is renewed, a new view event should be sent', () => {
109-
expect(serverRumEvents.length).toEqual(1)
110-
expect(serverRumEvents[0].type).toEqual('view')
111-
expect(serverRumEvents[0].session.id).toEqual('42')
111+
const getViewEvents = () =>
112+
serverRumEvents.filter((event): event is RumViewEvent => event.type === RumEventType.VIEW)
113+
114+
clock.tick(0)
115+
expect(getViewEvents().length).toEqual(1)
116+
expect(getViewEvents()[0].session.id).toEqual('42')
112117

113118
lifeCycle.notify(LifeCycleEventType.SESSION_EXPIRED, { endClocks: relativeToClocks(relativeNow()) })
114-
expect(serverRumEvents.length).toEqual(2)
119+
expect(getViewEvents().length).toEqual(2)
115120

116121
sessionManager.setId('43')
117122
lifeCycle.notify(LifeCycleEventType.SESSION_RENEWED)
123+
clock.tick(0)
118124

119-
expect(serverRumEvents.length).toEqual(3)
120-
121-
// New view event
122-
expect(serverRumEvents[2].type).toEqual('view')
123-
expect(serverRumEvents[2].session.id).toEqual('43')
124-
expect(serverRumEvents[2].view.id).not.toEqual(serverRumEvents[0].view.id)
125+
const viewEvents = getViewEvents()
126+
expect(viewEvents.length).toEqual(3)
127+
expect(viewEvents[2].session.id).toEqual('43')
128+
expect(viewEvents[2].view.id).not.toEqual(viewEvents[0].view.id)
125129
})
126130
})
127131

packages/browser-rum-core/src/domain/view/setupViewTest.specHelper.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
import { Observable } from '@datadog/browser-core'
22
import { deepClone } from '@datadog/js-core/util'
3+
import { registerCleanupTask } from '@datadog/browser-core/test'
34
import { mockRumConfiguration, setupLocationObserver } from '../../../test'
4-
import type { LifeCycle } from '../lifeCycle'
5-
import { LifeCycleEventType } from '../lifeCycle'
5+
import { LifeCycle, LifeCycleEventType } from '../lifeCycle'
66
import type { RumConfiguration } from '../configuration'
77
import type { RumMutationRecord } from '../../browser/domMutationObservable'
88
import type { ViewCreatedEvent, ViewEvent, ViewOptions, ViewEndedEvent } from './trackViews'
99
import { trackViews } from './trackViews'
1010

1111
export type ViewTest = ReturnType<typeof setupViewTest>
1212

13-
interface ViewTrackingContext {
14-
lifeCycle: LifeCycle
13+
interface ViewTestOptions {
1514
initialLocation?: string
1615
partialConfig?: Partial<RumConfiguration>
16+
initialViewOptions?: ViewOptions
1717
}
1818

19-
export function setupViewTest(
20-
{ lifeCycle, initialLocation, partialConfig }: ViewTrackingContext,
21-
initialViewOptions?: ViewOptions
22-
) {
19+
export function setupViewTest({ initialLocation, partialConfig, initialViewOptions }: ViewTestOptions = {}) {
20+
const lifeCycle = new LifeCycle()
2321
const domMutationObservable = new Observable<RumMutationRecord[]>()
2422
const windowOpenObservable = new Observable<void>()
2523
const configuration = mockRumConfiguration(partialConfig)
@@ -64,8 +62,11 @@ export function setupViewTest(
6462
!configuration.trackViewsManually,
6563
initialViewOptions
6664
)
65+
66+
registerCleanupTask(stop)
67+
6768
return {
68-
stop,
69+
lifeCycle,
6970
startView,
7071
setViewContext,
7172
setViewContextProperty,
@@ -80,9 +81,6 @@ export function setupViewTest(
8081
getViewCreateCount,
8182
getViewEnd,
8283
getViewEndCount,
83-
getLatestViewContext: () => ({
84-
id: getViewCreate(getViewCreateCount() - 1).id,
85-
}),
8684
}
8785
}
8886

0 commit comments

Comments
 (0)