-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathpreStartRum.spec.ts
More file actions
878 lines (747 loc) · 29.4 KB
/
preStartRum.spec.ts
File metadata and controls
878 lines (747 loc) · 29.4 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
import type { DeflateWorker, Duration, TimeStamp, TrackingConsentState } from '@datadog/browser-core'
import {
display,
getTimeStamp,
noop,
relativeToClocks,
clocksNow,
TrackingConsent,
createTrackingConsentState,
DefaultPrivacyLevel,
resetExperimentalFeatures,
resetFetchObservable,
ExperimentalFeature,
} from '@datadog/browser-core'
import type { Clock } from '@datadog/browser-core/test'
import {
callbackAddsInstrumentation,
interceptRequests,
mockClock,
mockEventBridge,
mockSyntheticsWorkerValues,
mockExperimentalFeatures,
} from '@datadog/browser-core/test'
import type { HybridInitConfiguration, RumConfiguration, RumInitConfiguration } from '../domain/configuration'
import type { ViewOptions } from '../domain/view/trackViews'
import { ActionType, VitalType } from '../rawRumEvent.types'
import type { RumPlugin } from '../domain/plugins'
import { createCustomVitalsState } from '../domain/vital/vitalCollection'
import type { ManualAction } from '../domain/action/trackManualActions'
import type { RumPublicApi, Strategy } from './rumPublicApi'
import type { StartRumResult } from './startRum'
import { createPreStartStrategy } from './preStartRum'
const DEFAULT_INIT_CONFIGURATION = { applicationId: 'xxx', clientToken: 'xxx' }
const INVALID_INIT_CONFIGURATION = { clientToken: 'yes' } as RumInitConfiguration
const AUTO_CONFIGURATION = { ...DEFAULT_INIT_CONFIGURATION }
const MANUAL_CONFIGURATION = { ...AUTO_CONFIGURATION, trackViewsManually: true }
const FAKE_WORKER = {} as DeflateWorker
const PUBLIC_API = {} as RumPublicApi
describe('preStartRum', () => {
let doStartRumSpy: jasmine.Spy<
(
configuration: RumConfiguration,
deflateWorker: DeflateWorker | undefined,
initialViewOptions?: ViewOptions
) => StartRumResult
>
beforeEach(() => {
doStartRumSpy = jasmine.createSpy()
})
afterEach(() => {
resetFetchObservable()
})
describe('configuration validation', () => {
let strategy: Strategy
let displaySpy: jasmine.Spy
beforeEach(() => {
displaySpy = spyOn(display, 'error')
strategy = createPreStartStrategy({}, createTrackingConsentState(), createCustomVitalsState(), doStartRumSpy)
})
it('should start when the configuration is valid', () => {
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(displaySpy).not.toHaveBeenCalled()
expect(doStartRumSpy).toHaveBeenCalled()
})
it('should not start when the configuration is missing', () => {
;(strategy.init as () => void)()
expect(displaySpy).toHaveBeenCalled()
expect(doStartRumSpy).not.toHaveBeenCalled()
})
it('should not start when the configuration is invalid', () => {
strategy.init(INVALID_INIT_CONFIGURATION, PUBLIC_API)
expect(displaySpy).toHaveBeenCalled()
expect(doStartRumSpy).not.toHaveBeenCalled()
})
describe('multiple init', () => {
it('should log an error if init is called several times', () => {
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(displaySpy).toHaveBeenCalledTimes(0)
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(displaySpy).toHaveBeenCalledTimes(1)
})
it('should not log an error if init is called several times and silentMultipleInit is true', () => {
strategy.init(
{
...DEFAULT_INIT_CONFIGURATION,
silentMultipleInit: true,
},
PUBLIC_API
)
expect(displaySpy).toHaveBeenCalledTimes(0)
strategy.init(
{
...DEFAULT_INIT_CONFIGURATION,
silentMultipleInit: true,
},
PUBLIC_API
)
expect(displaySpy).toHaveBeenCalledTimes(0)
})
})
describe('if event bridge present', () => {
it('init should accept empty application id and client token', () => {
mockEventBridge()
const hybridInitConfiguration: HybridInitConfiguration = {}
strategy.init(hybridInitConfiguration as RumInitConfiguration, PUBLIC_API)
expect(display.error).not.toHaveBeenCalled()
})
it('should force session sample rate to 100', () => {
mockEventBridge()
const invalidConfiguration: HybridInitConfiguration = { sessionSampleRate: 50 }
strategy.init(invalidConfiguration as RumInitConfiguration, PUBLIC_API)
expect(strategy.initConfiguration?.sessionSampleRate).toEqual(100)
})
it('should set the default privacy level received from the bridge if the not provided in the init configuration', () => {
mockEventBridge({ privacyLevel: DefaultPrivacyLevel.ALLOW })
const hybridInitConfiguration: HybridInitConfiguration = {}
strategy.init(hybridInitConfiguration as RumInitConfiguration, PUBLIC_API)
expect((strategy.initConfiguration as RumInitConfiguration)?.defaultPrivacyLevel).toEqual(
DefaultPrivacyLevel.ALLOW
)
})
it('should set the default privacy level from the init configuration if provided', () => {
mockEventBridge({ privacyLevel: DefaultPrivacyLevel.ALLOW })
const hybridInitConfiguration: HybridInitConfiguration = { defaultPrivacyLevel: DefaultPrivacyLevel.MASK }
strategy.init(hybridInitConfiguration as RumInitConfiguration, PUBLIC_API)
expect((strategy.initConfiguration as RumInitConfiguration)?.defaultPrivacyLevel).toEqual(
hybridInitConfiguration.defaultPrivacyLevel
)
})
it('should set the default privacy level to "mask" if not provided in init configuration nor the bridge', () => {
mockEventBridge({ privacyLevel: undefined })
const hybridInitConfiguration: HybridInitConfiguration = {}
strategy.init(hybridInitConfiguration as RumInitConfiguration, PUBLIC_API)
expect((strategy.initConfiguration as RumInitConfiguration)?.defaultPrivacyLevel).toEqual(
DefaultPrivacyLevel.MASK
)
})
it('should initialize even if session cannot be handled', () => {
mockEventBridge()
spyOnProperty(document, 'cookie', 'get').and.returnValue('')
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(doStartRumSpy).toHaveBeenCalled()
})
})
})
describe('init', () => {
it('should not initialize if session cannot be handled and bridge is not present', () => {
spyOnProperty(document, 'cookie', 'get').and.returnValue('')
const displaySpy = spyOn(display, 'warn')
const strategy = createPreStartStrategy(
{},
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(doStartRumSpy).not.toHaveBeenCalled()
expect(displaySpy).toHaveBeenCalled()
})
describe('skipInitIfSyntheticsWillInjectRum option', () => {
it('when true, ignores init() call if Synthetics will inject its own instance of RUM', () => {
mockSyntheticsWorkerValues({ injectsRum: true })
const strategy = createPreStartStrategy(
{
ignoreInitIfSyntheticsWillInjectRum: true,
},
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(doStartRumSpy).not.toHaveBeenCalled()
})
it('when undefined, ignores init() call if Synthetics will inject its own instance of RUM', () => {
mockSyntheticsWorkerValues({ injectsRum: true })
const strategy = createPreStartStrategy(
{},
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(doStartRumSpy).not.toHaveBeenCalled()
})
it('when false, does not ignore init() call even if Synthetics will inject its own instance of RUM', () => {
mockSyntheticsWorkerValues({ injectsRum: true })
const strategy = createPreStartStrategy(
{
ignoreInitIfSyntheticsWillInjectRum: false,
},
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(doStartRumSpy).toHaveBeenCalled()
})
})
describe('deflate worker', () => {
let strategy: Strategy
let startDeflateWorkerSpy: jasmine.Spy
beforeEach(() => {
startDeflateWorkerSpy = jasmine.createSpy().and.returnValue(FAKE_WORKER)
strategy = createPreStartStrategy(
{
startDeflateWorker: startDeflateWorkerSpy,
createDeflateEncoder: noop as any,
},
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
})
describe('with compressIntakeRequests: false', () => {
it('does not create a deflate worker', () => {
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(startDeflateWorkerSpy).not.toHaveBeenCalled()
const worker: DeflateWorker | undefined = doStartRumSpy.calls.mostRecent().args[1]
expect(worker).toBeUndefined()
})
})
describe('with compressIntakeRequests: true', () => {
it('creates a deflate worker instance', () => {
strategy.init(
{
...DEFAULT_INIT_CONFIGURATION,
compressIntakeRequests: true,
},
PUBLIC_API
)
expect(startDeflateWorkerSpy).toHaveBeenCalledTimes(1)
const worker: DeflateWorker | undefined = doStartRumSpy.calls.mostRecent().args[1]
expect(worker).toBeDefined()
})
it('aborts the initialization if it fails to create a deflate worker', () => {
startDeflateWorkerSpy.and.returnValue(undefined)
strategy.init(
{
...DEFAULT_INIT_CONFIGURATION,
compressIntakeRequests: true,
},
PUBLIC_API
)
expect(doStartRumSpy).not.toHaveBeenCalled()
})
it('if message bridge is present, does not create a deflate worker instance', () => {
mockEventBridge()
strategy.init(
{
...DEFAULT_INIT_CONFIGURATION,
compressIntakeRequests: true,
},
PUBLIC_API
)
expect(startDeflateWorkerSpy).not.toHaveBeenCalled()
expect(doStartRumSpy).toHaveBeenCalledTimes(1)
})
})
})
describe('trackViews mode', () => {
let clock: Clock | undefined
let strategy: Strategy
let startViewSpy: jasmine.Spy<StartRumResult['startView']>
let addTimingSpy: jasmine.Spy<StartRumResult['addTiming']>
let setViewNameSpy: jasmine.Spy<StartRumResult['setViewName']>
beforeEach(() => {
startViewSpy = jasmine.createSpy('startView')
addTimingSpy = jasmine.createSpy('addTiming')
setViewNameSpy = jasmine.createSpy('setViewName')
doStartRumSpy.and.returnValue({
startView: startViewSpy,
addTiming: addTimingSpy,
setViewName: setViewNameSpy,
} as unknown as StartRumResult)
strategy = createPreStartStrategy({}, createTrackingConsentState(), createCustomVitalsState(), doStartRumSpy)
})
describe('when auto', () => {
it('should start rum at init', () => {
strategy.init(AUTO_CONFIGURATION, PUBLIC_API)
expect(doStartRumSpy).toHaveBeenCalled()
})
it('before init startView should be handled after init', () => {
clock = mockClock()
clock.tick(10)
strategy.startView({ name: 'foo' })
expect(startViewSpy).not.toHaveBeenCalled()
clock.tick(20)
strategy.init(AUTO_CONFIGURATION, PUBLIC_API)
expect(startViewSpy).toHaveBeenCalled()
expect(startViewSpy.calls.argsFor(0)[0]).toEqual({ name: 'foo' })
expect(startViewSpy.calls.argsFor(0)[1]).toEqual({
relative: clock.relative(10),
timeStamp: clock.timeStamp(10),
})
})
})
describe('when views are tracked manually', () => {
it('should not start rum at init', () => {
strategy.init(MANUAL_CONFIGURATION, PUBLIC_API)
expect(doStartRumSpy).not.toHaveBeenCalled()
})
it('calling startView then init should start rum', () => {
strategy.startView({ name: 'foo' })
expect(doStartRumSpy).not.toHaveBeenCalled()
expect(startViewSpy).not.toHaveBeenCalled()
strategy.init(MANUAL_CONFIGURATION, PUBLIC_API)
expect(doStartRumSpy).toHaveBeenCalled()
const initialViewOptions: ViewOptions | undefined = doStartRumSpy.calls.argsFor(0)[2]
expect(initialViewOptions).toEqual({ name: 'foo' })
expect(startViewSpy).not.toHaveBeenCalled()
})
it('calling startView then init does not start rum if tracking consent is not granted', () => {
const strategy = createPreStartStrategy(
{},
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
strategy.startView({ name: 'foo' })
strategy.init(
{
...MANUAL_CONFIGURATION,
trackingConsent: TrackingConsent.NOT_GRANTED,
},
PUBLIC_API
)
expect(doStartRumSpy).not.toHaveBeenCalled()
})
it('calling startView twice before init should start rum and create a new view', () => {
clock = mockClock()
clock.tick(10)
strategy.startView({ name: 'foo' })
clock.tick(10)
strategy.startView({ name: 'bar' })
clock.tick(10)
strategy.init(MANUAL_CONFIGURATION, PUBLIC_API)
expect(doStartRumSpy).toHaveBeenCalled()
const initialViewOptions: ViewOptions | undefined = doStartRumSpy.calls.argsFor(0)[2]
expect(initialViewOptions).toEqual({ name: 'foo' })
expect(startViewSpy).toHaveBeenCalledOnceWith({ name: 'bar' }, relativeToClocks(clock.relative(20)))
})
it('calling init then startView should start rum', () => {
strategy.init(MANUAL_CONFIGURATION, PUBLIC_API)
expect(doStartRumSpy).not.toHaveBeenCalled()
expect(startViewSpy).not.toHaveBeenCalled()
strategy.startView({ name: 'foo' })
expect(doStartRumSpy).toHaveBeenCalled()
const initialViewOptions: ViewOptions | undefined = doStartRumSpy.calls.argsFor(0)[2]
expect(initialViewOptions).toEqual({ name: 'foo' })
expect(startViewSpy).not.toHaveBeenCalled()
})
it('API calls should be handled in order', () => {
clock = mockClock()
clock.tick(10)
strategy.addTiming('first')
clock.tick(10)
strategy.startView({ name: 'foo' })
clock.tick(10)
strategy.addTiming('second')
clock.tick(10)
strategy.init(MANUAL_CONFIGURATION, PUBLIC_API)
expect(addTimingSpy).toHaveBeenCalledTimes(2)
expect(addTimingSpy.calls.argsFor(0)[0]).toEqual('first')
expect(addTimingSpy.calls.argsFor(0)[1]).toEqual(getTimeStamp(clock.relative(10)))
expect(addTimingSpy.calls.argsFor(1)[0]).toEqual('second')
expect(addTimingSpy.calls.argsFor(1)[1]).toEqual(getTimeStamp(clock.relative(30)))
})
})
})
describe('remote configuration', () => {
let interceptor: ReturnType<typeof interceptRequests>
beforeEach(() => {
interceptor = interceptRequests()
})
it('should start with the remote configuration when a remoteConfigurationId is provided', (done) => {
interceptor.withFetch(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve({ rum: { sessionSampleRate: 50 } }),
})
)
const strategy = createPreStartStrategy(
{},
createTrackingConsentState(),
createCustomVitalsState(),
(configuration) => {
expect(configuration.sessionSampleRate).toEqual(50)
done()
return {} as StartRumResult
}
)
strategy.init(
{
...DEFAULT_INIT_CONFIGURATION,
remoteConfigurationId: '123',
},
PUBLIC_API
)
})
})
describe('plugins', () => {
it('calls the onInit method on provided plugins', () => {
const plugin = { name: 'a', onInit: jasmine.createSpy() }
const strategy = createPreStartStrategy(
{},
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
const initConfiguration: RumInitConfiguration = { ...DEFAULT_INIT_CONFIGURATION, plugins: [plugin] }
strategy.init(initConfiguration, PUBLIC_API)
expect(plugin.onInit).toHaveBeenCalledWith({
initConfiguration,
publicApi: PUBLIC_API,
})
})
it('plugins can edit the init configuration prior to validation', () => {
const plugin: RumPlugin = {
name: 'a',
onInit: ({ initConfiguration }) => {
initConfiguration.clientToken = 'client-token'
initConfiguration.applicationId = 'application-id'
},
}
const strategy = createPreStartStrategy(
{},
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
strategy.init(
{
plugins: [plugin],
} as RumInitConfiguration,
PUBLIC_API
)
expect(doStartRumSpy).toHaveBeenCalled()
expect(doStartRumSpy.calls.mostRecent().args[0].applicationId).toBe('application-id')
})
})
})
describe('getInternalContext', () => {
it('returns undefined', () => {
const strategy = createPreStartStrategy(
{},
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
expect(strategy.getInternalContext()).toBe(undefined)
})
})
describe('getViewContext', () => {
it('returns empty object', () => {
const strategy = createPreStartStrategy(
{},
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
expect(strategy.getViewContext()).toEqual({})
})
})
describe('stopSession', () => {
it('does not buffer the call before starting RUM', () => {
const strategy = createPreStartStrategy(
{},
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
const stopSessionSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({ stopSession: stopSessionSpy } as unknown as StartRumResult)
strategy.stopSession()
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(stopSessionSpy).not.toHaveBeenCalled()
})
})
describe('initConfiguration', () => {
let strategy: Strategy
let initConfiguration: RumInitConfiguration
let interceptor: ReturnType<typeof interceptRequests>
beforeEach(() => {
interceptor = interceptRequests()
strategy = createPreStartStrategy({}, createTrackingConsentState(), createCustomVitalsState(), doStartRumSpy)
initConfiguration = { ...DEFAULT_INIT_CONFIGURATION, service: 'my-service', version: '1.4.2', env: 'dev' }
})
afterEach(() => {
resetExperimentalFeatures()
})
it('is undefined before init', () => {
expect(strategy.initConfiguration).toBe(undefined)
})
it('returns the user configuration after init', () => {
strategy.init(initConfiguration, PUBLIC_API)
expect(strategy.initConfiguration).toEqual(initConfiguration)
})
it('returns the user configuration even if skipInitIfSyntheticsWillInjectRum is true', () => {
mockSyntheticsWorkerValues({ injectsRum: true })
const strategy = createPreStartStrategy(
{
ignoreInitIfSyntheticsWillInjectRum: true,
},
createTrackingConsentState(),
createCustomVitalsState(),
doStartRumSpy
)
strategy.init(initConfiguration, PUBLIC_API)
expect(strategy.initConfiguration).toEqual(initConfiguration)
})
it('returns the initConfiguration with the remote configuration when a remoteConfigurationId is provided', (done) => {
interceptor.withFetch(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve({ rum: { sessionSampleRate: 50 } }),
})
)
const strategy = createPreStartStrategy({}, createTrackingConsentState(), createCustomVitalsState(), () => {
expect(strategy.initConfiguration?.sessionSampleRate).toEqual(50)
done()
return {} as StartRumResult
})
strategy.init(
{
...DEFAULT_INIT_CONFIGURATION,
remoteConfigurationId: '123',
},
PUBLIC_API
)
})
})
describe('buffers API calls before starting RUM', () => {
let strategy: Strategy
beforeEach(() => {
strategy = createPreStartStrategy({}, createTrackingConsentState(), createCustomVitalsState(), doStartRumSpy)
})
it('addAction', () => {
const addActionSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({ addAction: addActionSpy } as unknown as StartRumResult)
const manualAction: Omit<ManualAction, 'id' | 'duration' | 'counts'> = {
name: 'foo',
type: ActionType.CUSTOM,
startClocks: clocksNow(),
}
strategy.addAction(manualAction)
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(addActionSpy).toHaveBeenCalledOnceWith(manualAction)
})
it('addError', () => {
const addErrorSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({ addError: addErrorSpy } as unknown as StartRumResult)
const error = {
error: new Error('foo'),
handlingStack: '',
context: {},
startClocks: clocksNow(),
}
strategy.addError(error)
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(addErrorSpy).toHaveBeenCalledOnceWith(error)
})
it('startView', () => {
const startViewSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({ startView: startViewSpy } as unknown as StartRumResult)
const options = { name: 'foo' }
const clockState = clocksNow()
strategy.startView(options, clockState)
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(startViewSpy).toHaveBeenCalledOnceWith(options, clockState)
})
it('addTiming', () => {
const addTimingSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({ addTiming: addTimingSpy } as unknown as StartRumResult)
const name = 'foo'
const time = 123 as TimeStamp
strategy.addTiming(name, time)
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(addTimingSpy).toHaveBeenCalledOnceWith(name, time)
})
it('setViewContext', () => {
const setViewContextSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({ setViewContext: setViewContextSpy } as unknown as StartRumResult)
strategy.setViewContext({ foo: 'bar' })
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(setViewContextSpy).toHaveBeenCalledOnceWith({ foo: 'bar' })
})
it('setViewContextProperty', () => {
const setViewContextPropertySpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({ setViewContextProperty: setViewContextPropertySpy } as unknown as StartRumResult)
strategy.setViewContextProperty('foo', 'bar')
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(setViewContextPropertySpy).toHaveBeenCalledOnceWith('foo', 'bar')
})
it('setViewName', () => {
const setViewNameSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({ setViewName: setViewNameSpy } as unknown as StartRumResult)
const name = 'foo'
strategy.setViewName(name)
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(setViewNameSpy).toHaveBeenCalledOnceWith(name)
})
it('addFeatureFlagEvaluation', () => {
const addFeatureFlagEvaluationSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({
addFeatureFlagEvaluation: addFeatureFlagEvaluationSpy,
} as unknown as StartRumResult)
const key = 'foo'
const value = 'bar'
strategy.addFeatureFlagEvaluation(key, value)
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(addFeatureFlagEvaluationSpy).toHaveBeenCalledOnceWith(key, value)
})
it('startDurationVital', () => {
const addDurationVitalSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({
addDurationVital: addDurationVitalSpy,
} as unknown as StartRumResult)
strategy.startDurationVital('timing')
strategy.stopDurationVital('timing')
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(addDurationVitalSpy).toHaveBeenCalled()
})
it('addDurationVital', () => {
const addDurationVitalSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({
addDurationVital: addDurationVitalSpy,
} as unknown as StartRumResult)
const vitalAdd = { name: 'timing', type: VitalType.DURATION, startClocks: clocksNow(), duration: 100 as Duration }
strategy.addDurationVital(vitalAdd)
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(addDurationVitalSpy).toHaveBeenCalledOnceWith(vitalAdd)
})
it('addOperationStepVital', () => {
const addOperationStepVitalSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({
addOperationStepVital: addOperationStepVitalSpy,
} as unknown as StartRumResult)
strategy.addOperationStepVital('foo', 'start')
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(addOperationStepVitalSpy).toHaveBeenCalledOnceWith('foo', 'start', undefined, undefined)
})
it('startAction / stopAction', () => {
mockExperimentalFeatures([ExperimentalFeature.START_STOP_ACTION])
const startActionSpy = jasmine.createSpy()
const stopActionSpy = jasmine.createSpy()
doStartRumSpy.and.returnValue({
startAction: startActionSpy,
stopAction: stopActionSpy,
} as unknown as StartRumResult)
strategy.startAction('user_login', { type: ActionType.CUSTOM })
strategy.stopAction('user_login')
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
expect(startActionSpy).toHaveBeenCalledWith(
'user_login',
jasmine.objectContaining({
type: ActionType.CUSTOM,
}),
jasmine.objectContaining({
relative: jasmine.any(Number),
timeStamp: jasmine.any(Number),
})
)
expect(stopActionSpy).toHaveBeenCalledWith(
'user_login',
undefined,
jasmine.objectContaining({
relative: jasmine.any(Number),
timeStamp: jasmine.any(Number),
})
)
})
})
describe('tracking consent', () => {
let strategy: Strategy
let trackingConsentState: TrackingConsentState
beforeEach(() => {
trackingConsentState = createTrackingConsentState()
strategy = createPreStartStrategy({}, trackingConsentState, createCustomVitalsState(), doStartRumSpy)
})
describe('basic methods instrumentation', () => {
it('should instrument fetch even if tracking consent is not granted', () => {
expect(
callbackAddsInstrumentation(() => {
strategy.init(
{
...DEFAULT_INIT_CONFIGURATION,
trackingConsent: TrackingConsent.NOT_GRANTED,
},
PUBLIC_API
)
})
.toMethod(window, 'fetch')
.whenCalled()
).toBeTrue()
})
})
it('does not start rum if tracking consent is not granted at init', () => {
strategy.init(
{
...DEFAULT_INIT_CONFIGURATION,
trackingConsent: TrackingConsent.NOT_GRANTED,
},
PUBLIC_API
)
expect(doStartRumSpy).not.toHaveBeenCalled()
})
it('starts rum if tracking consent is granted before init', () => {
trackingConsentState.update(TrackingConsent.GRANTED)
strategy.init(
{
...DEFAULT_INIT_CONFIGURATION,
trackingConsent: TrackingConsent.NOT_GRANTED,
},
PUBLIC_API
)
expect(doStartRumSpy).toHaveBeenCalledTimes(1)
})
it('does not start rum if tracking consent is withdrawn before init', () => {
trackingConsentState.update(TrackingConsent.NOT_GRANTED)
strategy.init(
{
...DEFAULT_INIT_CONFIGURATION,
trackingConsent: TrackingConsent.GRANTED,
},
PUBLIC_API
)
expect(doStartRumSpy).not.toHaveBeenCalled()
})
it('does not start rum if no view is started', () => {
trackingConsentState.update(TrackingConsent.GRANTED)
strategy.init(
{
...MANUAL_CONFIGURATION,
trackingConsent: TrackingConsent.NOT_GRANTED,
},
PUBLIC_API
)
expect(doStartRumSpy).not.toHaveBeenCalled()
})
it('do not call startRum when tracking consent state is updated after init', () => {
strategy.init(DEFAULT_INIT_CONFIGURATION, PUBLIC_API)
doStartRumSpy.calls.reset()
trackingConsentState.update(TrackingConsent.GRANTED)
expect(doStartRumSpy).not.toHaveBeenCalled()
})
})
})