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
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
import { type Duration, type RelativeTime, resetExperimentalFeatures } from '@datadog/browser-core'
import { setPageVisibility } from '@datadog/browser-core/test'
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 type { RumConfiguration } from '../../configuration'
import { RumPerformanceEntryType } from '../../../browser/performanceObservable'
import type { FirstInput } from './trackFirstInput'
import { trackFirstInput } from './trackFirstInput'
import { trackFirstHidden } from './trackFirstHidden'

describe('firstInputTimings', () => {
let setupBuilder: TestSetupBuilder
const lifeCycle = new LifeCycle()
let fitCallback: jasmine.Spy<(firstInput: FirstInput) => void>
let configuration: RumConfiguration
let cleanup: () => void

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

cleanup = () => {
firstHidden.stop()
firstInputTimings.stop()
}
}

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

setupBuilder = setup().beforeBuild(({ lifeCycle }) => {
const firstHidden = trackFirstHidden(configuration)
const firstInputTimings = trackFirstInput(lifeCycle, configuration, firstHidden, fitCallback)

return {
stop() {
firstHidden.stop()
firstInputTimings.stop()
},
}
})
startFirstInputTracking()
})

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

it('should provide the first input timings', () => {
const { lifeCycle } = setupBuilder.build()

lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.FIRST_INPUT),
])
Expand All @@ -50,8 +49,6 @@ describe('firstInputTimings', () => {
})

it('should provide the first input target selector', () => {
const { lifeCycle } = setupBuilder.build()

lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.FIRST_INPUT, {
target: appendElement('<button id="fid-target-element"></button>'),
Expand All @@ -66,8 +63,6 @@ describe('firstInputTimings', () => {
})

it("should not provide the first input target if it's not a DOM element", () => {
const { lifeCycle } = setupBuilder.build()

lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.FIRST_INPUT, {
target: appendText('text'),
Expand All @@ -82,8 +77,11 @@ describe('firstInputTimings', () => {
})

it('should be discarded if the page is hidden', () => {
// stop the current tracking from beforeEach
cleanup()
BenoitZugmeyer marked this conversation as resolved.
Show resolved Hide resolved

setPageVisibility('hidden')
const { lifeCycle } = setupBuilder.build()
startFirstInputTracking()

lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.FIRST_INPUT),
Expand All @@ -93,8 +91,6 @@ describe('firstInputTimings', () => {
})

it('should be adjusted to 0 if the computed value would be negative due to browser timings imprecisions', () => {
const { lifeCycle } = setupBuilder.build()

lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, [
createPerformanceEntry(RumPerformanceEntryType.FIRST_INPUT, {
processingStart: 900 as RelativeTime,
Expand Down