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