@newrelic/browser-agent 1.242.0 → 1.243.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/CHANGELOG.md +1465 -0
  2. package/dist/cjs/common/constants/env.cdn.js +1 -1
  3. package/dist/cjs/common/constants/env.npm.js +1 -1
  4. package/dist/cjs/common/session/session-entity.js +20 -2
  5. package/dist/cjs/common/wrap/wrap-function.js +1 -1
  6. package/dist/cjs/features/ajax/aggregate/index.js +1 -1
  7. package/dist/cjs/features/session_replay/aggregate/index.js +84 -54
  8. package/dist/cjs/features/utils/feature-base.js +1 -2
  9. package/dist/cjs/loaders/api/api.js +2 -2
  10. package/dist/cjs/loaders/api/apiAsync.js +0 -37
  11. package/dist/cjs/loaders/configure/configure.js +1 -1
  12. package/dist/cjs/loaders/configure/public-path.js +6 -3
  13. package/dist/esm/common/constants/env.cdn.js +1 -1
  14. package/dist/esm/common/constants/env.npm.js +1 -1
  15. package/dist/esm/common/session/session-entity.js +18 -1
  16. package/dist/esm/common/wrap/wrap-function.js +1 -1
  17. package/dist/esm/features/ajax/aggregate/index.js +1 -1
  18. package/dist/esm/features/session_replay/aggregate/index.js +83 -54
  19. package/dist/esm/features/utils/feature-base.js +1 -2
  20. package/dist/esm/loaders/api/api.js +2 -2
  21. package/dist/esm/loaders/api/apiAsync.js +1 -36
  22. package/dist/esm/loaders/configure/configure.js +1 -1
  23. package/dist/esm/loaders/configure/public-path.js +6 -3
  24. package/dist/types/common/session/session-entity.d.ts +5 -0
  25. package/dist/types/common/session/session-entity.d.ts.map +1 -1
  26. package/dist/types/features/session_replay/aggregate/index.d.ts +11 -14
  27. package/dist/types/features/session_replay/aggregate/index.d.ts.map +1 -1
  28. package/dist/types/features/utils/feature-base.d.ts.map +1 -1
  29. package/dist/types/loaders/api/api.d.ts.map +1 -1
  30. package/dist/types/loaders/api/apiAsync.d.ts.map +1 -1
  31. package/dist/types/loaders/configure/public-path.d.ts +1 -1
  32. package/dist/types/loaders/configure/public-path.d.ts.map +1 -1
  33. package/package.json +2 -2
  34. package/src/common/session/session-entity.js +20 -1
  35. package/src/common/wrap/wrap-function.js +1 -1
  36. package/src/features/ajax/aggregate/index.js +2 -2
  37. package/src/features/session_replay/aggregate/index.js +81 -37
  38. package/src/features/utils/feature-base.js +1 -2
  39. package/src/loaders/api/api.js +1 -2
  40. package/src/loaders/api/apiAsync.js +1 -39
  41. package/src/loaders/configure/configure.js +1 -1
  42. package/src/loaders/configure/public-path.js +6 -3
  43. package/src/common/aggregate/aggregator.test.js +0 -107
  44. package/src/common/config/state/configurable.test.js +0 -73
  45. package/src/common/config/state/info.test.js +0 -31
  46. package/src/common/config/state/init.test.js +0 -68
  47. package/src/common/config/state/loader-config.test.js +0 -21
  48. package/src/common/config/state/runtime.test.js +0 -21
  49. package/src/common/constants/env.cdn.test.js +0 -7
  50. package/src/common/constants/env.npm.test.js +0 -7
  51. package/src/common/constants/env.test.js +0 -7
  52. package/src/common/constants/runtime.test.js +0 -176
  53. package/src/common/deny-list/deny-list.test.js +0 -104
  54. package/src/common/dom/query-selector.test.js +0 -24
  55. package/src/common/drain/drain.test.js +0 -74
  56. package/src/common/event-emitter/contextual-ee.component-test.js +0 -293
  57. package/src/common/event-emitter/handle.test.js +0 -56
  58. package/src/common/event-emitter/register-handler.test.js +0 -61
  59. package/src/common/harvest/harvest-scheduler.test.js +0 -492
  60. package/src/common/harvest/harvest.test.js +0 -813
  61. package/src/common/ids/id.test.js +0 -92
  62. package/src/common/ids/unique-id.test.js +0 -58
  63. package/src/common/session/session-entity.component-test.js +0 -346
  64. package/src/common/storage/local-storage.test.js +0 -17
  65. package/src/common/timer/interaction-timer.component-test.js +0 -212
  66. package/src/common/timer/timer.test.js +0 -99
  67. package/src/common/timing/nav-timing.test.js +0 -161
  68. package/src/common/url/canonicalize-url.test.js +0 -45
  69. package/src/common/url/clean-url.test.js +0 -25
  70. package/src/common/url/encode.test.js +0 -81
  71. package/src/common/url/location.test.js +0 -15
  72. package/src/common/url/parse-url.test.js +0 -110
  73. package/src/common/url/protocol.test.js +0 -17
  74. package/src/common/util/console.test.js +0 -34
  75. package/src/common/util/data-size.test.js +0 -56
  76. package/src/common/util/feature-flags.test.js +0 -94
  77. package/src/common/util/get-or-set.test.js +0 -58
  78. package/src/common/util/invoke.test.js +0 -65
  79. package/src/common/util/map-own.test.js +0 -52
  80. package/src/common/util/obfuscate.component-test.js +0 -173
  81. package/src/common/util/stringify.test.js +0 -49
  82. package/src/common/util/submit-data.test.js +0 -183
  83. package/src/common/util/traverse.test.js +0 -50
  84. package/src/common/vitals/cumulative-layout-shift.test.js +0 -71
  85. package/src/common/vitals/first-contentful-paint.test.js +0 -124
  86. package/src/common/vitals/first-input-delay.test.js +0 -88
  87. package/src/common/vitals/first-paint.test.js +0 -127
  88. package/src/common/vitals/interaction-to-next-paint.test.js +0 -74
  89. package/src/common/vitals/largest-contentful-paint.test.js +0 -94
  90. package/src/common/vitals/long-task.test.js +0 -122
  91. package/src/common/vitals/time-to-first-byte.test.js +0 -147
  92. package/src/common/vitals/vital-metric.test.js +0 -171
  93. package/src/common/wrap/wrap-promise.component-test.js +0 -110
  94. package/src/features/ajax/instrument/distributed-tracing.test.js +0 -375
  95. package/src/features/jserrors/aggregate/canonical-function-name.test.js +0 -13
  96. package/src/features/jserrors/aggregate/compute-stack-trace.test.js +0 -414
  97. package/src/features/jserrors/aggregate/format-stack-trace.test.js +0 -39
  98. package/src/features/jserrors/aggregate/string-hash-code.test.js +0 -12
  99. package/src/features/metrics/aggregate/framework-detection.test.js +0 -332
  100. package/src/features/page_view_timing/aggregate/index.component-test.js +0 -86
  101. package/src/features/session_replay/aggregate/index.component-test.js +0 -317
  102. package/src/features/spa/aggregate/interaction-node.test.js +0 -17
  103. package/src/features/utils/agent-session.test.js +0 -194
  104. package/src/features/utils/aggregate-base.test.js +0 -123
  105. package/src/features/utils/feature-base.test.js +0 -45
  106. package/src/features/utils/handler-cache.test.js +0 -72
  107. package/src/features/utils/instrument-base.test.js +0 -216
  108. package/src/features/utils/lazy-feature-loader.test.js +0 -37
  109. package/src/loaders/api/api.component-test.js +0 -45
  110. package/src/loaders/api/api.test.js +0 -85
  111. package/src/loaders/api/apiAsync.test.js +0 -17
