Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
48 changes: 48 additions & 0 deletions packages/rum-core/src/domain/contexts/urlContexts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,54 @@ describe('urlContexts', () => {
expect(urlContext.referrer).toBe(document.referrer)
})

it('should use the provided url override instead of location', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
url: 'https://example.com/overridden-path',
} as ViewCreatedEvent)

const urlContext = urlContexts.findUrl()!
expect(urlContext.url).toBe('https://example.com/overridden-path')
expect(urlContext.referrer).toBe(document.referrer)
})

it('should fall back to location.href when no url override is provided', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)

const urlContext = urlContexts.findUrl()!
expect(urlContext.url).toBe('http://fake-url.com/')
})

it('should fall back to location.href when url override is explicitly undefined', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
url: undefined,
} as ViewCreatedEvent)

const urlContext = urlContexts.findUrl()!
expect(urlContext.url).toBe('http://fake-url.com/')
})

it('should use the provided url override for events starting before a location change', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: clocksNow(),
url: 'https://example.com/manual-url',
} as ViewCreatedEvent)

clock.tick(10)
const resourceStartTime = clock.relative(10)

clock.tick(10)
changeLocation('/new-path')

expect(urlContexts.findUrl(resourceStartTime)).toEqual({
url: 'https://example.com/manual-url',
referrer: document.referrer,
})
})

it('should update url context on location change', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
Expand Down
4 changes: 2 additions & 2 deletions packages/rum-core/src/domain/contexts/urlContexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export function startUrlContexts(

let previousViewUrl: string | undefined

lifeCycle.subscribe(LifeCycleEventType.BEFORE_VIEW_CREATED, ({ startClocks }) => {
const viewUrl = mockable(location).href
lifeCycle.subscribe(LifeCycleEventType.BEFORE_VIEW_CREATED, ({ startClocks, url }) => {
const viewUrl = url ?? mockable(location).href
urlContextHistory.add(
buildUrlContext({
url: viewUrl,
Expand Down
3 changes: 3 additions & 0 deletions packages/rum-core/src/domain/view/trackViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export interface ViewCreatedEvent {
version?: string
context?: Context
startClocks: ClocksState
url?: string
}

export interface BeforeViewUpdateEvent {
Expand Down Expand Up @@ -102,6 +103,7 @@ export interface ViewOptions {
version?: RumInitConfiguration['version']
context?: Context
handlingStack?: string
url?: string
}

export function trackViews(
Expand Down Expand Up @@ -241,6 +243,7 @@ function newView(
service,
version,
context,
url: viewOptions?.url,
}
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, viewCreatedEvent)
lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, viewCreatedEvent)
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/scenario/rum/init.scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,29 @@ test.describe('API calls and events around init', () => {
)
})

createTest('should use the provided url option instead of location')
.withRum()
.withRumSlim()
.withRumInit((configuration) => {
window.DD_RUM!.init(configuration)

setTimeout(
() =>
window.DD_RUM!.startView({
name: 'manual view',
url: 'https://example.com/overridden-path',
}),
10
)
})
.run(async ({ intakeRegistry, flushEvents }) => {
await flushEvents()

const manualView = intakeRegistry.rumViewEvents.find((event) => event.view.name === 'manual view')!
expect(manualView).toBeTruthy()
expect(manualView.view.url).toBe('https://example.com/overridden-path')
})

createTest('should be able to set view context')
.withRum()
.withRumSlim()
Expand Down