Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix update view name api in context history #2853

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/rum-core/src/domain/contexts/viewContexts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { relativeToClocks, CLEAR_OLD_VALUES_INTERVAL } from '@datadog/browser-co
import type { TestSetupBuilder } from '../../../test'
import { setup } from '../../../test'
import { LifeCycleEventType } from '../lifeCycle'
import type { ViewCreatedEvent } from '../view/trackViews'
import type { ViewCreatedEvent, ViewEvent } from '../view/trackViews'
import type { ViewContexts } from './viewContexts'
import { startViewContexts, VIEW_CONTEXT_TIME_OUT_DELAY } from './viewContexts'

Expand Down Expand Up @@ -105,6 +105,16 @@ describe('viewContexts', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, buildViewCreatedEvent({ name: 'Fake name' }))
expect(viewContexts.findView()!.name).toBe('Fake name')
})

it('should update the view name for the current context', () => {
const { lifeCycle } = setupBuilder.build()
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, buildViewCreatedEvent({ name: 'foo' }))
lifeCycle.notify(LifeCycleEventType.VIEW_UPDATED, {
startClocks,
name: 'Fake Name',
} as ViewEvent)
expect(viewContexts.findView()!.name).toBe('Fake Name')
})
})

describe('history contexts', () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/rum-core/src/domain/contexts/viewContexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { RelativeTime, ClocksState } from '@datadog/browser-core'
import { SESSION_TIME_OUT_DELAY, ValueHistory } from '@datadog/browser-core'
import type { LifeCycle } from '../lifeCycle'
import { LifeCycleEventType } from '../lifeCycle'
import type { ViewCreatedEvent } from '../view/trackViews'
import type { ViewCreatedEvent, ViewEvent } from '../view/trackViews'

export const VIEW_CONTEXT_TIME_OUT_DELAY = SESSION_TIME_OUT_DELAY

Expand Down Expand Up @@ -30,6 +30,13 @@ export function startViewContexts(lifeCycle: LifeCycle): ViewContexts {
viewContextHistory.closeActive(endClocks.relative)
})

lifeCycle.subscribe(LifeCycleEventType.VIEW_UPDATED, (viewUpdate: ViewEvent) => {
const currentView = viewContextHistory.find(viewUpdate.startClocks.relative)
if (currentView && viewUpdate.name) {
currentView.name = viewUpdate.name
}
})

lifeCycle.subscribe(LifeCycleEventType.SESSION_RENEWED, () => {
viewContextHistory.reset()
})
Expand Down
10 changes: 8 additions & 2 deletions test/e2e/scenario/rum/init.scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('API calls and events around init', () => {
intakeRegistry,
{ name: 'before init', viewId: initialView.view.id },
{ name: 'before manual view', viewId: initialView.view.id },
{ name: 'after manual view', viewId: initialView.view.id }
{ name: 'after manual view', viewId: initialView.view.id, viewName: 'after manual view' }
)
})
})
Expand Down Expand Up @@ -173,12 +173,18 @@ function expectToHaveErrors(events: IntakeRegistry, ...errors: Array<{ message:
}
}

function expectToHaveActions(events: IntakeRegistry, ...actions: Array<{ name: string; viewId: string }>) {
function expectToHaveActions(
events: IntakeRegistry,
...actions: Array<{ name: string; viewId: string; viewName?: string }>
) {
expect(events.rumActionEvents.length).toBe(actions.length)
for (let i = 0; i < actions.length; i++) {
const registryAction = events.rumActionEvents[i]
const expectedAction = actions[i]
expect(registryAction.action.target!.name).toBe(expectedAction.name)
expect(registryAction.view.id).toBe(expectedAction.viewId)
if (i === 0 && expectedAction.viewName) {
expect(registryAction.view.name).toBe(expectedAction.viewName)
}
}
}