Skip to content

Commit

Permalink
Scope trackScrollMetrics tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Sep 20, 2023
1 parent bd4de0c commit 5170675
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ViewLoadingType } from '../../../rawRumEvent.types'
import type { TestSetupBuilder } from '../../../../test'
import { createPerformanceEntry, setup } from '../../../../test'
import { PAGE_ACTIVITY_END_DELAY, PAGE_ACTIVITY_VALIDATION_DELAY } from '../../waitPageActivityEnd'
import { THROTTLE_VIEW_UPDATE_PERIOD } from '../trackViews'
import { RumPerformanceEntryType } from '../../../browser/performanceCollection'
import { trackLoadingTime } from './trackLoadingTime'

Expand Down Expand Up @@ -118,7 +117,6 @@ describe('trackLoadingTime', () => {

domMutationObservable.notify()
clock.tick(AFTER_PAGE_ACTIVITY_END_DELAY)
clock.tick(THROTTLE_VIEW_UPDATE_PERIOD)

expect(loadingTimeCallback).toHaveBeenCalledOnceWith(addDuration(BEFORE_PAGE_ACTIVITY_VALIDATION_DELAY, CLOCK_GAP))
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import type { RelativeTime, Subscription, TimeStamp } from '@datadog/browser-core'
import type { Duration, RelativeTime, Subscription, TimeStamp } from '@datadog/browser-core'
import { DOM_EVENT, Observable, isIE } from '@datadog/browser-core'
import type { Clock } from '@datadog/browser-core/test'
import { createNewEvent, mockClock } from '@datadog/browser-core/test'
import { createNewEvent } from '@datadog/browser-core/test'
import type { TestSetupBuilder } from '../../../../test'
import { setup } from '../../../../test'
import type { RumConfiguration } from '../../configuration'
import type { ViewTest } from '../setupViewTest.specHelper'
import { setupViewTest } from '../setupViewTest.specHelper'
import type { ScrollValues } from './trackScrollMetrics'
import type { ScrollMetrics, ScrollValues } from './trackScrollMetrics'
import { createScrollValuesObservable, trackScrollMetrics } from './trackScrollMetrics'

describe('createScrollValuesObserver', () => {
Expand Down Expand Up @@ -52,69 +49,64 @@ describe('createScrollValuesObserver', () => {

describe('trackScrollMetrics', () => {
let setupBuilder: TestSetupBuilder
let viewTest: ViewTest
let stopTrackScrollMetrics: () => void
let callbackSpy: jasmine.Spy
let clock: Clock
let scrollMetricsCallback: jasmine.Spy<(metrics: ScrollMetrics) => void>

const scrollObservable = new Observable<ScrollValues>()

beforeEach(() => {
scrollMetricsCallback = jasmine.createSpy()
setupBuilder = setup()
.withFakeLocation('/foo')
.beforeBuild((buildContext) => {
viewTest = setupViewTest(buildContext)
return viewTest
})
callbackSpy = jasmine.createSpy('callback')
stopTrackScrollMetrics = trackScrollMetrics(
{} as RumConfiguration,
{ relative: 0 as RelativeTime, timeStamp: 0 as TimeStamp },
callbackSpy,
scrollObservable
).stop
clock = mockClock()
.withFakeClock()
.beforeBuild(({ configuration }) =>
trackScrollMetrics(
configuration,
{ relative: 0 as RelativeTime, timeStamp: 0 as TimeStamp },
scrollMetricsCallback,
scrollObservable
)
)
})

afterEach(() => {
stopTrackScrollMetrics()
document.body.innerHTML = ''
setupBuilder.cleanup()
clock.cleanup()
})

const updateScrollValues = (scrollValues: ScrollValues) => {
clock.tick(100)
setupBuilder.clock!.tick(100)
scrollObservable.notify(scrollValues)
}

it('should update scroll height and scroll depth', () => {
setupBuilder.build()
updateScrollValues({ scrollDepth: 700, scrollHeight: 2000, scrollTop: 100 })
expect(callbackSpy).toHaveBeenCalledOnceWith({
expect(scrollMetricsCallback).toHaveBeenCalledOnceWith({
maxDepth: 700,
maxDepthScrollHeight: 2000,
maxDepthTime: 100,
maxDepthTime: 100 as Duration,
maxDepthScrollTop: 100,
})
})
it('should update time and scroll height only if it has increased', () => {
setupBuilder.build()
updateScrollValues({ scrollDepth: 700, scrollHeight: 2000, scrollTop: 100 })
updateScrollValues({ scrollDepth: 700, scrollHeight: 1900, scrollTop: 100 })
expect(callbackSpy).toHaveBeenCalledOnceWith({
expect(scrollMetricsCallback).toHaveBeenCalledOnceWith({
maxDepth: 700,
maxDepthScrollHeight: 2000,
maxDepthTime: 100,
maxDepthTime: 100 as Duration,
maxDepthScrollTop: 100,
})
})

it('should update max depth only if it has increased', () => {
setupBuilder.build()
updateScrollValues({ scrollDepth: 700, scrollHeight: 2000, scrollTop: 100 })
updateScrollValues({ scrollDepth: 600, scrollHeight: 2000, scrollTop: 0 })
expect(callbackSpy).toHaveBeenCalledOnceWith({
expect(scrollMetricsCallback).toHaveBeenCalledOnceWith({
maxDepth: 700,
maxDepthScrollHeight: 2000,
maxDepthTime: 100,
maxDepthTime: 100 as Duration,
maxDepthScrollTop: 100,
})
})
Expand Down

0 comments on commit 5170675

Please sign in to comment.