Skip to content

Commit

Permalink
♻️ [RUM-1039] Harmonize view tests (#2430)
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque authored Sep 20, 2023
1 parent 5b6bb0b commit b45d271
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 593 deletions.
187 changes: 10 additions & 177 deletions packages/rum-core/src/domain/view/trackViewEventCounts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,209 +3,42 @@ import type { RumEvent } from '../../rumEvent.types'
import { LifeCycleEventType } from '../lifeCycle'
import type { TestSetupBuilder } from '../../../test'
import { setup } from '../../../test'
import { FrustrationType, RumEventType } from '../../rawRumEvent.types'
import { THROTTLE_VIEW_UPDATE_PERIOD } from './trackViews'
import type { ViewTest } from './setupViewTest.specHelper'
import { setupViewTest } from './setupViewTest.specHelper'
import { RumEventType } from '../../rawRumEvent.types'
import { trackViewEventCounts } from './trackViewEventCounts'

describe('trackViewEventCounts', () => {
let setupBuilder: TestSetupBuilder
let viewTest: ViewTest
let onChange: () => void

beforeEach(() => {
setupBuilder = setup()
.withFakeLocation('/foo')
.beforeBuild((buildContext) => {
viewTest = setupViewTest(buildContext)
return viewTest
})
onChange = jasmine.createSpy('onChange')

setupBuilder = setup().beforeBuild(({ lifeCycle }) => trackViewEventCounts(lifeCycle, 'view-id', onChange))
})

afterEach(() => {
setupBuilder.cleanup()
})

it('should track error count', () => {
it('should track events count', () => {
const { lifeCycle } = setupBuilder.build()
const { getViewUpdate, getViewUpdateCount, startView, getLatestViewContext } = viewTest

expect(getViewUpdateCount()).toEqual(1)
expect(getViewUpdate(0).eventCounts.errorCount).toEqual(0)

lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.ERROR,
view: getLatestViewContext(),
} as RumEvent & Context)
lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.ERROR,
view: getLatestViewContext(),
} as RumEvent & Context)
startView()

expect(getViewUpdateCount()).toEqual(3)
expect(getViewUpdate(1).eventCounts.errorCount).toEqual(2)
expect(getViewUpdate(2).eventCounts.errorCount).toEqual(0)
})

it('should track long task count', () => {
const { lifeCycle } = setupBuilder.build()
const { getViewUpdate, getViewUpdateCount, startView, getLatestViewContext } = viewTest

expect(getViewUpdateCount()).toEqual(1)
expect(getViewUpdate(0).eventCounts.longTaskCount).toEqual(0)

lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.LONG_TASK,
view: getLatestViewContext(),
} as RumEvent & Context)
startView()

expect(getViewUpdateCount()).toEqual(3)
expect(getViewUpdate(1).eventCounts.longTaskCount).toEqual(1)
expect(getViewUpdate(2).eventCounts.longTaskCount).toEqual(0)
})

it('should track resource count', () => {
const { lifeCycle } = setupBuilder.build()
const { getViewUpdate, getViewUpdateCount, startView, getLatestViewContext } = viewTest

expect(getViewUpdateCount()).toEqual(1)
expect(getViewUpdate(0).eventCounts.resourceCount).toEqual(0)

lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.RESOURCE,
view: getLatestViewContext(),
} as RumEvent & Context)
startView()

expect(getViewUpdateCount()).toEqual(3)
expect(getViewUpdate(1).eventCounts.resourceCount).toEqual(1)
expect(getViewUpdate(2).eventCounts.resourceCount).toEqual(0)
})

it('should track action count', () => {
const { lifeCycle } = setupBuilder.build()
const { getViewUpdate, getViewUpdateCount, startView, getLatestViewContext } = viewTest

expect(getViewUpdateCount()).toEqual(1)
expect(getViewUpdate(0).eventCounts.actionCount).toEqual(0)

lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.ACTION,
action: { type: 'custom' },
view: getLatestViewContext(),
} as RumEvent & Context)
startView()

