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 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CustomerDataTracker, RelativeTime } from '@datadog/browser-core'
import { relativeToClocks, createCustomerDataTracker, noop } from '@datadog/browser-core'
import type { Clock } from '@datadog/browser-core/test'
import { mockClock } 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'
Expand All @@ -17,10 +17,10 @@ describe('featureFlagContexts', () => {
clock = mockClock()
customerDataTracker = createCustomerDataTracker(noop)
featureFlagContexts = startFeatureFlagContexts(lifeCycle, customerDataTracker)
})

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

it('should return undefined before the initial view', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import type { Context } from '@datadog/browser-core'
import { registerCleanupTask } from '@datadog/browser-core/test'
import type { RumEvent } from '../../rumEvent.types'
import { LifeCycle, LifeCycleEventType } from '../lifeCycle'
import { RumEventType } from '../../rawRumEvent.types'
import { trackViewEventCounts } from './trackViewEventCounts'

describe('trackViewEventCounts', () => {
const lifeCycle = new LifeCycle()
let viewEventCountsTracking: ReturnType<typeof trackViewEventCounts>
let onChange: () => void

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

viewEventCountsTracking = trackViewEventCounts(lifeCycle, 'view-id', onChange)
})

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

it('should track events count', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { RelativeTime } from '@datadog/browser-core'
import { registerCleanupTask } from '@datadog/browser-core/test'
import { resetExperimentalFeatures, elapsed, ONE_SECOND } from '@datadog/browser-core'
import { appendElement, appendText, createPerformanceEntry } from '../../../../test'
import { LifeCycle, LifeCycleEventType } from '../../lifeCycle'
Expand All @@ -7,19 +8,36 @@ 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', () => {
const lifeCycle = new LifeCycle()
let isLayoutShiftSupported: boolean
let originalSupportedEntryTypes: PropertyDescriptor | undefined
let clsCallback: jasmine.Spy<(csl: CumulativeLayoutShift) => void>
let viewStart: RelativeTime
let cleanup: () => void

function startCLSTracking() {
function startCLSTracking(
{ viewStart, isLayoutShiftSupported }: StartCLSTrackingArgs = {
viewStart: 0 as RelativeTime,
isLayoutShiftSupported: true,
}
) {
clsCallback = jasmine.createSpy()
originalSupportedEntryTypes = Object.getOwnPropertyDescriptor(PerformanceObserver, 'supportedEntryTypes')
Object.defineProperty(PerformanceObserver, 'supportedEntryTypes', {
get: () => (isLayoutShiftSupported ? ['layout-shift'] : []),
})

const clsTrackingesult = trackCumulativeLayoutShift({} as RumConfiguration, lifeCycle, viewStart, clsCallback)
cleanup = clsTrackingesult.stop

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

beforeEach(() => {
Expand All @@ -30,39 +48,21 @@ describe('trackCumulativeLayoutShift', () => {
) {
pending('No LayoutShift API support')
}

viewStart = 0 as RelativeTime
startCLSTracking()

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

afterEach(() => {
cleanup()
if (originalSupportedEntryTypes) {
Object.defineProperty(PerformanceObserver, 'supportedEntryTypes', originalSupportedEntryTypes)
}
})

it('should be initialized to 0', () => {
startCLSTracking()
expect(clsCallback).toHaveBeenCalledOnceWith({ value: 0 })
})

it('should be initialized to undefined if layout-shift is not supported', () => {
// stop the previous tracking from beforeEach
cleanup()

isLayoutShiftSupported = false
startCLSTracking()
startCLSTracking({ viewStart: 0 as RelativeTime, isLayoutShiftSupported: false })

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

it('should accumulate layout shift values for the first session window', () => {
startCLSTracking()
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.LAYOUT_SHIFT, { value: 0.1, startTime: 1 as RelativeTime }),
])
Expand All @@ -80,6 +80,7 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should round the cumulative layout shift value to 4 decimals', () => {
startCLSTracking()
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.LAYOUT_SHIFT, { value: 1.23456789 }),
])
Expand All @@ -93,6 +94,7 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should ignore entries with recent input', () => {
startCLSTracking()
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.LAYOUT_SHIFT, {
value: 0.1,
Expand All @@ -107,6 +109,7 @@ describe('trackCumulativeLayoutShift', () => {
})

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

it('should create a new session window if the current session window is more than 5 second', () => {
startCLSTracking()
for (let i = 1; i <= 7; i++) {
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.LAYOUT_SHIFT, {
Expand All @@ -149,6 +153,7 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should get the max value sessions', () => {
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 @@ -200,11 +205,8 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should get the time from the beginning of the view', () => {
// stop the previous tracking from beforeEach
cleanup()

viewStart = 100 as RelativeTime
startCLSTracking()
const viewStart = 100 as RelativeTime
startCLSTracking({ viewStart, isLayoutShiftSupported: true })

const shiftStart = 110 as RelativeTime
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
Expand All @@ -220,6 +222,7 @@ describe('trackCumulativeLayoutShift', () => {
})

it('should return the first target element selector amongst all the shifted nodes', () => {
startCLSTracking()
const textNode = appendText('text')
const divElement = appendElement('<div id="div-element"></div>')

Expand All @@ -234,6 +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', () => {
startCLSTracking()
// first session window
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.LAYOUT_SHIFT, {
Expand Down Expand Up @@ -261,6 +265,7 @@ describe('trackCumulativeLayoutShift', () => {
})

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

// first session window: { value: 0.5, time: 1, targetSelector: '#div-element' }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RelativeTime } from '@datadog/browser-core'
import { setPageVisibility } from '@datadog/browser-core/test'
import { registerCleanupTask, setPageVisibility } from '@datadog/browser-core/test'
import { RumPerformanceEntryType } from '../../../browser/performanceObservable'
import { createPerformanceEntry } from '../../../../test'
import { LifeCycle, LifeCycleEventType } from '../../lifeCycle'
Expand All @@ -10,28 +10,20 @@ import { trackFirstHidden } from './trackFirstHidden'
describe('trackFirstContentfulPaint', () => {
const lifeCycle = new LifeCycle()
let fcpCallback: jasmine.Spy<(value: RelativeTime) => void>
let cleanup: () => void

function startTrackingFCP() {
fcpCallback = jasmine.createSpy()
const firstHidden = trackFirstHidden({} as RumConfiguration)
const firstContentfulPaint = trackFirstContentfulPaint(lifeCycle, firstHidden, fcpCallback)

cleanup = () => {
registerCleanupTask(() => {
firstHidden.stop()
firstContentfulPaint.stop()
}
})
}

beforeEach(() => {
startTrackingFCP()
})

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

it('should provide the first contentful paint timing', () => {
startTrackingFCP()
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.PAINT),
])
Expand All @@ -41,9 +33,6 @@ describe('trackFirstContentfulPaint', () => {
})

it('should be discarded if the page is hidden', () => {
// stop the previous setup from the beforeEach
cleanup()

setPageVisibility('hidden')
startTrackingFCP()

Expand All @@ -54,6 +43,7 @@ describe('trackFirstContentfulPaint', () => {
})

it('should be discarded if it is reported after a long time', () => {
startTrackingFCP()
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.PAINT, { startTime: FCP_MAXIMUM_DELAY as RelativeTime }),
])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Duration, type RelativeTime, resetExperimentalFeatures } from '@datadog/browser-core'
import { setPageVisibility } from '@datadog/browser-core/test'
import { registerCleanupTask, setPageVisibility } from '@datadog/browser-core/test'
import { appendElement, appendText, createPerformanceEntry } from '../../../../test'
import { LifeCycle, LifeCycleEventType } from '../../lifeCycle'
import type { RumConfiguration } from '../../configuration'
Expand All @@ -12,31 +12,23 @@ describe('firstInputTimings', () => {
const lifeCycle = new LifeCycle()
let fitCallback: jasmine.Spy<(firstInput: FirstInput) => void>
let configuration: RumConfiguration
let cleanup: () => void

function startFirstInputTracking() {
configuration = {} as RumConfiguration
fitCallback = jasmine.createSpy()

const firstHidden = trackFirstHidden(configuration)
const firstInputTimings = trackFirstInput(lifeCycle, configuration, firstHidden, fitCallback)

cleanup = () => {
registerCleanupTask(() => {
firstHidden.stop()
firstInputTimings.stop()
}
resetExperimentalFeatures()
N-Boutaib marked this conversation as resolved.
Show resolved Hide resolved
})
}

beforeEach(() => {
configuration = {} as RumConfiguration
fitCallback = jasmine.createSpy()

startFirstInputTracking()
})

afterEach(() => {
cleanup()
resetExperimentalFeatures()
})

it('should provide the first input timings', () => {
startFirstInputTracking()
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.FIRST_INPUT),
])
Expand All @@ -49,6 +41,7 @@ describe('firstInputTimings', () => {
})

it('should provide the first input target selector', () => {
startFirstInputTracking()
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.FIRST_INPUT, {
target: appendElement('<button id="fid-target-element"></button>'),
Expand All @@ -63,6 +56,7 @@ describe('firstInputTimings', () => {
})

it("should not provide the first input target if it's not a DOM element", () => {
startFirstInputTracking()
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.FIRST_INPUT, {
target: appendText('text'),
Expand All @@ -77,9 +71,6 @@ describe('firstInputTimings', () => {
})

it('should be discarded if the page is hidden', () => {
// stop the current tracking from beforeEach
cleanup()

setPageVisibility('hidden')
startFirstInputTracking()

Expand All @@ -91,6 +82,7 @@ describe('firstInputTimings', () => {
})

it('should be adjusted to 0 if the computed value would be negative due to browser timings imprecisions', () => {
startFirstInputTracking()
lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.FIRST_INPUT, {
processingStart: 900 as RelativeTime,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Duration, RelativeTime } from '@datadog/browser-core'
import { registerCleanupTask } from '@datadog/browser-core/test'
import { RumPerformanceEntryType } from '../../../browser/performanceObservable'
import { createPerformanceEntry } from '../../../../test'
import { LifeCycle, LifeCycleEventType } from '../../lifeCycle'
Expand All @@ -22,10 +23,8 @@ describe('trackInitialViewMetrics', () => {
setLoadEventSpy,
scheduleViewUpdateSpy
)
})

afterEach(() => {
trackInitialViewMetricsResult.stop()
registerCleanupTask(trackInitialViewMetricsResult.stop)
})

it('should merge metrics from various sources', () => {
Expand Down
Loading