Skip to content

Commit

Permalink
🐛 avoid to execute view tests twice
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaudan committed Jun 4, 2021
1 parent d9547e1 commit 4fcff0d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context, RelativeTime, Duration } from '@datadog/browser-core'
import { LifeCycleEventType, RumEvent } from '@datadog/browser-rum-core'
import { TestSetupBuilder, setup } from '../../../../test/specHelper'
import { TestSetupBuilder, setup, setupViewTest, ViewTest } from '../../../../test/specHelper'
import { RumPerformanceNavigationTiming } from '../../../browser/performanceCollection'
import { RumEventType } from '../../../rawRumEvent.types'
import {
Expand All @@ -9,7 +9,6 @@ import {
PAGE_ACTIVITY_VALIDATION_DELAY,
} from '../../trackPageActivities'
import { THROTTLE_VIEW_UPDATE_PERIOD } from './trackViews'
import { setupViewTest, ViewTest } from './trackViews.spec'

const BEFORE_PAGE_ACTIVITY_VALIDATION_DELAY = (PAGE_ACTIVITY_VALIDATION_DELAY * 0.8) as Duration

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Duration, RelativeTime, timeStampNow, display, relativeToClocks } from '@datadog/browser-core'
import { setup, TestSetupBuilder, BuildContext } from '../../../../test/specHelper'
import { setup, TestSetupBuilder, setupViewTest, ViewTest } from '../../../../test/specHelper'
import {
RumLargestContentfulPaintTiming,
RumPerformanceNavigationTiming,
RumPerformancePaintTiming,
} from '../../../browser/performanceCollection'
import { ViewLoadingType } from '../../../rawRumEvent.types'
import { LifeCycleEventType } from '../../lifeCycle'
import { THROTTLE_VIEW_UPDATE_PERIOD, trackViews, ViewEvent } from './trackViews'
import { THROTTLE_VIEW_UPDATE_PERIOD, ViewEvent } from './trackViews'

const FAKE_PAINT_ENTRY: RumPerformancePaintTiming = {
entryType: 'paint',
Expand All @@ -26,52 +26,6 @@ const FAKE_NAVIGATION_ENTRY: RumPerformanceNavigationTiming = {
loadEventEnd: 567 as RelativeTime,
}

export type ViewTest = ReturnType<typeof setupViewTest>

export function setupViewTest(
{ lifeCycle, location, domMutationObservable, configuration }: BuildContext,
initialViewName?: string
) {
const { handler: viewUpdateHandler, getViewEvent: getViewUpdate, getHandledCount: getViewUpdateCount } = spyOnViews(
'view update'
)
lifeCycle.subscribe(LifeCycleEventType.VIEW_UPDATED, viewUpdateHandler)
const { handler: viewCreateHandler, getViewEvent: getViewCreate, getHandledCount: getViewCreateCount } = spyOnViews(
'view create'
)
lifeCycle.subscribe(LifeCycleEventType.VIEW_CREATED, viewCreateHandler)
const { stop, startView, addTiming } = trackViews(
location,
lifeCycle,
domMutationObservable,
!configuration.trackViewsManually,
initialViewName
)
return {
stop,
startView,
addTiming,
getViewUpdate,
getViewUpdateCount,
getViewCreate,
getViewCreateCount,
}
}

function spyOnViews(name?: string) {
const handler = jasmine.createSpy(name)

function getViewEvent(index: number) {
return handler.calls.argsFor(index)[0] as ViewEvent
}

function getHandledCount() {
return handler.calls.count()
}

return { handler, getViewEvent, getHandledCount }
}

describe('track views automatically', () => {
let setupBuilder: TestSetupBuilder
let viewTest: ViewTest
Expand Down
49 changes: 48 additions & 1 deletion packages/rum-core/test/specHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
noop,
} from '@datadog/browser-core'
import { SPEC_ENDPOINTS, mockClock, Clock } from '../../core/test/specHelper'
import { ForegroundContexts } from '../src/domain/foregroundContexts'
import { LifeCycle, LifeCycleEventType } from '../src/domain/lifeCycle'
import { ParentContexts } from '../src/domain/parentContexts'
import { ForegroundContexts } from '../src/domain/foregroundContexts'
import { trackViews, ViewEvent } from '../src/domain/rumEventsCollection/view/trackViews'
import { RumSession } from '../src/domain/rumSession'
import { CommonContext, RawRumEvent, RumContext, ViewContext } from '../src/rawRumEvent.types'
import { validateFormat } from './formatValidation'
Expand Down Expand Up @@ -206,3 +207,49 @@ function validateRumEventFormat(rawRumEvent: RawRumEvent) {
}
validateFormat(combine(fakeContext, rawRumEvent))
}

export type ViewTest = ReturnType<typeof setupViewTest>

export function setupViewTest(
{ lifeCycle, location, domMutationObservable, configuration }: BuildContext,
initialViewName?: string
) {
const { handler: viewUpdateHandler, getViewEvent: getViewUpdate, getHandledCount: getViewUpdateCount } = spyOnViews(
'view update'
)
lifeCycle.subscribe(LifeCycleEventType.VIEW_UPDATED, viewUpdateHandler)
const { handler: viewCreateHandler, getViewEvent: getViewCreate, getHandledCount: getViewCreateCount } = spyOnViews(
'view create'
)
lifeCycle.subscribe(LifeCycleEventType.VIEW_CREATED, viewCreateHandler)
const { stop, startView, addTiming } = trackViews(
location,
lifeCycle,
domMutationObservable,
!configuration.trackViewsManually,
initialViewName
)
return {
stop,
startView,
addTiming,
getViewUpdate,
getViewUpdateCount,
getViewCreate,
getViewCreateCount,
}
}

export function spyOnViews(name?: string) {
const handler = jasmine.createSpy(name)

function getViewEvent(index: number) {
return handler.calls.argsFor(index)[0] as ViewEvent
}

function getHandledCount() {
return handler.calls.count()
}

return { handler, getViewEvent, getHandledCount }
}

0 comments on commit 4fcff0d

Please sign in to comment.