-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
0a0e00a
commit a865fc1
Showing
12 changed files
with
253 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 35 additions & 48 deletions
83
packages/rum-core/src/domain/contexts/pageStateHistory.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,79 @@ | ||
import type { RelativeTime } from '@datadog/browser-core' | ||
import { resetExperimentalFeatures } from '@datadog/browser-core' | ||
import type { TestSetupBuilder } from '../../../test' | ||
import { setup } from '../../../test' | ||
import type { RelativeTime, ServerDuration } from '@datadog/browser-core' | ||
import type { Clock } from '../../../../core/test' | ||
import { mockClock } from '../../../../core/test' | ||
import type { PageStateHistory } from './pageStateHistory' | ||
import { resetPageStates, startPageStateHistory, addPageState, PageState } from './pageStateHistory' | ||
import { startPageStateHistory, PageState } from './pageStateHistory' | ||
|
||
describe('pageStateHistory', () => { | ||
let pageStateHistory: PageStateHistory | ||
let setupBuilder: TestSetupBuilder | ||
|
||
let clock: Clock | ||
beforeEach(() => { | ||
setupBuilder = setup() | ||
.withFakeClock() | ||
.beforeBuild(() => { | ||
pageStateHistory = startPageStateHistory() | ||
return pageStateHistory | ||
}) | ||
clock = mockClock() | ||
pageStateHistory = startPageStateHistory() | ||
}) | ||
|
||
afterEach(() => { | ||
setupBuilder.cleanup() | ||
resetPageStates() | ||
resetExperimentalFeatures() | ||
pageStateHistory.stop() | ||
clock.cleanup() | ||
}) | ||
|
||
it('should have the current state when starting', () => { | ||
setupBuilder.build() | ||
expect(pageStateHistory.findAll(0 as RelativeTime, 10 as RelativeTime)).toBeDefined() | ||
}) | ||
|
||
it('should return undefined if the time period is out of history bounds', () => { | ||
pageStateHistory = startPageStateHistory() | ||
expect(pageStateHistory.findAll(-10 as RelativeTime, 0 as RelativeTime)).not.toBeDefined() | ||
}) | ||
|
||
it('should return the correct page states for the given time period', () => { | ||
const { clock } = setupBuilder.build() | ||
resetPageStates() | ||
|
||
addPageState(PageState.ACTIVE) | ||
pageStateHistory.addPageState(PageState.ACTIVE) | ||
|
||
clock.tick(10) | ||
addPageState(PageState.PASSIVE) | ||
pageStateHistory.addPageState(PageState.PASSIVE) | ||
|
||
clock.tick(10) | ||
addPageState(PageState.HIDDEN) | ||
pageStateHistory.addPageState(PageState.HIDDEN) | ||
|
||
clock.tick(10) | ||
addPageState(PageState.FROZEN) | ||
pageStateHistory.addPageState(PageState.FROZEN) | ||
|
||
clock.tick(10) | ||
addPageState(PageState.TERMINATED) | ||
|
||
expect(pageStateHistory.findAll(15 as RelativeTime, 20 as RelativeTime)).toEqual([ | ||
pageStateHistory.addPageState(PageState.TERMINATED) | ||
|
||
/* | ||
page state time 0 10 20 30 40 | ||
event time 15<-------->35 | ||
*/ | ||
const event = { | ||
startTime: 15 as RelativeTime, | ||
duration: 20 as RelativeTime, | ||
} | ||
expect(pageStateHistory.findAll(event.startTime, event.duration)).toEqual([ | ||
{ | ||
state: PageState.PASSIVE, | ||
startTime: 10 as RelativeTime, | ||
start: -5000000 as ServerDuration, | ||
}, | ||
{ | ||
state: PageState.HIDDEN, | ||
startTime: 20 as RelativeTime, | ||
start: 5000000 as ServerDuration, | ||
}, | ||
{ | ||
state: PageState.FROZEN, | ||
startTime: 30 as RelativeTime, | ||
start: 15000000 as ServerDuration, | ||
}, | ||
]) | ||
}) | ||
|
||
it('should limit the history entry number', () => { | ||
const limit = 1 | ||
const { clock } = setupBuilder.build() | ||
resetPageStates() | ||
|
||
clock.tick(10) | ||
addPageState(PageState.ACTIVE, limit) | ||
it('should limit the number of selectable entries', () => { | ||
const maxPageStateEntriesSelectable = 1 | ||
pageStateHistory = startPageStateHistory(maxPageStateEntriesSelectable) | ||
|
||
pageStateHistory.addPageState(PageState.ACTIVE) | ||
clock.tick(10) | ||
addPageState(PageState.PASSIVE, limit) | ||
pageStateHistory.addPageState(PageState.PASSIVE) | ||
|
||
clock.tick(10) | ||
addPageState(PageState.HIDDEN, limit) | ||
|
||
expect(pageStateHistory.findAll(0 as RelativeTime, 40 as RelativeTime)).toEqual([ | ||
{ | ||
state: PageState.HIDDEN, | ||
startTime: 30 as RelativeTime, | ||
}, | ||
]) | ||
expect(pageStateHistory.findAll(0 as RelativeTime, Infinity as RelativeTime)?.length).toEqual( | ||
maxPageStateEntriesSelectable | ||
) | ||
}) | ||
}) |
Oops, something went wrong.