-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathsetupViewTest.specHelper.ts
More file actions
102 lines (89 loc) · 2.8 KB
/
Copy pathsetupViewTest.specHelper.ts
File metadata and controls
102 lines (89 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { Observable } from '@datadog/browser-core'
import { deepClone } from '@datadog/js-core/util'
import { registerCleanupTask } from '@datadog/browser-core/test'
import { mockRumConfiguration, setupLocationObserver } from '../../../test'
import { LifeCycle, LifeCycleEventType } from '../lifeCycle'
import type { RumConfiguration } from '../configuration'
import type { RumMutationRecord } from '../../browser/domMutationObservable'
import type { ViewCreatedEvent, ViewEvent, ViewOptions, ViewEndedEvent } from './trackViews'
import { trackViews } from './trackViews'
export type ViewTest = ReturnType<typeof setupViewTest>
interface ViewTestOptions {
initialLocation?: string
partialConfig?: Partial<RumConfiguration>
initialViewOptions?: ViewOptions
}
export function setupViewTest({ initialLocation, partialConfig, initialViewOptions }: ViewTestOptions = {}) {
const lifeCycle = new LifeCycle()
const domMutationObservable = new Observable<RumMutationRecord[]>()
const windowOpenObservable = new Observable<void>()
const configuration = mockRumConfiguration(partialConfig)
const { locationChangeObservable, changeLocation } = setupLocationObserver(initialLocation)
const {
handler: viewUpdateHandler,
getViewEvent: getViewUpdate,
getHandledCount: getViewUpdateCount,
} = spyOnViews<ViewEvent>()
lifeCycle.subscribe(LifeCycleEventType.VIEW_UPDATED, viewUpdateHandler)
const {
handler: viewCreateHandler,
getViewEvent: getViewCreate,
getHandledCount: getViewCreateCount,
} = spyOnViews<ViewCreatedEvent>()
lifeCycle.subscribe(LifeCycleEventType.VIEW_CREATED, viewCreateHandler)
const {
handler: viewEndHandler,
getViewEvent: getViewEnd,
getHandledCount: getViewEndCount,
} = spyOnViews<ViewEndedEvent>()
lifeCycle.subscribe(LifeCycleEventType.VIEW_ENDED, viewEndHandler)
const {
stop,
startView,
setViewName,
setViewContext,
setViewContextProperty,
getViewContext,
addTiming,
setLoadingTime,
} = trackViews(
lifeCycle,
domMutationObservable,
windowOpenObservable,
configuration,
locationChangeObservable,
!configuration.trackViewsManually,
initialViewOptions
)
registerCleanupTask(stop)
return {
lifeCycle,
startView,
setViewContext,
setViewContextProperty,
getViewContext,
changeLocation,
setViewName,
addTiming,
setLoadingTime,
getViewUpdate,
getViewUpdateCount,
getViewCreate,
getViewCreateCount,
getViewEnd,
getViewEndCount,
}
}
function spyOnViews<Event>() {
const events: Event[] = []
return {
handler: (event: Event) => {
events.push(
// Some properties can be mutated later
deepClone(event)
)
},
getViewEvent: (index: number) => events[index],
getHandledCount: () => events.length,
}
}