-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathfeatureFlagContext.spec.ts
More file actions
193 lines (164 loc) · 7.13 KB
/
featureFlagContext.spec.ts
File metadata and controls
193 lines (164 loc) · 7.13 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import type { RelativeTime } from '@datadog/browser-core'
import { HookNames, relativeToClocks } from '@datadog/browser-core'
import type { Clock } from '@datadog/browser-core/test'
import { mockClock } from '@datadog/browser-core/test'
import { LifeCycle, LifeCycleEventType } from '../lifeCycle'
import type { ViewCreatedEvent, ViewEndedEvent } from '../view/trackViews'
import type { RumConfiguration } from '../configuration'
import { RumEventType } from '../../rawRumEvent.types'
import type { AssembleHookParams, Hooks } from '../hooks'
import { createHooks } from '../hooks'
import type { FeatureFlagContexts } from './featureFlagContext'
import { startFeatureFlagContexts } from './featureFlagContext'
describe('featureFlagContexts', () => {
const lifeCycle = new LifeCycle()
let clock: Clock
let featureFlagContexts: FeatureFlagContexts
let hooks: Hooks
let trackFeatureFlagsForEvents: any[]
beforeEach(() => {
clock = mockClock()
hooks = createHooks()
trackFeatureFlagsForEvents = []
featureFlagContexts = startFeatureFlagContexts(lifeCycle, hooks, {
trackFeatureFlagsForEvents,
} as unknown as RumConfiguration)
})
describe('assemble hook', () => {
it('should add feature flag evaluations on VIEW and ERROR by default ', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
featureFlagContexts.addFeatureFlagEvaluation('feature', 'foo')
const defaultViewAttributes = hooks.triggerHook(HookNames.Assemble, {
eventType: 'view',
startTime: 0 as RelativeTime,
} as AssembleHookParams)
const defaultErrorAttributes = hooks.triggerHook(HookNames.Assemble, {
eventType: 'error',
startTime: 0 as RelativeTime,
} as AssembleHookParams)
expect(defaultViewAttributes).toEqual({
type: 'view',
feature_flags: {
feature: 'foo',
},
})
expect(defaultErrorAttributes).toEqual({
type: 'error',
feature_flags: {
feature: 'foo',
},
})
})
it('should add feature flag evaluations on VIEW_UPDATE by default', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
featureFlagContexts.addFeatureFlagEvaluation('feature', 'foo')
const defaultViewUpdateAttributes = hooks.triggerHook(HookNames.Assemble, {
eventType: 'view_update',
startTime: 0 as RelativeTime,
} as AssembleHookParams)
expect(defaultViewUpdateAttributes).toEqual({
type: 'view_update',
feature_flags: {
feature: 'foo',
},
})
})
;[RumEventType.VITAL, RumEventType.ACTION, RumEventType.LONG_TASK, RumEventType.RESOURCE].forEach((eventType) => {
it(`should add feature flag evaluations on ${eventType} when specified in trackFeatureFlagsForEvents`, () => {
trackFeatureFlagsForEvents.push(eventType)
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
featureFlagContexts.addFeatureFlagEvaluation('feature', 'foo')
const defaultRumEventAttributes = hooks.triggerHook(HookNames.Assemble, {
eventType,
startTime: 0 as RelativeTime,
} as AssembleHookParams)
expect(defaultRumEventAttributes).toEqual({
type: eventType,
feature_flags: {
feature: 'foo',
},
})
})
})
it('should add feature flag evaluations of any type', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
featureFlagContexts.addFeatureFlagEvaluation('feature', 'foo')
featureFlagContexts.addFeatureFlagEvaluation('feature2', 2)
featureFlagContexts.addFeatureFlagEvaluation('feature3', true)
featureFlagContexts.addFeatureFlagEvaluation('feature4', { foo: 'bar' })
const defaultRumEventAttributes = hooks.triggerHook(HookNames.Assemble, {
eventType: 'view',
startTime: 0 as RelativeTime,
} as AssembleHookParams)
expect(defaultRumEventAttributes).toEqual({
type: 'view',
feature_flags: {
feature: 'foo',
feature2: 2,
feature3: true,
feature4: { foo: 'bar' },
},
})
})
it('should add feature flag evaluations corresponding to the view start time', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
clock.tick(10)
featureFlagContexts.addFeatureFlagEvaluation('feature', 'one')
lifeCycle.notify(LifeCycleEventType.AFTER_VIEW_ENDED, {
endClocks: relativeToClocks(10 as RelativeTime),
} as ViewEndedEvent)
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(10 as RelativeTime),
} as ViewCreatedEvent)
clock.tick(10)
featureFlagContexts.addFeatureFlagEvaluation('feature', 'two')
const defaultEventOneAttributes = hooks.triggerHook(HookNames.Assemble, {
eventType: 'view',
startTime: 5 as RelativeTime,
} as AssembleHookParams)
const defaultEventTwoAttributes = hooks.triggerHook(HookNames.Assemble, {
eventType: 'view',
startTime: 15 as RelativeTime,
} as AssembleHookParams)
expect(defaultEventOneAttributes).toEqual({ type: 'view', feature_flags: { feature: 'one' } })
expect(defaultEventTwoAttributes).toEqual({ type: 'view', feature_flags: { feature: 'two' } })
})
/**
* It could happen if there is an event happening just between view end and view creation
* (which seems unlikely) and this event would anyway be rejected by lack of view id
*/
it('should not add feature flag evaluations when no current view', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
const defaultRumEventAttributes = hooks.triggerHook(HookNames.Assemble, {
eventType: 'view',
startTime: 0 as RelativeTime,
} as AssembleHookParams)
expect(defaultRumEventAttributes).toBeUndefined()
})
it('should replace existing feature flag evaluations for the current view', () => {
lifeCycle.notify(LifeCycleEventType.BEFORE_VIEW_CREATED, {
startClocks: relativeToClocks(0 as RelativeTime),
} as ViewCreatedEvent)
featureFlagContexts.addFeatureFlagEvaluation('feature', 'foo')
featureFlagContexts.addFeatureFlagEvaluation('feature2', 'baz')
featureFlagContexts.addFeatureFlagEvaluation('feature', 'bar')
const defaultRumEventAttributes = hooks.triggerHook(HookNames.Assemble, {
eventType: 'view',
startTime: 0 as RelativeTime,
} as AssembleHookParams)
expect(defaultRumEventAttributes).toEqual({ type: 'view', feature_flags: { feature: 'bar', feature2: 'baz' } })
})
})
})