expect(getViewUpdateCount()).toEqual(3)
expect(getViewUpdate(1).eventCounts.actionCount).toEqual(1)
expect(getViewUpdate(2).eventCounts.actionCount).toEqual(0)
})

it('should track frustration count', () => {
const { lifeCycle } = setupBuilder.build()
const { getViewUpdate, getViewUpdateCount, startView, getLatestViewContext } = viewTest

expect(getViewUpdateCount()).toEqual(1)
expect(getViewUpdate(0).eventCounts.frustrationCount).toEqual(0)

lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.ACTION,
action: {
type: 'click',
id: '123',
frustration: {
type: [FrustrationType.DEAD_CLICK, FrustrationType.ERROR_CLICK],
},
},
view: getLatestViewContext(),
view: { id: 'view-id' },
} as RumEvent & Context)
startView()

expect(getViewUpdateCount()).toEqual(3)
expect(getViewUpdate(1).eventCounts.frustrationCount).toEqual(2)
expect(getViewUpdate(2).eventCounts.frustrationCount).toEqual(0)
expect(onChange).toHaveBeenCalledTimes(1)
})

it('should not count child events unrelated to the view', () => {
const { lifeCycle } = setupBuilder.build()
const { getViewUpdate, getViewUpdateCount, startView } = viewTest

expect(getViewUpdateCount()).toEqual(1)
expect(getViewUpdate(0).eventCounts.errorCount).toEqual(0)

lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.ERROR,
view: { id: 'unrelated-view-id' },
} as RumEvent & Context)
startView()

expect(getViewUpdateCount()).toEqual(3)
expect(getViewUpdate(1).eventCounts.errorCount).toEqual(0)
expect(getViewUpdate(2).eventCounts.errorCount).toEqual(0)
})

it('should reset event count when the view changes', () => {
const { lifeCycle, changeLocation } = setupBuilder.build()
const { getViewUpdate, getViewUpdateCount, startView, getLatestViewContext } = viewTest

expect(getViewUpdateCount()).toEqual(1)
expect(getViewUpdate(0).eventCounts.resourceCount).toEqual(0)

lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.RESOURCE,
view: getLatestViewContext(),
} as RumEvent & Context)
startView()

expect(getViewUpdateCount()).toEqual(3)
expect(getViewUpdate(1).eventCounts.resourceCount).toEqual(1)
expect(getViewUpdate(2).eventCounts.resourceCount).toEqual(0)

lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.RESOURCE,
view: getLatestViewContext(),
} as RumEvent & Context)
lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.RESOURCE,
view: getLatestViewContext(),
} as RumEvent & Context)
changeLocation('/baz')

expect(getViewUpdateCount()).toEqual(5)
expect(getViewUpdate(3).eventCounts.resourceCount).toEqual(2)
expect(getViewUpdate(4).eventCounts.resourceCount).toEqual(0)
})

it('should update eventCounts when a resource event is collected (throttled)', () => {
const { lifeCycle, clock } = setupBuilder.withFakeClock().build()
const { getViewUpdate, getViewUpdateCount, getLatestViewContext } = viewTest

expect(getViewUpdateCount()).toEqual(1)
expect(getViewUpdate(0).eventCounts).toEqual({
errorCount: 0,
longTaskCount: 0,
resourceCount: 0,
actionCount: 0,
frustrationCount: 0,
})

lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.RESOURCE,
view: getLatestViewContext(),
} as RumEvent & Context)

expect(getViewUpdateCount()).toEqual(1)

clock.tick(THROTTLE_VIEW_UPDATE_PERIOD)

expect(getViewUpdateCount()).toEqual(2)
expect(getViewUpdate(1).eventCounts).toEqual({
errorCount: 0,
longTaskCount: 0,
resourceCount: 1,
actionCount: 0,
frustrationCount: 0,
})
expect(onChange).not.toHaveBeenCalled()
})
})
Loading

0 comments on commit b45d271

Please sign in to comment.