@newrelic/browser-agent 1.241.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 (132) hide show
  1. package/CHANGELOG.md +1465 -0
  2. package/dist/cjs/cdn/polyfills/lite.js +13 -1
  3. package/dist/cjs/cdn/polyfills/pro.js +17 -1
  4. package/dist/cjs/cdn/polyfills/spa.js +18 -1
  5. package/dist/cjs/common/config/state/init.js +32 -5
  6. package/dist/cjs/common/constants/env.cdn.js +1 -1
  7. package/dist/cjs/common/constants/env.npm.js +1 -1
  8. package/dist/cjs/common/dom/query-selector.js +16 -0
  9. package/dist/cjs/common/session/session-entity.js +20 -2
  10. package/dist/cjs/common/wrap/wrap-function.js +1 -1
  11. package/dist/cjs/features/ajax/aggregate/index.js +1 -1
  12. package/dist/cjs/features/session_replay/aggregate/index.js +84 -50
  13. package/dist/cjs/features/utils/feature-base.js +1 -2
  14. package/dist/cjs/features/utils/instrument-base.js +1 -0
  15. package/dist/cjs/loaders/api/api.js +2 -2
  16. package/dist/cjs/loaders/api/apiAsync.js +0 -37
  17. package/dist/cjs/loaders/configure/configure.js +1 -1
  18. package/dist/cjs/loaders/configure/public-path.js +6 -3
  19. package/dist/esm/cdn/polyfills/lite.js +8 -1
  20. package/dist/esm/cdn/polyfills/pro.js +13 -2
  21. package/dist/esm/cdn/polyfills/spa.js +13 -1
  22. package/dist/esm/common/config/state/init.js +32 -5
  23. package/dist/esm/common/constants/env.cdn.js +1 -1
  24. package/dist/esm/common/constants/env.npm.js +1 -1
  25. package/dist/esm/common/dom/query-selector.js +9 -0
  26. package/dist/esm/common/session/session-entity.js +18 -1
  27. package/dist/esm/common/wrap/wrap-function.js +1 -1
  28. package/dist/esm/features/ajax/aggregate/index.js +1 -1
  29. package/dist/esm/features/session_replay/aggregate/index.js +83 -50
  30. package/dist/esm/features/utils/feature-base.js +1 -2
  31. package/dist/esm/features/utils/instrument-base.js +1 -0
  32. package/dist/esm/loaders/api/api.js +2 -2
  33. package/dist/esm/loaders/api/apiAsync.js +1 -36
  34. package/dist/esm/loaders/configure/configure.js +1 -1
  35. package/dist/esm/loaders/configure/public-path.js +6 -3
  36. package/dist/types/common/config/state/init.d.ts.map +1 -1
  37. package/dist/types/common/dom/query-selector.d.ts +2 -0
  38. package/dist/types/common/dom/query-selector.d.ts.map +1 -0
  39. package/dist/types/common/session/session-entity.d.ts +5 -0
  40. package/dist/types/common/session/session-entity.d.ts.map +1 -1
  41. package/dist/types/features/session_replay/aggregate/index.d.ts +11 -14
  42. package/dist/types/features/session_replay/aggregate/index.d.ts.map +1 -1
  43. package/dist/types/features/utils/feature-base.d.ts.map +1 -1
  44. package/dist/types/features/utils/instrument-base.d.ts.map +1 -1
  45. package/dist/types/loaders/api/api.d.ts.map +1 -1
  46. package/dist/types/loaders/api/apiAsync.d.ts.map +1 -1
  47. package/dist/types/loaders/configure/public-path.d.ts +1 -1
  48. package/dist/types/loaders/configure/public-path.d.ts.map +1 -1
  49. package/package.json +2 -2
  50. package/src/cdn/polyfills/lite.js +14 -1
  51. package/src/cdn/polyfills/pro.js +23 -2
  52. package/src/cdn/polyfills/spa.js +24 -1
  53. package/src/common/config/state/init.js +33 -4
  54. package/src/common/dom/query-selector.js +9 -0
  55. package/src/common/session/session-entity.js +20 -1
  56. package/src/common/wrap/wrap-function.js +1 -1
  57. package/src/features/ajax/aggregate/index.js +2 -2
  58. package/src/features/session_replay/aggregate/index.js +82 -34
  59. package/src/features/utils/feature-base.js +1 -2
  60. package/src/features/utils/instrument-base.js +1 -0
  61. package/src/loaders/api/api.js +1 -2
  62. package/src/loaders/api/apiAsync.js +1 -39
  63. package/src/loaders/configure/configure.js +1 -1
  64. package/src/loaders/configure/public-path.js +6 -3
  65. package/src/common/aggregate/aggregator.test.js +0 -107
  66. package/src/common/config/state/configurable.test.js +0 -73
  67. package/src/common/config/state/info.test.js +0 -31
  68. package/src/common/config/state/init.test.js +0 -28
  69. package/src/common/config/state/loader-config.test.js +0 -21
  70. package/src/common/config/state/runtime.test.js +0 -21
  71. package/src/common/constants/env.cdn.test.js +0 -7
  72. package/src/common/constants/env.npm.test.js +0 -7
  73. package/src/common/constants/env.test.js +0 -7
  74. package/src/common/constants/runtime.test.js +0 -176
  75. package/src/common/deny-list/deny-list.test.js +0 -104
  76. package/src/common/drain/drain.test.js +0 -74
  77. package/src/common/event-emitter/contextual-ee.component-test.js +0 -293
  78. package/src/common/event-emitter/handle.test.js +0 -56
  79. package/src/common/event-emitter/register-handler.test.js +0 -61
  80. package/src/common/harvest/harvest-scheduler.test.js +0 -492
  81. package/src/common/harvest/harvest.test.js +0 -813
  82. package/src/common/ids/id.test.js +0 -92
  83. package/src/common/ids/unique-id.test.js +0 -58
  84. package/src/common/session/session-entity.component-test.js +0 -346
  85. package/src/common/storage/local-storage.test.js +0 -17
  86. package/src/common/timer/interaction-timer.component-test.js +0 -212
  87. package/src/common/timer/timer.test.js +0 -99
  88. package/src/common/timing/nav-timing.test.js +0 -161
  89. package/src/common/url/canonicalize-url.test.js +0 -45
  90. package/src/common/url/clean-url.test.js +0 -25
  91. package/src/common/url/encode.test.js +0 -81
  92. package/src/common/url/location.test.js +0 -15
  93. package/src/common/url/parse-url.test.js +0 -110
  94. package/src/common/url/protocol.test.js +0 -17
  95. package/src/common/util/console.test.js +0 -34
  96. package/src/common/util/data-size.test.js +0 -56
  97. package/src/common/util/feature-flags.test.js +0 -94
  98. package/src/common/util/get-or-set.test.js +0 -58
  99. package/src/common/util/invoke.test.js +0 -65
  100. package/src/common/util/map-own.test.js +0 -52
  101. package/src/common/util/obfuscate.component-test.js +0 -173
  102. package/src/common/util/stringify.test.js +0 -49
  103. package/src/common/util/submit-data.test.js +0 -183
  104. package/src/common/util/traverse.test.js +0 -50
  105. package/src/common/vitals/cumulative-layout-shift.test.js +0 -71
  106. package/src/common/vitals/first-contentful-paint.test.js +0 -124
  107. package/src/common/vitals/first-input-delay.test.js +0 -88
  108. package/src/common/vitals/first-paint.test.js +0 -127
  109. package/src/common/vitals/interaction-to-next-paint.test.js +0 -74
  110. package/src/common/vitals/largest-contentful-paint.test.js +0 -94
  111. package/src/common/vitals/long-task.test.js +0 -122
  112. package/src/common/vitals/time-to-first-byte.test.js +0 -147
  113. package/src/common/vitals/vital-metric.test.js +0 -171
  114. package/src/common/wrap/wrap-promise.component-test.js +0 -110
  115. package/src/features/ajax/instrument/distributed-tracing.test.js +0 -375
  116. package/src/features/jserrors/aggregate/canonical-function-name.test.js +0 -13
  117. package/src/features/jserrors/aggregate/compute-stack-trace.test.js +0 -414
  118. package/src/features/jserrors/aggregate/format-stack-trace.test.js +0 -39
  119. package/src/features/jserrors/aggregate/string-hash-code.test.js +0 -12
  120. package/src/features/metrics/aggregate/framework-detection.test.js +0 -332
  121. package/src/features/page_view_timing/aggregate/index.component-test.js +0 -86
  122. package/src/features/session_replay/aggregate/index.component-test.js +0 -317
  123. package/src/features/spa/aggregate/interaction-node.test.js +0 -17
  124. package/src/features/utils/agent-session.test.js +0 -194
  125. package/src/features/utils/aggregate-base.test.js +0 -123
  126. package/src/features/utils/feature-base.test.js +0 -45
  127. package/src/features/utils/handler-cache.test.js +0 -72
  128. package/src/features/utils/instrument-base.test.js +0 -216
  129. package/src/features/utils/lazy-feature-loader.test.js +0 -37
  130. package/src/loaders/api/api.component-test.js +0 -45
  131. package/src/loaders/api/api.test.js +0 -85
  132. 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
- })