-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathrumPublicApi.spec.ts
More file actions
1229 lines (1063 loc) · 40.1 KB
/
Copy pathrumPublicApi.spec.ts
File metadata and controls
1229 lines (1063 loc) · 40.1 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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type { RelativeTime, DeflateWorker, TimeStamp } from '@datadog/browser-core'
import {
ONE_SECOND,
display,
DefaultPrivacyLevel,
timeStampToClocks,
ResourceType,
startTelemetry,
startSessionManager,
getTimeStamp,
} from '@datadog/browser-core'
import type { Clock } from '@datadog/browser-core/test'
import {
collectAsyncCalls,
mockClock,
mockEventBridge,
createFakeTelemetryObject,
replaceMockable,
replaceMockableWithSpy,
createStartSessionManagerMock,
} from '@datadog/browser-core/test'
import { noopRecorderApi, noopProfilerApi } from '../../test'
import { ActionType, VitalType } from '../rawRumEvent.types'
import type { RumPublicApi, RecorderApi, ProfilerApi, RumPublicApiOptions } from './rumPublicApi'
import { makeRumPublicApi } from './rumPublicApi'
import type { StartRum } from './startRum'
import { startRum } from './startRum'
const noopStartRum = (): ReturnType<StartRum> => ({
addAction: () => undefined,
addError: () => undefined,
addEvent: () => undefined,
addTiming: () => undefined,
setLoadingTime: () => undefined,
addFeatureFlagEvaluation: () => undefined,
startView: () => undefined,
setViewContext: () => undefined,
setViewContextProperty: () => undefined,
setViewName: () => undefined,
getViewContext: () => ({}),
getInternalContext: () => undefined,
lifeCycle: {} as any,
viewHistory: {} as any,
sessionManager: {} as any,
stopSession: () => undefined,
startDurationVital: () => undefined,
stopDurationVital: () => undefined,
addDurationVital: () => undefined,
stop: () => undefined,
globalContext: {} as any,
userContext: {} as any,
accountContext: {} as any,
hooks: {} as any,
telemetry: {} as any,
addOperationStepVital: () => undefined,
startAction: () => undefined,
stopAction: () => undefined,
startResource: () => undefined,
stopResource: () => undefined,
})
const DEFAULT_INIT_CONFIGURATION = { applicationId: 'xxx', clientToken: 'xxx' }
const FAKE_WORKER = {} as DeflateWorker
describe('rum public api', () => {
describe('init', () => {
describe('deflate worker', () => {
let rumPublicApi: RumPublicApi
let recorderApiOnRumStartSpy: jasmine.Spy<RecorderApi['onRumStart']>
beforeEach(() => {
recorderApiOnRumStartSpy = jasmine.createSpy()
;({ rumPublicApi } = makeRumPublicApiWithDefaults({
recorderApi: {
onRumStart: recorderApiOnRumStartSpy,
},
rumPublicApiOptions: {
startDeflateWorker: () => FAKE_WORKER,
},
}))
})
it('passes addError to plugins on rum start', async () => {
const plugin = { name: 'test-plugin', onRumStart: jasmine.createSpy() }
rumPublicApi.init({
...DEFAULT_INIT_CONFIGURATION,
plugins: [plugin],
})
await collectAsyncCalls(plugin.onRumStart, 1)
expect(plugin.onRumStart).toHaveBeenCalledWith(
jasmine.objectContaining({
addError: jasmine.any(Function),
})
)
})
it('pass the worker to the recorder API', async () => {
rumPublicApi.init({
...DEFAULT_INIT_CONFIGURATION,
compressIntakeRequests: true,
})
await collectAsyncCalls(recorderApiOnRumStartSpy, 1)
expect(recorderApiOnRumStartSpy.calls.mostRecent().args[4]).toBe(FAKE_WORKER)
})
})
})
describe('getInternalContext', () => {
let getInternalContextSpy: jasmine.Spy<ReturnType<StartRum>['getInternalContext']>
let rumPublicApi: RumPublicApi
let startRumSpy: jasmine.Spy<StartRum>
beforeEach(() => {
getInternalContextSpy = jasmine.createSpy().and.callFake(() => ({
application_id: '123',
session_id: '123',
}))
;({ rumPublicApi, startRumSpy } = makeRumPublicApiWithDefaults({
startRumResult: {
getInternalContext: getInternalContextSpy,
},
}))
})
it('returns the internal context after init', async () => {
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
await collectAsyncCalls(startRumSpy, 1)
expect(rumPublicApi.getInternalContext()).toEqual({ application_id: '123', session_id: '123' })
expect(getInternalContextSpy).toHaveBeenCalled()
})
it('uses the startTime if specified', async () => {
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
await collectAsyncCalls(startRumSpy, 1)
const startTime = 234832890
expect(rumPublicApi.getInternalContext(startTime)).toEqual({ application_id: '123', session_id: '123' })
expect(getInternalContextSpy).toHaveBeenCalledWith(startTime)
})
})
describe('getInitConfiguration', () => {
it('clones the init configuration', () => {
const { rumPublicApi } = makeRumPublicApiWithDefaults()
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
expect(rumPublicApi.getInitConfiguration()).toEqual(DEFAULT_INIT_CONFIGURATION)
expect(rumPublicApi.getInitConfiguration()).not.toBe(DEFAULT_INIT_CONFIGURATION)
})
})
describe('addAction', () => {
let addActionSpy: jasmine.Spy<ReturnType<StartRum>['addAction']>
let rumPublicApi: RumPublicApi
beforeEach(() => {
mockEventBridge()
addActionSpy = jasmine.createSpy()
;({ rumPublicApi } = makeRumPublicApiWithDefaults({
startRumResult: {
addAction: addActionSpy,
},
}))
mockClock()
})
it('allows sending actions before init', async () => {
rumPublicApi.addAction('foo', { bar: 'baz' })
expect(addActionSpy).not.toHaveBeenCalled()
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
await collectAsyncCalls(addActionSpy, 1)
expect(addActionSpy).toHaveBeenCalledTimes(1)
expect(addActionSpy.calls.argsFor(0)).toEqual([
{
context: { bar: 'baz' },
name: 'foo',
startClocks: jasmine.any(Object),
type: ActionType.CUSTOM,
handlingStack: jasmine.any(String),
},
])
})
it('should generate a handling stack', async () => {
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
function triggerAction() {
rumPublicApi.addAction('foo', { bar: 'baz' })
}
triggerAction()
await collectAsyncCalls(addActionSpy, 1)
expect(addActionSpy).toHaveBeenCalledTimes(1)
const stacktrace = addActionSpy.calls.argsFor(0)[0].handlingStack
expect(stacktrace).toMatch(/^HandlingStack: action\s+at triggerAction @/)
})
})
describe('addError', () => {
let addErrorSpy: jasmine.Spy<ReturnType<StartRum>['addError']>
let rumPublicApi: RumPublicApi
let clock: Clock
beforeEach(() => {
mockEventBridge()
clock = mockClock()
addErrorSpy = jasmine.createSpy()
;({ rumPublicApi } = makeRumPublicApiWithDefaults({
startRumResult: {
addError: addErrorSpy,
},
}))
})
it('allows capturing an error before init', async () => {
rumPublicApi.addError(new Error('foo'))
expect(addErrorSpy).not.toHaveBeenCalled()
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
await collectAsyncCalls(addErrorSpy, 1)
expect(addErrorSpy).toHaveBeenCalledTimes(1)
expect(addErrorSpy.calls.argsFor(0)).toEqual([
{
context: undefined,
error: new Error('foo'),
handlingStack: jasmine.any(String),
startClocks: jasmine.any(Object),
},
])
})
it('should generate a handling stack', async () => {
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
function triggerError() {
rumPublicApi.addError(new Error('message'))
}
triggerError()
await collectAsyncCalls(addErrorSpy, 1)
expect(addErrorSpy).toHaveBeenCalledTimes(1)
const stacktrace = addErrorSpy.calls.argsFor(0)[0].handlingStack
expect(stacktrace).toMatch(/^HandlingStack: error\s+at triggerError (.|\n)*$/)
})
describe('save context when capturing an error', () => {
it('saves the date', async () => {
clock.tick(ONE_SECOND)
rumPublicApi.addError(new Error('foo'))
clock.tick(ONE_SECOND)
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
await collectAsyncCalls(addErrorSpy, 1)
expect(addErrorSpy.calls.argsFor(0)[0].startClocks.relative as number).toEqual(clock.relative(ONE_SECOND))
})
})
})
describe('setUser', () => {
let addActionSpy: jasmine.Spy<ReturnType<StartRum>['addAction']>
let displaySpy: jasmine.Spy<() => void>
let rumPublicApi: RumPublicApi
beforeEach(() => {
addActionSpy = jasmine.createSpy()
displaySpy = spyOn(display, 'error')
;({ rumPublicApi } = makeRumPublicApiWithDefaults({
startRumResult: {
addAction: addActionSpy,
},
}))
})
it('should attach valid objects', () => {
const user = { id: 'foo', name: 'bar', email: 'qux', foo: { bar: 'qux' } }
rumPublicApi.setUser(user)
expect(rumPublicApi.getUser()).toEqual({
email: 'qux',
foo: { bar: 'qux' },
id: 'foo',
name: 'bar',
})
expect(displaySpy).not.toHaveBeenCalled()
})
it('should sanitize predefined properties', () => {
const user = { id: false, name: 2, email: { bar: 'qux' } }
rumPublicApi.setUser(user as any)
rumPublicApi.addAction('message')
expect(rumPublicApi.getUser()).toEqual({
email: '[object Object]',
id: 'false',
name: '2',
})
expect(displaySpy).not.toHaveBeenCalled()
})
it('should remove the user', () => {
const user = { id: 'foo', name: 'bar', email: 'qux' }
rumPublicApi.setUser(user)
rumPublicApi.clearUser()
rumPublicApi.addAction('message')
expect(rumPublicApi.getUser()).toEqual({})
expect(displaySpy).not.toHaveBeenCalled()
})
it('should reject non object input', () => {
rumPublicApi.setUser(2 as any)
rumPublicApi.setUser(null as any)
rumPublicApi.setUser(undefined as any)
expect(displaySpy).toHaveBeenCalledTimes(3)
})
})
describe('getUser', () => {
let rumPublicApi: RumPublicApi
beforeEach(() => {
;({ rumPublicApi } = makeRumPublicApiWithDefaults())
})
it('should return empty object if no user has been set', () => {
const userClone = rumPublicApi.getUser()
expect(userClone).toEqual({})
})
it('should return a clone of the original object if set', () => {
const user = { id: 'foo', name: 'bar', email: 'qux', foo: { bar: 'qux' } }
rumPublicApi.setUser(user)
const userClone = rumPublicApi.getUser()
const userClone2 = rumPublicApi.getUser()
expect(userClone).not.toBe(user)
expect(userClone).not.toBe(userClone2)
expect(userClone).toEqual(user)
})
})
describe('setUserProperty', () => {
const user = { id: 'foo', name: 'bar', email: 'qux', foo: { bar: 'qux' } }
const addressAttribute = { city: 'Paris' }
let rumPublicApi: RumPublicApi
beforeEach(() => {
;({ rumPublicApi } = makeRumPublicApiWithDefaults())
})
it('should add attribute', () => {
rumPublicApi.setUser(user)
rumPublicApi.setUserProperty('address', addressAttribute)
const userClone = rumPublicApi.getUser()
expect(userClone.address).toEqual(addressAttribute)
})
it('should not contain original reference to object', () => {
const userDetails: { [key: string]: any } = { name: 'john' }
rumPublicApi.setUser(user)
rumPublicApi.setUserProperty('userDetails', userDetails)
userDetails.DOB = '11/11/1999'
const userClone = rumPublicApi.getUser()
expect(userClone.userDetails).not.toBe(userDetails)
})
it('should override attribute', () => {
rumPublicApi.setUser(user)
rumPublicApi.setUserProperty('foo', addressAttribute)
const userClone = rumPublicApi.getUser()
expect(userClone).toEqual({ ...user, foo: addressAttribute })
})
it('should sanitize properties', () => {
rumPublicApi.setUserProperty('id', 123)
rumPublicApi.setUserProperty('name', ['Adam', 'Smith'])
rumPublicApi.setUserProperty('email', { foo: 'bar' })
const userClone = rumPublicApi.getUser()
expect(userClone.id).toEqual('123')
expect(userClone.name).toEqual('Adam,Smith')
expect(userClone.email).toEqual('[object Object]')
})
})
describe('removeUserProperty', () => {
let rumPublicApi: RumPublicApi
beforeEach(() => {
;({ rumPublicApi } = makeRumPublicApiWithDefaults())
})
it('should remove property', () => {
const user = { id: 'foo', name: 'bar', email: 'qux', foo: { bar: 'qux' } }
rumPublicApi.setUser(user)
rumPublicApi.removeUserProperty('foo')
const userClone = rumPublicApi.getUser()
expect(userClone.foo).toBeUndefined()
})
})
describe('setAccount', () => {
let addActionSpy: jasmine.Spy<ReturnType<StartRum>['addAction']>
let displaySpy: jasmine.Spy<() => void>
let rumPublicApi: RumPublicApi
beforeEach(() => {
addActionSpy = jasmine.createSpy()
displaySpy = spyOn(display, 'error')
;({ rumPublicApi } = makeRumPublicApiWithDefaults({
startRumResult: {
addAction: addActionSpy,
},
}))
})
it('should attach valid objects', () => {
const account = { id: 'foo', name: 'bar', foo: { bar: 'qux' } }
rumPublicApi.setAccount(account)
rumPublicApi.addAction('message')
expect(rumPublicApi.getAccount()).toEqual({
foo: { bar: 'qux' },
id: 'foo',
name: 'bar',
})
expect(displaySpy).not.toHaveBeenCalled()
})
it('should sanitize predefined properties', () => {
const account = { id: false, name: 2 }
rumPublicApi.setAccount(account as any)
rumPublicApi.addAction('message')
expect(rumPublicApi.getAccount()).toEqual({
id: 'false',
name: '2',
})
expect(displaySpy).not.toHaveBeenCalled()
})
it('should remove the account', () => {
const account = { id: 'foo', name: 'bar' }
rumPublicApi.setAccount(account)
rumPublicApi.clearAccount()
rumPublicApi.addAction('message')
expect(rumPublicApi.getAccount()).toEqual({})
expect(displaySpy).not.toHaveBeenCalled()
})
it('should reject non object input', () => {
rumPublicApi.setAccount(2 as any)
rumPublicApi.setAccount(null as any)
rumPublicApi.setAccount(undefined as any)
expect(displaySpy).toHaveBeenCalledTimes(3)
})
})
describe('getAccount', () => {
let rumPublicApi: RumPublicApi
beforeEach(() => {
;({ rumPublicApi } = makeRumPublicApiWithDefaults())
})
it('should return empty object if no account has been set', () => {
const accountClone = rumPublicApi.getAccount()
expect(accountClone).toEqual({})
})
it('should return a clone of the original object if set', () => {
const account = { id: 'foo', name: 'bar', foo: { bar: 'qux' } }
rumPublicApi.setAccount(account)
const accountClone = rumPublicApi.getAccount()
const accountClone2 = rumPublicApi.getAccount()
expect(accountClone).not.toBe(account)
expect(accountClone).not.toBe(accountClone2)
expect(accountClone).toEqual(account)
})
})
describe('setAccountProperty', () => {
const account = { id: 'foo', name: 'bar', foo: { bar: 'qux' } }
const addressAttribute = { city: 'Paris' }
let rumPublicApi: RumPublicApi
beforeEach(() => {
;({ rumPublicApi } = makeRumPublicApiWithDefaults())
})
it('should add attribute', () => {
rumPublicApi.setAccount(account)
rumPublicApi.setAccountProperty('address', addressAttribute)
const accountClone = rumPublicApi.getAccount()
expect(accountClone.address).toEqual(addressAttribute)
})
it('should not contain original reference to object', () => {
const accountDetails: { [key: string]: any } = { name: 'company' }
rumPublicApi.setAccount(account)
rumPublicApi.setAccountProperty('accountDetails', accountDetails)
const accountClone = rumPublicApi.getAccount()
expect(accountClone.accountDetails).not.toBe(accountDetails)
})
it('should override attribute', () => {
rumPublicApi.setAccount(account)
rumPublicApi.setAccountProperty('foo', addressAttribute)
const accountClone = rumPublicApi.getAccount()
expect(accountClone).toEqual({ ...account, foo: addressAttribute })
})
it('should sanitize properties', () => {
rumPublicApi.setAccountProperty('id', 123)
rumPublicApi.setAccountProperty('name', ['My', 'Company'])
const accountClone = rumPublicApi.getAccount()
expect(accountClone.id).toEqual('123')
expect(accountClone.name).toEqual('My,Company')
})
})
describe('removeAccountProperty', () => {
let rumPublicApi: RumPublicApi
beforeEach(() => {
;({ rumPublicApi } = makeRumPublicApiWithDefaults())
})
it('should remove property', () => {
const account = { id: 'foo', name: 'bar', email: 'qux', foo: { bar: 'qux' } }
rumPublicApi.setAccount(account)
rumPublicApi.removeAccountProperty('foo')
const accountClone = rumPublicApi.getAccount()
expect(accountClone.foo).toBeUndefined()
})
})
describe('addTiming', () => {
let addTimingSpy: jasmine.Spy<ReturnType<StartRum>['addTiming']>
let displaySpy: jasmine.Spy<() => void>
let rumPublicApi: RumPublicApi
beforeEach(() => {
addTimingSpy = jasmine.createSpy()
displaySpy = spyOn(display, 'error')
;({ rumPublicApi } = makeRumPublicApiWithDefaults({
startRumResult: {
addTiming: addTimingSpy,
},
}))
})
it('should add custom timings', async () => {
const clock = mockClock()
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
rumPublicApi.addTiming('foo')
await collectAsyncCalls(addTimingSpy, 1)
expect(addTimingSpy).toHaveBeenCalledTimes(1)
expect(addTimingSpy.calls.argsFor(0)[0]).toEqual('foo')
expect(addTimingSpy.calls.argsFor(0)[1]).toBe(getTimeStamp(clock.relative(0)))
expect(displaySpy).not.toHaveBeenCalled()
})
it('buffered calls should be replayed before microtasks scheduled after init', async () => {
rumPublicApi.addTiming('before_init')
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
// Simulate user code scheduling a microtask right after init
void Promise.resolve().then(() => rumPublicApi.addTiming('after_init'))
await collectAsyncCalls(addTimingSpy, 2)
expect(addTimingSpy.calls.argsFor(0)[0]).toEqual('before_init')
expect(addTimingSpy.calls.argsFor(1)[0]).toEqual('after_init')
})
it('adds custom timing with provided time', async () => {
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
rumPublicApi.addTiming('foo', 12)
await collectAsyncCalls(addTimingSpy, 1)
expect(addTimingSpy).toHaveBeenCalledTimes(1)
expect(addTimingSpy.calls.argsFor(0)[0]).toEqual('foo')
expect(addTimingSpy.calls.argsFor(0)[1]).toBe(12 as RelativeTime)
expect(displaySpy).not.toHaveBeenCalled()
})
})
describe('setViewLoadingTime', () => {
let setLoadingTimeSpy: jasmine.Spy<ReturnType<StartRum>['setLoadingTime']>
let rumPublicApi: RumPublicApi
beforeEach(() => {
setLoadingTimeSpy = jasmine.createSpy()
;({ rumPublicApi } = makeRumPublicApiWithDefaults({
startRumResult: {
setLoadingTime: setLoadingTimeSpy,
},
}))
})
it('should call setLoadingTime with timestamp', async () => {
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
rumPublicApi.setViewLoadingTime()
await collectAsyncCalls(setLoadingTimeSpy, 1)
expect(setLoadingTimeSpy).toHaveBeenCalledOnceWith(jasmine.any(Number))
})
it('should not throw when called before init', () => {
expect(() => rumPublicApi.setViewLoadingTime()).not.toThrow()
expect(setLoadingTimeSpy).not.toHaveBeenCalled()
})
})
describe('addFeatureFlagEvaluation', () => {
it('should add feature flag evaluation when ff feature_flags enabled', async () => {
const addFeatureFlagEvaluationSpy = jasmine.createSpy()
const displaySpy = spyOn(display, 'error')
const { rumPublicApi } = makeRumPublicApiWithDefaults({
startRumResult: {
addFeatureFlagEvaluation: addFeatureFlagEvaluationSpy,
},
})
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
rumPublicApi.addFeatureFlagEvaluation('feature', 'foo')
await collectAsyncCalls(addFeatureFlagEvaluationSpy, 1)
expect(addFeatureFlagEvaluationSpy).toHaveBeenCalledTimes(1)
expect(addFeatureFlagEvaluationSpy.calls.argsFor(0)).toEqual(['feature', 'foo'])
expect(displaySpy).not.toHaveBeenCalled()
})
})
describe('stopSession', () => {
it('calls stopSession on the startRum result', async () => {
const stopSessionSpy = jasmine.createSpy()
const { rumPublicApi, startRumSpy } = makeRumPublicApiWithDefaults({
startRumResult: {
stopSession: stopSessionSpy,
},
})
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
await collectAsyncCalls(startRumSpy, 1)
rumPublicApi.stopSession()
expect(stopSessionSpy).toHaveBeenCalled()
})
})
describe('startView', () => {
it('should call RUM results startView with the view name', async () => {
const startViewSpy = jasmine.createSpy()
const { rumPublicApi } = makeRumPublicApiWithDefaults({
startRumResult: {
startView: startViewSpy,
},
})
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
rumPublicApi.startView('foo')
const calls = await collectAsyncCalls(startViewSpy, 1)
expect(calls.argsFor(0)[0]).toEqual({ name: 'foo', handlingStack: jasmine.any(String) })
})
it('should call RUM results startView with the view options', async () => {
const startViewSpy = jasmine.createSpy()
const { rumPublicApi } = makeRumPublicApiWithDefaults({
startRumResult: {
startView: startViewSpy,
},
})
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
rumPublicApi.startView({ name: 'foo', service: 'bar', version: 'baz', context: { foo: 'bar' } })
const calls = await collectAsyncCalls(startViewSpy, 1)
expect(calls.argsFor(0)[0]).toEqual({
name: 'foo',
service: 'bar',
version: 'baz',
context: { foo: 'bar' },
handlingStack: jasmine.any(String),
})
})
})
describe('recording', () => {
let rumPublicApi: RumPublicApi
let startRumSpy: ReturnType<typeof makeRumPublicApiWithDefaults>['startRumSpy']
let recorderApi: {
onRumStart: jasmine.Spy<RecorderApi['onRumStart']>
start: jasmine.Spy
stop: jasmine.Spy
getSessionReplayLink: jasmine.Spy
}
beforeEach(() => {
recorderApi = {
onRumStart: jasmine.createSpy(),
start: jasmine.createSpy(),
stop: jasmine.createSpy(),
getSessionReplayLink: jasmine.createSpy(),
}
;({ rumPublicApi, startRumSpy } = makeRumPublicApiWithDefaults({ recorderApi }))
})
it('is started with the default defaultPrivacyLevel', async () => {
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
const calls = await collectAsyncCalls(recorderApi.onRumStart, 1)
expect(calls.mostRecent().args[1].defaultPrivacyLevel).toBe(DefaultPrivacyLevel.MASK_USER_INPUT)
})
it('is started with the configured defaultPrivacyLevel', async () => {
rumPublicApi.init({
...DEFAULT_INIT_CONFIGURATION,
defaultPrivacyLevel: DefaultPrivacyLevel.MASK_USER_INPUT,
})
await collectAsyncCalls(startRumSpy, 1)
expect(recorderApi.onRumStart.calls.mostRecent().args[1].defaultPrivacyLevel).toBe(
DefaultPrivacyLevel.MASK_USER_INPUT
)
})
it('public api calls are forwarded to the recorder api', () => {
rumPublicApi.startSessionReplayRecording()
rumPublicApi.stopSessionReplayRecording()
rumPublicApi.getSessionReplayLink()
expect(recorderApi.start).toHaveBeenCalledTimes(1)
expect(recorderApi.stop).toHaveBeenCalledTimes(1)
expect(recorderApi.getSessionReplayLink).toHaveBeenCalledTimes(1)
})
it('is started with the default startSessionReplayRecordingManually', async () => {
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
const calls = await collectAsyncCalls(recorderApi.onRumStart, 1)
expect(calls.mostRecent().args[1].startSessionReplayRecordingManually).toBe(true)
})
it('is started with the configured startSessionReplayRecordingManually', async () => {
rumPublicApi.init({
...DEFAULT_INIT_CONFIGURATION,
startSessionReplayRecordingManually: false,
})
const calls = await collectAsyncCalls(recorderApi.onRumStart, 1)
expect(calls.mostRecent().args[1].startSessionReplayRecordingManually).toBe(false)
})
})
describe('startDurationVital', () => {
it('should call startDurationVital on the startRum result', async () => {
const startDurationVitalSpy = jasmine.createSpy()
const { rumPublicApi, startRumSpy } = makeRumPublicApiWithDefaults({
startRumResult: {
startDurationVital: startDurationVitalSpy,
},
})
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
await collectAsyncCalls(startRumSpy, 1)
rumPublicApi.startDurationVital('foo', { context: { foo: 'bar' }, description: 'description-value' })
await collectAsyncCalls(startDurationVitalSpy, 1)
expect(startDurationVitalSpy).toHaveBeenCalledWith(
'foo',
{
vitalKey: undefined,
description: 'description-value',
context: { foo: 'bar' },
handlingStack: jasmine.any(String),
},
jasmine.objectContaining({ relative: jasmine.any(Number), timeStamp: jasmine.any(Number) })
)
})
})
describe('stopDurationVital', () => {
it('should call stopDurationVital with a name on the startRum result', async () => {
const stopDurationVitalSpy = jasmine.createSpy()
const { rumPublicApi, startRumSpy } = makeRumPublicApiWithDefaults({
startRumResult: {
stopDurationVital: stopDurationVitalSpy,
},
})
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
await collectAsyncCalls(startRumSpy, 1)
rumPublicApi.startDurationVital('foo', { context: { foo: 'bar' }, description: 'description-value' })
rumPublicApi.stopDurationVital('foo', { context: { foo: 'bar' }, description: 'description-value' })
await collectAsyncCalls(stopDurationVitalSpy, 1)
expect(stopDurationVitalSpy).toHaveBeenCalledWith(
'foo',
{
vitalKey: undefined,
description: 'description-value',
context: { foo: 'bar' },
},
jasmine.objectContaining({ relative: jasmine.any(Number), timeStamp: jasmine.any(Number) })
)
})
it('should call stopDurationVital with a vitalKey on the startRum result', async () => {
const stopDurationVitalSpy = jasmine.createSpy()
const { rumPublicApi, startRumSpy } = makeRumPublicApiWithDefaults({
startRumResult: {
stopDurationVital: stopDurationVitalSpy,
},
})
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
await collectAsyncCalls(startRumSpy, 1)
rumPublicApi.startDurationVital('foo', {
vitalKey: 'my-key',
context: { foo: 'bar' },
description: 'description-value',
})
rumPublicApi.stopDurationVital('foo', {
vitalKey: 'my-key',
context: { foo: 'bar' },
description: 'description-value',
})
await collectAsyncCalls(stopDurationVitalSpy, 1)
expect(stopDurationVitalSpy).toHaveBeenCalledWith(
'foo',
{
vitalKey: 'my-key',
description: 'description-value',
context: { foo: 'bar' },
},
jasmine.objectContaining({ relative: jasmine.any(Number), timeStamp: jasmine.any(Number) })
)
})
})
describe('startAction / stopAction', () => {
it('should call startAction and stopAction on the strategy', async () => {
const clock = mockClock()
const startActionSpy = jasmine.createSpy('startAction')
const stopActionSpy = jasmine.createSpy('stopAction')
const { rumPublicApi } = makeRumPublicApiWithDefaults({
startRumResult: {
startAction: startActionSpy,
stopAction: stopActionSpy,
},
})
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
rumPublicApi.startAction('purchase', {
type: ActionType.CUSTOM,
context: { cart: 'abc' },
})
rumPublicApi.stopAction('purchase', {
context: { total: 100 },
})
await collectAsyncCalls(stopActionSpy, 1)
expect(startActionSpy).toHaveBeenCalledWith(
'purchase',
jasmine.objectContaining({
type: ActionType.CUSTOM,
context: { cart: 'abc' },
actionKey: undefined,
}),
{ relative: clock.relative(0), timeStamp: clock.timeStamp(0) }
)
expect(stopActionSpy).toHaveBeenCalledWith(
'purchase',
jasmine.objectContaining({
context: { total: 100 },
actionKey: undefined,
}),
{ relative: clock.relative(0), timeStamp: clock.timeStamp(0) }
)
})
it('should sanitize startAction and stopAction inputs', async () => {
const startActionSpy = jasmine.createSpy()
const { rumPublicApi } = makeRumPublicApiWithDefaults({
startRumResult: {
startAction: startActionSpy,
},
})
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
rumPublicApi.startAction('action_name', {
type: ActionType.CUSTOM,
context: { count: 123, nested: { foo: 'bar' } } as any,
actionKey: 'action_key',
})
await collectAsyncCalls(startActionSpy, 1)
expect(startActionSpy.calls.argsFor(0)[1]).toEqual(
jasmine.objectContaining({
type: ActionType.CUSTOM,
context: { count: 123, nested: { foo: 'bar' } },
actionKey: 'action_key',
})
)
})
})
describe('startResource / stopResource', () => {
it('should call startResource and stopResource on the strategy', async () => {
const clock = mockClock()
const startResourceSpy = jasmine.createSpy('startResource')
const stopResourceSpy = jasmine.createSpy('stopResource')
const { rumPublicApi } = makeRumPublicApiWithDefaults({
startRumResult: {
startResource: startResourceSpy,
stopResource: stopResourceSpy,
},
})
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
rumPublicApi.startResource('https://api.example.com/data', {
type: ResourceType.FETCH,
method: 'POST',
context: { requestId: 'abc' },
})
rumPublicApi.stopResource('https://api.example.com/data', {
type: ResourceType.XHR,
statusCode: 200,
size: 1024,
context: { requestId: 'abc' },
})
await collectAsyncCalls(stopResourceSpy, 1)
expect(startResourceSpy).toHaveBeenCalledWith(
'https://api.example.com/data',
jasmine.objectContaining({
type: ResourceType.FETCH,
method: 'POST',
context: { requestId: 'abc' },
resourceKey: undefined,
}),
{ relative: clock.relative(0), timeStamp: clock.timeStamp(0) }
)
expect(stopResourceSpy).toHaveBeenCalledWith(
'https://api.example.com/data',
jasmine.objectContaining({
type: ResourceType.XHR,
statusCode: 200,
size: 1024,
context: { requestId: 'abc' },
resourceKey: undefined,
}),
{ relative: clock.relative(0), timeStamp: clock.timeStamp(0) }
)
})
it('should sanitize startResource and stopResource inputs', async () => {
const startResourceSpy = jasmine.createSpy()
const { rumPublicApi } = makeRumPublicApiWithDefaults({
startRumResult: {
startResource: startResourceSpy,
},
})
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
rumPublicApi.startResource('https://api.example.com/data', {
type: ResourceType.XHR,
method: 'GET',
context: { count: 123, nested: { foo: 'bar' } },
resourceKey: 'resource_key',
})
await collectAsyncCalls(startResourceSpy, 1)
expect(startResourceSpy.calls.argsFor(0)[1]).toEqual(
jasmine.objectContaining({
type: ResourceType.XHR,
method: 'GET',
context: { count: 123, nested: { foo: 'bar' } },
resourceKey: 'resource_key',
})
)
})
})
describe('addDurationVital', () => {
it('should call addDurationVital on the startRum result', async () => {
const addDurationVitalSpy = jasmine.createSpy()