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
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 12 additions & 25 deletions packages/rum-core/src/domain/contexts/featureFlagContext.spec.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
import type { CustomerDataTracker, RelativeTime } from '@datadog/browser-core'
import { relativeToClocks, createCustomerDataTracker, noop } from '@datadog/browser-core'
import type { TestSetupBuilder } from '../../../test'
import { setup } from '../../../test'
import { LifeCycleEventType } from '../lifeCycle'
import type { Clock } from '@datadog/browser-core/test'
import { mockClock, registerCleanupTask } from '@datadog/browser-core/test'
import { LifeCycle, LifeCycleEventType } from '../lifeCycle'
import type { ViewCreatedEvent, ViewEndedEvent } from '../view/trackViews'
import type { FeatureFlagContexts } from './featureFlagContext'
import { startFeatureFlagContexts } from './featureFlagContext'

describe('featureFlagContexts', () => {
let setupBuilder: TestSetupBuilder
const lifeCycle = new LifeCycle()
let clock: Clock
let customerDataTracker: CustomerDataTracker
let featureFlagContexts: FeatureFlagContexts

beforeEach(() => {
setupBuilder = setup().beforeBuild(({ lifeCycle }) => {
customerDataTracker = createCustomerDataTracker(noop)
featureFlagContexts = startFeatureFlagContexts(lifeCycle, customerDataTracker)
})
})
clock = mockClock()
customerDataTracker = createCustomerDataTracker(noop)
featureFlagContexts = startFeatureFlagContexts(lifeCycle, customerDataTracker)

afterEach(() => {
featureFlagContexts.stop()
registerCleanupTask(() => {
clock.cleanup()
featureFlagContexts.stop()
})
})

it('should return undefined before the initial view', () => {
setupBuilder.build()

expect(featureFlagContexts.findFeatureFlagEvaluations()).toBeUndefined()
})

describe('addFeatureFlagEvaluation', () => {
it('should add feature flag evaluations of any type', () => {
const { lifeCycle } = setupBuilder.build()

lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
Expand All @@ -53,8 +50,6 @@ describe('featureFlagContexts', () => {
})

it('should replace existing feature flag evaluation to the current context', () => {
const { lifeCycle } = setupBuilder.build()

lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
Expand All @@ -69,8 +64,6 @@ describe('featureFlagContexts', () => {
})

it('should notify the customer data tracker on feature flag evaluation', () => {
const { lifeCycle } = setupBuilder.build()

const updateCustomerDataSpy = spyOn(customerDataTracker, 'updateCustomerData')

lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
Expand All @@ -89,14 +82,10 @@ describe('featureFlagContexts', () => {
* (which seems unlikely) and this event would anyway be rejected by lack of view id
*/
it('should return undefined when no current view', () => {
setupBuilder.build()

expect(featureFlagContexts.findFeatureFlagEvaluations()).toBeUndefined()
})

it('should clear feature flag context on new view', () => {
const { lifeCycle } = setupBuilder.build()

lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
Expand All @@ -113,8 +102,6 @@ describe('featureFlagContexts', () => {
})

it('should return the feature flag context corresponding to the start time', () => {
const { lifeCycle, clock } = setupBuilder.withFakeClock().build()

lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
Expand Down
14 changes: 5 additions & 9 deletions packages/rum-core/src/domain/view/trackViewEventCounts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import type { Context } from '@datadog/browser-core'
import { registerCleanupTask } from '@datadog/browser-core/test'
import type { RumEvent } from '../../rumEvent.types'
import { LifeCycleEventType } from '../lifeCycle'
import type { TestSetupBuilder } from '../../../test'
import { setup } from '../../../test'
import { LifeCycle, LifeCycleEventType } from '../lifeCycle'
import { RumEventType } from '../../rawRumEvent.types'
import { trackViewEventCounts } from './trackViewEventCounts'

describe('trackViewEventCounts', () => {
let setupBuilder: TestSetupBuilder
const lifeCycle = new LifeCycle()
let onChange: () => void

beforeEach(() => {
onChange = jasmine.createSpy('onChange')

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

it('should track events count', () => {
const { lifeCycle } = setupBuilder.build()

lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.ERROR,
view: { id: 'view-id' },
Expand All @@ -28,8 +26,6 @@ describe('trackViewEventCounts', () => {
})

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

lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, {
type: RumEventType.ERROR,
view: { id: 'unrelated-view-id' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,68 @@
import type { RelativeTime } from '@datadog/browser-core'
import { registerCleanupTask } from '@datadog/browser-core/test'
import { resetExperimentalFeatures, elapsed, ONE_SECOND } from '@datadog/browser-core'
import type { TestSetupBuilder } from '../../../../test'
import { appendElement, appendText, createPerformanceEntry, setup } from '../../../../test'
import { LifeCycleEventType } from '../../lifeCycle'
import { appendElement, appendText, createPerformanceEntry } from '../../../../test'
import { LifeCycle, LifeCycleEventType } from '../../lifeCycle'
import { RumPerformanceEntryType } from '../../../browser/performanceObservable'
import type { RumConfiguration } from '../../configuration'
import type { CumulativeLayoutShift } from './trackCumulativeLayoutShift'
import { MAX_WINDOW_DURATION, trackCumulativeLayoutShift } from './trackCumulativeLayoutShift'

interface StartCLSTrackingArgs {
viewStart: RelativeTime
isLayoutShiftSupported: boolean
}

describe('trackCumulativeLayoutShift', () => {
let setupBuilder: TestSetupBuilder
let isLayoutShiftSupported: boolean
const lifeCycle = new LifeCycle()
let originalSupportedEntryTypes: PropertyDescriptor | undefined
let clsCallback: jasmine.Spy<(csl: CumulativeLayoutShift) => void>
let viewStart: RelativeTime

beforeEach(() => {
if (
!window.PerformanceObserver ||
!PerformanceObserver.supportedEntryTypes ||
!PerformanceObserver.supportedEntryTypes.includes('layout-shift')
) {
pending('No LayoutShift API support')
function startCLSTracking(
{ viewStart, isLayoutShiftSupported }: StartCLSTrackingArgs = {
viewStart: 0 as RelativeTime,
isLayoutShiftSupported: true,
}

) {
clsCallback = jasmine.createSpy()
viewStart = 0 as RelativeTime
setupBuilder = setup().beforeBuild(({ lifeCycle, configuration }) =>
trackCumulativeLayoutShift(configuration, lifeCycle, viewStart, clsCallback)
)

originalSupportedEntryTypes = Object.getOwnPropertyDescriptor(PerformanceObserver, 'supportedEntryTypes')
isLayoutShiftSupported = true
Object.defineProperty(PerformanceObserver, 'supportedEntryTypes', {
get: () => (isLayoutShiftSupported ? ['layout-shift'] : []),
})
})

afterEach(() => {
if (originalSupportedEntryTypes) {
Object.defineProperty(PerformanceObserver, 'supportedEntryTypes', originalSupportedEntryTypes)
const clsTrackingesult = trackCumulativeLayoutShift({} as RumConfiguration, lifeCycle, viewStart, clsCallback)

registerCleanupTask(() => {
clsTrackingesult.stop()
if (originalSupportedEntryTypes) {
Object.defineProperty(PerformanceObserver, 'supportedEntryTypes', originalSupportedEntryTypes)
}
})
}

beforeEach(() => {
if (
!window.PerformanceObserver ||
!PerformanceObserver.supportedEntryTypes ||
!PerformanceObserver.supportedEntryTypes.includes('layout-shift')
) {
pending('No LayoutShift API support')
}
})

it('should be initialized to 0', () => {
setupBuilder.build()

startCLSTracking()
expect(clsCallback).toHaveBeenCalledOnceWith({ value: 0 })
})

it('should be initialized to undefined if layout-shift is not supported', () => {
isLayoutShiftSupported = false
setupBuilder.build()
startCLSTracking({ viewStart: 0 as RelativeTime, isLayoutShiftSupported: false })

expect(clsCallback).not.toHaveBeenCalled()
})

it('should accumulate layout shift values for the first session window', () => {
const { lifeCycle } = setupBuilder.build()

startCLSTracking()
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.LAYOUT_SHIFT, { value: 0.1, startTime: 1 as RelativeTime }),
])
Expand All @@ -75,8 +80,7 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should round the cumulative layout shift value to 4 decimals', () => {
const { lifeCycle } = setupBuilder.build()

startCLSTracking()
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.LAYOUT_SHIFT, { value: 1.23456789 }),
])
Expand All @@ -90,8 +94,7 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should ignore entries with recent input', () => {
const { lifeCycle } = setupBuilder.build()

startCLSTracking()
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.LAYOUT_SHIFT, {
value: 0.1,
Expand All @@ -106,7 +109,7 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should create a new session window if the gap is more than 1 second', () => {
const { lifeCycle } = setupBuilder.build()
startCLSTracking()
// first session window
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.LAYOUT_SHIFT, { value: 0.1, startTime: 0 as RelativeTime }),
Expand All @@ -131,8 +134,7 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should create a new session window if the current session window is more than 5 second', () => {
const { lifeCycle } = setupBuilder.build()

startCLSTracking()
for (let i = 1; i <= 7; i++) {
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.LAYOUT_SHIFT, {
Expand All @@ -151,8 +153,7 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should get the max value sessions', () => {
const { lifeCycle } = setupBuilder.build()

startCLSTracking()
// first session window: { value: 0.3, time: 1 }
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.LAYOUT_SHIFT, { value: 0.1, startTime: 0 as RelativeTime }),
Expand Down Expand Up @@ -204,8 +205,9 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should get the time from the beginning of the view', () => {
viewStart = 100 as RelativeTime
const { lifeCycle } = setupBuilder.build()
const viewStart = 100 as RelativeTime
startCLSTracking({ viewStart, isLayoutShiftSupported: true })

const shiftStart = 110 as RelativeTime
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.LAYOUT_SHIFT, { value: 0.1, startTime: shiftStart }),
Expand All @@ -220,8 +222,7 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should return the first target element selector amongst all the shifted nodes', () => {
const { lifeCycle } = setupBuilder.build()

startCLSTracking()
const textNode = appendText('text')
const divElement = appendElement('<div id="div-element"></div>')

Expand All @@ -236,8 +237,7 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should not return the target element when the element is detached from the DOM before the performance entry event is triggered', () => {
const { lifeCycle } = setupBuilder.build()

startCLSTracking()
// first session window
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.LAYOUT_SHIFT, {
Expand All @@ -247,7 +247,6 @@ describe('trackCumulativeLayoutShift', () => {
])

expect(clsCallback.calls.mostRecent().args[0].value).toEqual(0.2)

// second session window
// first shift with an element
const divElement = appendElement('<div id="div-element"></div>')
Expand All @@ -266,7 +265,7 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should get the target element and time of the largest layout shift', () => {
const { lifeCycle } = setupBuilder.build()
startCLSTracking()
const divElement = appendElement('<div id="div-element"></div>')

// first session window: { value: 0.5, time: 1, targetSelector: '#div-element' }
Expand Down
Loading