@@ -1,492 +0,0 @@
1
- import { faker } from '@faker-js/faker'
2
-
3
- import * as submitData from '../util/submit-data'
4
- import { subscribeToEOL } from '../unload/eol'
5
- import { Harvest } from './harvest'
6
-
7
- import { HarvestScheduler } from './harvest-scheduler'
8
-
9
- jest.enableAutomock()
10
- jest.unmock('./harvest-scheduler')
11
- jest.useFakeTimers()
12
-
13
- let harvestSchedulerInstance
14
- let harvestInstance
15
-
16
- beforeEach(() => {
17
- harvestSchedulerInstance = new HarvestScheduler()
18
- harvestInstance = jest.mocked(Harvest).mock.instances[0]
19
- })
20
-
21
- afterEach(() => {
22
- jest.clearAllMocks()
23
- })
24
-
25
- describe('unload', () => {
26
- let eolSubscribeFn
27
-
28
- beforeEach(() => {
29
- eolSubscribeFn = jest.mocked(subscribeToEOL).mock.calls[0][0]
30
- jest.spyOn(harvestSchedulerInstance, 'runHarvest').mockImplementation(jest.fn())
31
- })
32
-
33
- test('should subscribe to eol', () => {
34
- new HarvestScheduler()
35
-
36
- expect(subscribeToEOL).toHaveBeenCalledWith(expect.any(Function))
37
- })
38
-
39
- test('should run onUnload callback when started', () => {
40
- harvestSchedulerInstance.opts.onUnload = jest.fn()
41
-
42
- eolSubscribeFn()
43
-
44
- expect(harvestSchedulerInstance.opts.onUnload).toHaveBeenCalledTimes(1)
45
- })
46
-
47
- test('should run harvest when started and not aborted', () => {
48
- harvestSchedulerInstance.aborted = false
49
-
50
- eolSubscribeFn()
51
-
52
- expect(harvestSchedulerInstance.runHarvest).toHaveBeenCalledWith({ unload: true })
53
- })
54
-
55
- test('should not run harvest when aborted', () => {
56
- harvestSchedulerInstance.aborted = true
57
- jest.spyOn(harvestSchedulerInstance, 'runHarvest').mockImplementation(jest.fn())
58
-
59
- eolSubscribeFn()
60
-
61
- expect(harvestSchedulerInstance.runHarvest).not.toHaveBeenCalled()
62
- })
63
- })
64
-
65
- describe('startTimer', () => {
66
- beforeEach(() => {
67
- jest.spyOn(harvestSchedulerInstance, 'scheduleHarvest').mockImplementation(jest.fn())
68
- })
69
-
70
- test('should use provided delay to schedule harvest', () => {
71
- const interval = faker.datatype.number({ min: 100, max: 1000 })
72
- const initialDelay = faker.datatype.number({ min: 100, max: 1000 })
73
-
74
- harvestSchedulerInstance.startTimer(interval, initialDelay)
75
-
76
- expect(harvestSchedulerInstance.interval).toEqual(interval)
77
- expect(harvestSchedulerInstance.started).toEqual(true)
78
- expect(harvestSchedulerInstance.scheduleHarvest).toHaveBeenCalledWith(initialDelay)
79
- })
80
-
81
- test('should use provided interval to schedule harvest when initialDelay is null', () => {
82
- const interval = faker.datatype.number({ min: 100, max: 1000 })
83
-
84
- harvestSchedulerInstance.startTimer(interval, null)
85
-
86
- expect(harvestSchedulerInstance.interval).toEqual(interval)
87
- expect(harvestSchedulerInstance.started).toEqual(true)
88
- expect(harvestSchedulerInstance.scheduleHarvest).toHaveBeenCalledWith(interval)
89
- })
90
- })
91
-
92
- describe('stopTimer', () => {
93
- beforeEach(() => {
94
- jest.spyOn(harvestSchedulerInstance, 'scheduleHarvest').mockImplementation(jest.fn())
95
- })
96
-
97
- test.each([
98
- false, undefined
99
- ])('should not abort the scheduler when permanently param is %s', (permanently) => {
100
- harvestSchedulerInstance.stopTimer(permanently)
101
-
102
- expect(harvestSchedulerInstance.aborted).toEqual(false)
103
- expect(harvestSchedulerInstance.started).toEqual(false)
104
- })
105
-
106
- test('should abort the scheduler when permanently param is true', () => {
107
- harvestSchedulerInstance.stopTimer(true)
108
-
109
- expect(harvestSchedulerInstance.aborted).toEqual(true)
110
- expect(harvestSchedulerInstance.started).toEqual(false)
111
- })
112
-
113
- test('should clear the timeoutHandle', () => {
114
- jest.spyOn(global, 'clearTimeout')
115
- const timeoutHandle = setTimeout(jest.fn(), 1000000)
116
- harvestSchedulerInstance.timeoutHandle = timeoutHandle
117
-
118
- harvestSchedulerInstance.stopTimer()
119
-
120
- expect(global.clearTimeout).toHaveBeenCalledWith(timeoutHandle)
121
- })
122
- })
123
-
124
- describe('scheduleHarvest', () => {
125
- beforeEach(() => {
126
- jest.spyOn(harvestSchedulerInstance, 'runHarvest').mockImplementation(jest.fn())
127
- })
128
-
129
- test('should runHarvest after the provided delay in seconds', () => {
130
- const delay = faker.datatype.number({ min: 100, max: 1000 })
131
- const opts = {
132
- [faker.datatype.uuid()]: faker.lorem.sentence()
133
- }
134
-
135
- harvestSchedulerInstance.scheduleHarvest(delay, opts)
136
-
137
- expect(harvestSchedulerInstance.timeoutHandle).toEqual(expect.any(Number))
138
- expect(harvestSchedulerInstance.runHarvest).not.toHaveBeenCalled()
139
-
140
- jest.advanceTimersByTime(delay * 1000)
141
-
142
- expect(harvestSchedulerInstance.timeoutHandle).toEqual(null)
143
- expect(harvestSchedulerInstance.runHarvest).toHaveBeenCalledWith(opts)
144
- })
145
-
146
- test('should default delay to internal interval', () => {
147
- const interval = faker.datatype.number({ min: 100, max: 1000 })
148
- harvestSchedulerInstance.interval = interval
149
- const opts = {
150
- [faker.datatype.uuid()]: faker.lorem.sentence()
151
- }
152
-
153
- harvestSchedulerInstance.scheduleHarvest(null, opts)
154
-
155
- expect(harvestSchedulerInstance.timeoutHandle).toEqual(expect.any(Number))
156
- expect(harvestSchedulerInstance.runHarvest).not.toHaveBeenCalled()
157
-
158
- jest.advanceTimersByTime(interval * 1000)
159
-
160
- expect(harvestSchedulerInstance.timeoutHandle).toEqual(null)
161
- expect(harvestSchedulerInstance.runHarvest).toHaveBeenCalledWith(opts)
162
- })
163
-
164
- test('should not call setTimeout when timeoutHandle already exists', () => {
165
- jest.spyOn(global, 'setTimeout')
166
- const timeoutHandle = setTimeout(jest.fn(), 1000000)
167
- harvestSchedulerInstance.timeoutHandle = timeoutHandle
168
-
169
- harvestSchedulerInstance.scheduleHarvest(null)
170
-
171
- expect(global.setTimeout).toHaveBeenCalledTimes(1)
172
- })
173
- })
174
-
175
- describe('runHarvest', () => {
176
- beforeEach(() => {
177
- jest.spyOn(harvestSchedulerInstance, 'scheduleHarvest').mockImplementation(jest.fn())
178
- jest.spyOn(harvestSchedulerInstance, 'onHarvestFinished').mockImplementation(jest.fn())
179
- })
180
-
181
- test('should not run harvest when scheduler is aborted', () => {
182
- harvestSchedulerInstance.aborted = true
183
- harvestSchedulerInstance.runHarvest({})
184
-
185
- expect(harvestInstance.sendX).not.toHaveBeenCalled()
186
- expect(harvestInstance.send).not.toHaveBeenCalled()
187
- })
188
-
189
- test.each([
190
- null, undefined
191
- ])('should use sendX for harvesting when getPayload is %s', (getPayload) => {
192
- harvestSchedulerInstance.endpoint = faker.datatype.uuid()
193
- harvestSchedulerInstance.opts.getPayload = getPayload
194
- const harvestRunOpts = {
195
- [faker.datatype.uuid()]: faker.lorem.sentence()
196
- }
197
-
198
- harvestSchedulerInstance.runHarvest(harvestRunOpts)
199
-
200
- expect(harvestInstance.sendX).toHaveBeenCalledWith({
201
- cbFinished: expect.any(Function),
202
- customUrl: undefined,
203
- endpoint: harvestSchedulerInstance.endpoint,
204
- opts: harvestRunOpts,
205
- payload: undefined,
206
- raw: undefined,
207
- submitMethod: undefined
208
- })
209
- })
210
-
211
- test('should use send for harvesting when getPayload is defined', () => {
212
- const payload = {
213
- body: {
214
- [faker.datatype.uuid()]: faker.lorem.sentence()
215
- },
216
- qs: {
217
- [faker.datatype.uuid()]: faker.lorem.sentence()
218
- }
219
- }
220
- harvestSchedulerInstance.endpoint = faker.datatype.uuid()
221
- harvestSchedulerInstance.opts.getPayload = jest.fn().mockReturnValue(payload)
222
- const harvestRunOpts = {
223
- [faker.datatype.uuid()]: faker.lorem.sentence()
224
- }
225
-
226
- harvestSchedulerInstance.runHarvest(harvestRunOpts)
227
-
228
- expect(harvestInstance.send).toHaveBeenCalledWith({
229
- cbFinished: expect.any(Function),
230
- customUrl: undefined,
231
- endpoint: harvestSchedulerInstance.endpoint,
232
- opts: harvestRunOpts,
233
- payload,
234
- raw: undefined,
235
- submitMethod: expect.any(Function)
236
- })
237
- })
238
-
239
- test('should use _send for harvesting when opts.raw is true', () => {
240
- const payload = {
241
- body: {
242
- [faker.datatype.uuid()]: faker.lorem.sentence()
243
- },
244
- qs: {
245
- [faker.datatype.uuid()]: faker.lorem.sentence()
246
- }
247
- }
248
- harvestSchedulerInstance.endpoint = faker.datatype.uuid()
249
- harvestSchedulerInstance.opts.raw = true
250
- harvestSchedulerInstance.opts.getPayload = jest.fn().mockReturnValue(payload)
251
- const harvestRunOpts = {
252
- [faker.datatype.uuid()]: faker.lorem.sentence()
253
- }
254
-
255
- harvestSchedulerInstance.runHarvest(harvestRunOpts)
256
-
257
- expect(harvestInstance._send).toHaveBeenCalledWith({
258
- cbFinished: expect.any(Function),
259
- customUrl: undefined,
260
- endpoint: harvestSchedulerInstance.endpoint,
261
- opts: harvestRunOpts,
262
- payload,
263
- raw: true,
264
- submitMethod: expect.any(Function)
265
- })
266
- })
267
-
268
- test('should rescheduled harvesting when getPayload returns no data', () => {
269
- harvestSchedulerInstance.started = true
270
- harvestSchedulerInstance.opts.getPayload = jest.fn().mockReturnValue()
271
-
272
- harvestSchedulerInstance.runHarvest({})
273
-
274
- expect(harvestInstance.sendX).not.toHaveBeenCalled()
275
- expect(harvestInstance.send).not.toHaveBeenCalled()
276
- expect(harvestSchedulerInstance.scheduleHarvest).toHaveBeenCalled()
277
- })
278
-
279
- test('should schedule the next harvest after running harvest', () => {
280
- const payload = {
281
- body: {
282
- [faker.datatype.uuid()]: faker.lorem.sentence()
283
- },
284
- qs: {
285
- [faker.datatype.uuid()]: faker.lorem.sentence()
286
- }
287
- }
288
- harvestSchedulerInstance.started = true
289
- harvestSchedulerInstance.endpoint = faker.datatype.uuid()
290
- harvestSchedulerInstance.opts.getPayload = jest.fn().mockReturnValue(payload)
291
- const harvestRunOpts = {
292
- [faker.datatype.uuid()]: faker.lorem.sentence()
293
- }
294
-
295
- harvestSchedulerInstance.runHarvest(harvestRunOpts)
296
-
297
- expect(harvestInstance.send).toHaveBeenCalled()
298
- expect(harvestSchedulerInstance.scheduleHarvest).toHaveBeenCalled()
299
- })
300
-
301
- test.each([
302
- null, undefined, false
303
- ])('should set retry to true unload opt is %s', (unload) => {
304
- jest.mocked(submitData.getSubmitMethod).mockReturnValue(submitData.xhr)
305
- harvestSchedulerInstance.opts.getPayload = jest.fn().mockReturnValue()
306
-
307
- harvestSchedulerInstance.runHarvest({ unload })
308
-
309
- expect(harvestSchedulerInstance.opts.getPayload).toHaveBeenCalledWith({ retry: true })
310
- })
311
-
312
- test('should set retry to false when submitMethod is not xhr', () => {
313
- jest.mocked(submitData.getSubmitMethod).mockReturnValue(jest.fn())
314
- harvestSchedulerInstance.opts.getPayload = jest.fn().mockReturnValue()
315
-
316
- harvestSchedulerInstance.runHarvest({ unload: false })
317
-
318
- expect(harvestSchedulerInstance.opts.getPayload).toHaveBeenCalledWith({ retry: false })
319
- })
320
-
321
- test('should run onHarvestFinished after harvest finishes', () => {
322
- const harvestRunOpts = {
323
- [faker.datatype.uuid()]: faker.lorem.sentence()
324
- }
325
- const result = {
326
- [faker.datatype.uuid()]: faker.lorem.sentence()
327
- }
328
-
329
- harvestSchedulerInstance.runHarvest(harvestRunOpts)
330
- const cbFinishedFn = jest.mocked(harvestInstance.sendX).mock.calls[0][0].cbFinished
331
- cbFinishedFn(result)
332
-
333
- expect(harvestSchedulerInstance.onHarvestFinished).toHaveBeenCalledWith(harvestRunOpts, result)
334
- })
335
-
336
- test('should disable retry in harvest callback when forceNoRetry is true', () => {
337
- const harvestRunOpts = {
338
- [faker.datatype.uuid()]: faker.lorem.sentence(),
339
- forceNoRetry: true
340
- }
341
- const result = {
342
- [faker.datatype.uuid()]: faker.lorem.sentence()
343
- }
344
-
345
- harvestSchedulerInstance.runHarvest(harvestRunOpts)
346
- const cbFinishedFn = jest.mocked(harvestInstance.sendX).mock.calls[0][0].cbFinished
347
- cbFinishedFn(result)
348
-
349
- expect(harvestSchedulerInstance.onHarvestFinished).toHaveBeenCalledWith(harvestRunOpts, {
350
- ...result,
351
- retry: false
352
- })
353
- })
354
- })
355
-
356
- describe('onHarvestFinished', () => {
357
- beforeEach(() => {
358
- jest.spyOn(global, 'clearTimeout')
359
- jest.spyOn(harvestSchedulerInstance, 'scheduleHarvest').mockImplementation(jest.fn())
360
- })
361
-
362
- test('should call onFinished callback', () => {
363
- harvestSchedulerInstance.opts.onFinished = jest.fn()
364
- const result = {
365
- [faker.datatype.uuid()]: faker.lorem.sentence()
366
- }
367
-
368
- harvestSchedulerInstance.onHarvestFinished({}, result)
369
-
370
- expect(harvestSchedulerInstance.opts.onFinished).toHaveBeenCalledWith(result)
371
- })
372
-
373
- test.each([
374
- null, undefined, false
375
- ])('should not reschedule harvest when result.sent is %s', (sent) => {
376
- const result = {
377
- [faker.datatype.uuid()]: faker.lorem.sentence(),
378
- sent,
379
- retry: true
380
- }
381
-
382
- harvestSchedulerInstance.onHarvestFinished({}, result)
383
-
384
- expect(harvestSchedulerInstance.scheduleHarvest).not.toHaveBeenCalled()
385
- })
386
-
387
- test.each([
388
- null, undefined, false
389
- ])('should not reschedule harvest when result.retry is %s', (retry) => {
390
- const result = {
391
- [faker.datatype.uuid()]: faker.lorem.sentence(),
392
- sent: true,
393
- retry
394
- }
395
-
396
- harvestSchedulerInstance.onHarvestFinished({}, result)
397
-
398
- expect(harvestSchedulerInstance.scheduleHarvest).not.toHaveBeenCalled()
399
- })
400
-
401
- test('should reschedule harvest using result.delay', () => {
402
- const harvestOpts = {
403
- [faker.datatype.uuid()]: faker.lorem.sentence()
404
- }
405
- const result = {
406
- [faker.datatype.uuid()]: faker.lorem.sentence(),
407
- sent: true,
408
- retry: true,
409
- delay: faker.datatype.number({ min: 100, max: 1000 })
410
- }
411
-
412
- harvestSchedulerInstance.onHarvestFinished(harvestOpts, result)
413
-
414
- expect(harvestSchedulerInstance.scheduleHarvest).toHaveBeenCalledWith(result.delay, harvestOpts)
415
- })
416
-
417
- test('should reschedule harvest using instance retryDelay opt', () => {
418
- harvestSchedulerInstance.opts.retryDelay = faker.datatype.number({ min: 100, max: 1000 })
419
- const harvestOpts = {
420
- [faker.datatype.uuid()]: faker.lorem.sentence()
421
- }
422
- const result = {
423
- [faker.datatype.uuid()]: faker.lorem.sentence(),
424
- sent: true,
425
- retry: true
426
- }
427
-
428
- harvestSchedulerInstance.onHarvestFinished(harvestOpts, result)
429
-
430
- expect(harvestSchedulerInstance.scheduleHarvest).toHaveBeenCalledWith(harvestSchedulerInstance.opts.retryDelay, harvestOpts)
431
- })
432
-
433
- test.each([
434
- null, undefined, false, 0
435
- ])('should not reschedule harvest when delay is %s and scheduler not started', (delay) => {
436
- harvestSchedulerInstance.opts.retryDelay = delay
437
- const harvestOpts = {
438
- [faker.datatype.uuid()]: faker.lorem.sentence()
439
- }
440
- const result = {
441
- [faker.datatype.uuid()]: faker.lorem.sentence(),
442
- sent: true,
443
- retry: true,
444
- delay
445
- }
446
-
447
- harvestSchedulerInstance.onHarvestFinished(harvestOpts, result)
448
-
449
- expect(harvestSchedulerInstance.scheduleHarvest).not.toHaveBeenCalled()
450
- })
451
-
452
- test.each([
453
- null, undefined, false, 0
454
- ])('should not reschedule harvest when delay is %s and scheduler started', (delay) => {
455
- harvestSchedulerInstance.started = true
456
- harvestSchedulerInstance.opts.retryDelay = delay
457
- const harvestOpts = {
458
- [faker.datatype.uuid()]: faker.lorem.sentence()
459
- }
460
- const result = {
461
- [faker.datatype.uuid()]: faker.lorem.sentence(),
462
- sent: true,
463
- retry: true,
464
- delay
465
- }
466
-
467
- harvestSchedulerInstance.onHarvestFinished(harvestOpts, result)
468
-
469
- expect(harvestSchedulerInstance.scheduleHarvest).not.toHaveBeenCalled()
470
- })
471
-
472
- test('should clear the current timeout handle and reschedule the harvest', () => {
473
- const timeoutHandle = setTimeout(jest.fn(), 100000)
474
- harvestSchedulerInstance.opts.retryDelay = faker.datatype.number({ min: 100, max: 1000 })
475
- harvestSchedulerInstance.timeoutHandle = timeoutHandle
476
- harvestSchedulerInstance.started = true
477
- const harvestOpts = {
478
- [faker.datatype.uuid()]: faker.lorem.sentence()
479
- }
480
- const result = {
481
- [faker.datatype.uuid()]: faker.lorem.sentence(),
482
- sent: true,
483
- retry: true
484
- }
485
-
486
- harvestSchedulerInstance.onHarvestFinished(harvestOpts, result)
487
-
488
- expect(global.clearTimeout).toHaveBeenCalledWith(timeoutHandle)
489
- expect(harvestSchedulerInstance.timeoutHandle).toEqual(null)
490
- expect(harvestSchedulerInstance.scheduleHarvest).toHaveBeenCalledWith(harvestSchedulerInstance.opts.retryDelay, harvestOpts)
491
- })
492
- })