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

♻️ Get rid of setupBuilder from simple unit tests - pt 1 #2858

Merged
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
51 changes: 29 additions & 22 deletions packages/rum-core/src/domain/vital/vitalCollection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
import type { Duration } from '@datadog/browser-core'
import type { Duration, Subscription } from '@datadog/browser-core'
import { mockClock, type Clock } from '@datadog/browser-core/test'
import { clocksNow } from '@datadog/browser-core'
import type { TestSetupBuilder } from '../../../test'
import { setup } from '../../../test'
import type { RawRumVitalEvent } from '../../rawRumEvent.types'
import type { RawRumEvent, RawRumVitalEvent } from '../../rawRumEvent.types'
import { VitalType, RumEventType } from '../../rawRumEvent.types'
import type { RawRumEventCollectedData } from '../lifeCycle'
import { LifeCycle, LifeCycleEventType } from '../lifeCycle'
import type { PageStateHistory } from '../contexts/pageStateHistory'
import { createVitalInstance, startVitalCollection } from './vitalCollection'

const pageStateHistory: PageStateHistory = {
findAll: () => undefined,
addPageState: () => {},
stop: () => {},
wasInPageStateAt: () => false,
wasInPageStateDuringPeriod: () => false,
}

describe('vitalCollection', () => {
let setupBuilder: TestSetupBuilder
const lifeCycle = new LifeCycle()
let rawRumEvents: Array<RawRumEventCollectedData<RawRumEvent>> = []
let clock: Clock
let vitalCollection: ReturnType<typeof startVitalCollection>
let wasInPageStateDuringPeriodSpy: jasmine.Spy<jasmine.Func>
let eventsSubscription: Subscription

beforeEach(() => {
setupBuilder = setup()
.withFakeClock()
.beforeBuild(({ lifeCycle, pageStateHistory }) => {
wasInPageStateDuringPeriodSpy = spyOn(pageStateHistory, 'wasInPageStateDuringPeriod')
vitalCollection = startVitalCollection(lifeCycle, pageStateHistory)
})
clock = mockClock()
wasInPageStateDuringPeriodSpy = spyOn(pageStateHistory, 'wasInPageStateDuringPeriod')
vitalCollection = startVitalCollection(lifeCycle, pageStateHistory)
eventsSubscription = lifeCycle.subscribe(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, (data) => {
BenoitZugmeyer marked this conversation as resolved.
Show resolved Hide resolved
BenoitZugmeyer marked this conversation as resolved.
Show resolved Hide resolved
rawRumEvents.push(data)
})
})

afterEach(() => {
eventsSubscription.unsubscribe()
rawRumEvents = []
})

describe('custom duration', () => {
describe('createVitalInstance', () => {
it('should create duration vital from a vital instance', () => {
const { clock } = setupBuilder.build()

const cbSpy = jasmine.createSpy()

const vital = createVitalInstance(cbSpy, { name: 'foo' })
Expand Down Expand Up @@ -53,8 +69,6 @@ describe('vitalCollection', () => {
})

it('should create multiple duration vitals from createVitalInstance', () => {
const { clock } = setupBuilder.build()

const cbSpy = jasmine.createSpy()

const vital1 = createVitalInstance(cbSpy, { name: 'foo', details: 'component 1' })
Expand Down Expand Up @@ -108,8 +122,6 @@ describe('vitalCollection', () => {
})

it('should create a vital from start API', () => {
const { rawRumEvents, clock } = setupBuilder.build()

const vital = vitalCollection.startDurationVital({
name: 'foo',
context: { foo: 'bar' },
Expand All @@ -127,8 +139,6 @@ describe('vitalCollection', () => {
})

it('should create a vital from add API', () => {
const { rawRumEvents } = setupBuilder.build()

vitalCollection.addDurationVital({
name: 'foo',
type: VitalType.DURATION,
Expand All @@ -145,7 +155,6 @@ describe('vitalCollection', () => {
})

it('should discard a vital for which a frozen state happened', () => {
const { rawRumEvents } = setupBuilder.build()
wasInPageStateDuringPeriodSpy.and.returnValue(true)

vitalCollection.addDurationVital({
Expand All @@ -160,8 +169,6 @@ describe('vitalCollection', () => {
})

it('should collect raw rum event from duration vital', () => {
const { rawRumEvents } = setupBuilder.build()

const vital = createVitalInstance(vitalCollection.addDurationVital, { name: 'foo' })
vital.stop()

Expand Down