coveo.analytics 2.30.56 → 2.32.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 (84) hide show
  1. package/README.md +37 -43
  2. package/dist/browser.mjs +513 -357
  3. package/dist/coveoua.browser.js +1 -1
  4. package/dist/coveoua.browser.js.map +1 -1
  5. package/dist/coveoua.debug.js +542 -359
  6. package/dist/coveoua.debug.js.map +1 -1
  7. package/dist/coveoua.js +1 -1
  8. package/dist/coveoua.js.map +1 -1
  9. package/dist/definitions/client/analytics.d.ts +3 -2
  10. package/dist/definitions/client/runtimeEnvironment.d.ts +1 -1
  11. package/dist/definitions/donottrack.d.ts +1 -0
  12. package/dist/definitions/react-native/index.d.ts +1 -2
  13. package/dist/definitions/version.d.ts +1 -1
  14. package/dist/library.cjs +15350 -3699
  15. package/dist/library.es.js +513 -357
  16. package/dist/library.js +15350 -3699
  17. package/dist/library.mjs +15350 -3699
  18. package/dist/react-native.es.js +634 -460
  19. package/modules/package.json +9 -9
  20. package/package.json +60 -91
  21. package/react-native/package.json +7 -7
  22. package/src/caseAssist/caseAssistActions.ts +51 -50
  23. package/src/caseAssist/caseAssistClient.spec.ts +328 -297
  24. package/src/caseAssist/caseAssistClient.ts +201 -185
  25. package/src/client/analytics.spec.ts +724 -703
  26. package/src/client/analytics.ts +677 -602
  27. package/src/client/analyticsBeaconClient.spec.ts +195 -188
  28. package/src/client/analyticsBeaconClient.ts +99 -88
  29. package/src/client/analyticsFetchClient.ts +77 -61
  30. package/src/client/analyticsRequestClient.ts +17 -17
  31. package/src/client/location.ts +3 -3
  32. package/src/client/measurementProtocolMapper.ts +54 -45
  33. package/src/client/measurementProtocolMapping/baseMeasurementProtocolMapper.ts +48 -44
  34. package/src/client/measurementProtocolMapping/commerceMeasurementProtocolMapper.ts +133 -121
  35. package/src/client/measurementProtocolMapping/serviceMeasurementProtocolMapper.ts +17 -17
  36. package/src/client/noopAnalytics.ts +80 -68
  37. package/src/client/runtimeEnvironment.ts +50 -41
  38. package/src/client/utils.spec.ts +112 -97
  39. package/src/client/utils.ts +39 -39
  40. package/src/cookieutils.spec.ts +59 -0
  41. package/src/cookieutils.ts +40 -39
  42. package/src/coveoua/browser.ts +14 -12
  43. package/src/coveoua/library.ts +4 -4
  44. package/src/coveoua/plugins.ts +36 -34
  45. package/src/coveoua/simpleanalytics.spec.ts +467 -452
  46. package/src/coveoua/simpleanalytics.ts +146 -140
  47. package/src/detector.ts +17 -17
  48. package/src/donottrack.spec.ts +199 -121
  49. package/src/donottrack.ts +31 -8
  50. package/src/events.ts +86 -79
  51. package/src/formatting/format-array-for-coveo-custom-data.spec.ts +13 -12
  52. package/src/formatting/format-array-for-coveo-custom-data.ts +18 -18
  53. package/src/formatting/format-omnibox-metadata.ts +16 -12
  54. package/src/history.spec.ts +197 -195
  55. package/src/history.ts +143 -133
  56. package/src/hook/addDefaultValues.ts +13 -8
  57. package/src/hook/enhanceViewEvent.ts +17 -17
  58. package/src/insight/insightClient.spec.ts +1848 -1717
  59. package/src/insight/insightClient.ts +866 -761
  60. package/src/insight/insightEvents.ts +57 -56
  61. package/src/plugins/BasePlugin.ts +125 -121
  62. package/src/plugins/ec.spec.ts +457 -429
  63. package/src/plugins/ec.ts +202 -199
  64. package/src/plugins/link.spec.ts +183 -159
  65. package/src/plugins/link.ts +88 -85
  66. package/src/plugins/svc.spec.ts +161 -155
  67. package/src/plugins/svc.ts +93 -91
  68. package/src/react-native/index.ts +8 -1
  69. package/src/react-native/react-native-runtime.ts +33 -33
  70. package/src/react-native/react-native-utils.ts +1 -1
  71. package/src/react-native/react-native.spec.ts +31 -21
  72. package/src/searchPage/searchPageClient.spec.ts +1944 -1794
  73. package/src/searchPage/searchPageClient.ts +1261 -1040
  74. package/src/searchPage/searchPageEvents.ts +524 -511
  75. package/src/storage.spec.ts +51 -51
  76. package/src/storage.ts +47 -47
  77. package/dist/definitions/bundle/browser-fetch.d.ts +0 -1
  78. package/dist/definitions/src/coveoua/browser.d.ts +0 -6
  79. package/dist/definitions/src/coveoua/headless.d.ts +0 -6
  80. package/dist/definitions/src/coveoua/library.d.ts +0 -17
  81. package/dist/definitions/src/coveoua/plugins.d.ts +0 -10
  82. package/dist/definitions/src/coveoua/simpleanalytics.d.ts +0 -33
  83. package/dist/definitions/src/react-native/index.d.ts +0 -3
  84. package/dist/definitions/src/react-native/react-native-runtime.d.ts +0 -19
@@ -1,3 +1,5 @@
1
+ import {vi} from 'vitest';
2
+ import type {Mock} from 'vitest';
1
3
  import coveoua from './simpleanalytics';
2
4
  import {createAnalyticsClientMock, visitorIdMock} from '../../tests/analyticsClientMock';
3
5
  import {TestPlugin} from '../../tests/pluginMock';
@@ -7,539 +9,552 @@ import {mockFetch, lastCallBody} from '../../tests/fetchMock';
7
9
  import {CookieStorage} from '../storage';
8
10
  import {libVersion} from '../version';
9
11
 
10
- const uuidv4Mock = jest.fn();
11
- jest.mock('uuid', () => ({v4: () => uuidv4Mock()}));
12
+ const uuidv4Mock = vi.fn();
13
+ vi.mock('uuid', () => ({v4: () => uuidv4Mock()}));
12
14
 
13
15
  const {fetchMock, fetchMockBeforeEach} = mockFetch();
14
16
 
15
17
  class TestPluginWithSpy extends TestPlugin {
16
- public static readonly Id: 'test';
17
- public static spy: jest.Mock;
18
- constructor({client, uuidGenerator = uuidv4}: PluginOptions) {
19
- super({client, uuidGenerator});
20
- TestPluginWithSpy.spy = jest.fn();
18
+ public static readonly Id: 'test';
19
+ public static spy: Mock;
20
+ constructor({client, uuidGenerator = uuidv4}: PluginOptions) {
21
+ super({client, uuidGenerator});
22
+ TestPluginWithSpy.spy = vi.fn();
23
+ }
24
+
25
+ public getApi(name: string): Function | null {
26
+ switch (name) {
27
+ case 'testMethod':
28
+ return this.testMethod;
29
+ default:
30
+ return null;
21
31
  }
32
+ }
22
33
 
23
- public getApi(name: string): Function | null {
24
- switch (name) {
25
- case 'testMethod':
26
- return this.testMethod;
27
- default:
28
- return null;
29
- }
30
- }
31
-
32
- testMethod(...args: any[]) {
33
- TestPluginWithSpy.spy(args);
34
- }
34
+ testMethod(...args: any[]) {
35
+ TestPluginWithSpy.spy(args);
36
+ }
35
37
  }
36
38
 
37
39
  describe('simpleanalytics', () => {
38
- const analyticsClientMock = createAnalyticsClientMock();
39
- const analyticsEndpoint = 'https://analytics.cloud.coveo.com/rest/ua/v15/analytics';
40
- const someRandomEventName = 'kawabunga';
40
+ const analyticsClientMock = createAnalyticsClientMock();
41
+ const analyticsEndpoint = 'https://analytics.cloud.coveo.com/rest/ua/v15/analytics';
42
+ const someRandomEventName = 'testeventname';
43
+
44
+ beforeEach(() => {
45
+ vi.clearAllMocks();
46
+ fetchMockBeforeEach();
47
+
48
+ new CookieStorage().removeItem('visitorId');
49
+ localStorage.clear();
50
+ fetchMock.mock('*', {});
51
+ uuidv4Mock.mockImplementationOnce(() => visitorIdMock);
52
+ coveoua('reset');
53
+ });
54
+
55
+ afterEach(() => {
56
+ fetchMock.reset();
57
+ fetchMock.resetHistory();
58
+ fetchMock.resetBehavior();
59
+ });
60
+
61
+ describe('init', () => {
62
+ it('throws when initializing without a token', () => {
63
+ expect(() => coveoua('init')).toThrow(`You must pass your token when you call 'init'`);
64
+ });
41
65
 
42
- beforeEach(() => {
43
- jest.clearAllMocks();
44
- fetchMockBeforeEach();
66
+ it('throws when initializing with a token that is not a string nor a AnalyticClient', () => {
67
+ expect(() => coveoua('init', {})).toThrow(
68
+ `You must pass either your token or a valid object when you call 'init'`
69
+ );
70
+ });
45
71
 
46
- new CookieStorage().removeItem('visitorId');
47
- localStorage.clear();
48
- fetchMock.mock('*', {});
49
- uuidv4Mock.mockImplementationOnce(() => visitorIdMock);
50
- coveoua('reset');
72
+ it('can initialize with analyticsClient', () => {
73
+ expect(() => coveoua('init', analyticsClientMock)).not.toThrow();
51
74
  });
52
75
 
53
- afterEach(() => {
54
- fetchMock.reset();
55
- fetchMock.resetHistory();
56
- fetchMock.resetBehavior();
76
+ it('can initialize with a token', () => {
77
+ expect(() => coveoua('init', 'SOME TOKEN')).not.toThrow();
57
78
  });
58
79
 
59
- describe('init', () => {
60
- it('throws when initializing without a token', () => {
61
- expect(() => coveoua('init')).toThrow(`You must pass your token when you call 'init'`);
62
- });
80
+ it('default to analytics.cloud.coveo.com when no endpoint is given', async () => {
81
+ coveoua('init', 'SOME TOKEN');
63
82
 
64
- it('throws when initializing with a token that is not a string nor a AnalyticClient', () => {
65
- expect(() => coveoua('init', {})).toThrow(
66
- `You must pass either your token or a valid object when you call 'init'`,
67
- );
68
- });
83
+ await coveoua('send', 'pageview');
69
84
 
70
- it('can initialize with analyticsClient', () => {
71
- expect(() => coveoua('init', analyticsClientMock)).not.toThrow();
72
- });
85
+ expect(fetchMock.calls().length).toBe(1);
86
+ const foo = fetchMock.lastUrl();
87
+ expect(fetchMock.lastUrl()).toMatch(/^https:\/\/analytics\.cloud\.coveo\.com\/rest\/ua/);
88
+ });
73
89
 
74
- it('can initialize with a token', () => {
75
- expect(() => coveoua('init', 'SOME TOKEN')).not.toThrow();
76
- });
90
+ it('default to analytics.cloud.coveo.com when the endpoint is an empty string', async () => {
91
+ coveoua('init', 'SOME TOKEN', '');
77
92
 
78
- it('default to analytics.cloud.coveo.com when no endpoint is given', async () => {
79
- coveoua('init', 'SOME TOKEN');
93
+ await coveoua('send', 'pageview');
80
94
 
81
- await coveoua('send', 'pageview');
95
+ expect(fetchMock.calls().length).toBe(1);
96
+ const foo = fetchMock.lastUrl();
97
+ expect(fetchMock.lastUrl()).toMatch(/^https:\/\/analytics\.cloud\.coveo\.com\/rest\/ua/);
98
+ });
82
99
 
83
- expect(fetchMock.calls().length).toBe(1);
84
- const foo = fetchMock.lastUrl();
85
- expect(fetchMock.lastUrl()).toMatch(/^https:\/\/analytics\.cloud\.coveo\.com\/rest\/ua/);
86
- });
100
+ it('default to analytics.cloud.coveo.com when an options object is given but does not include an endpoint', async () => {
101
+ coveoua('init', 'SOME TOKEN', {});
87
102
 
88
- it('default to analytics.cloud.coveo.com when the endpoint is an empty string', async () => {
89
- coveoua('init', 'SOME TOKEN', '');
103
+ await coveoua('send', 'pageview');
90
104
 
91
- await coveoua('send', 'pageview');
105
+ expect(fetchMock.calls().length).toBe(1);
106
+ expect(fetchMock.lastUrl()).toMatch(/^https:\/\/analytics\.cloud\.coveo\.com\/rest\/ua/);
107
+ });
92
108
 
93
- expect(fetchMock.calls().length).toBe(1);
94
- const foo = fetchMock.lastUrl();
95
- expect(fetchMock.lastUrl()).toMatch(/^https:\/\/analytics\.cloud\.coveo\.com\/rest\/ua/);
96
- });
109
+ it('default to analytics.cloud.coveo.com when an options object is given but the endpoint property is falsy', async () => {
110
+ coveoua('init', 'SOME TOKEN', {endpoint: ''});
97
111
 
98
- it('default to analytics.cloud.coveo.com when an options object is given but does not include an endpoint', async () => {
99
- coveoua('init', 'SOME TOKEN', {});
112
+ await coveoua('send', 'pageview');
100
113
 
101
- await coveoua('send', 'pageview');
114
+ expect(fetchMock.calls().length).toBe(1);
115
+ expect(fetchMock.lastUrl()).toMatch(/^https:\/\/analytics\.cloud\.coveo\.com\/rest\/ua/);
116
+ });
102
117
 
103
- expect(fetchMock.calls().length).toBe(1);
104
- expect(fetchMock.lastUrl()).toMatch(/^https:\/\/analytics\.cloud\.coveo\.com\/rest\/ua/);
105
- });
118
+ it('uses the endpoint given if its a non-empty string', async () => {
119
+ coveoua('init', 'SOME TOKEN', 'https://someendpoint.com');
106
120
 
107
- it('default to analytics.cloud.coveo.com when an options object is given but the endpoint property is falsy', async () => {
108
- coveoua('init', 'SOME TOKEN', {endpoint: ''});
121
+ await coveoua('send', 'pageview');
109
122
 
110
- await coveoua('send', 'pageview');
123
+ expect(fetchMock.calls().length).toBe(1);
124
+ expect(fetchMock.lastUrl()).toMatch(/^https:\/\/someendpoint\.com/);
125
+ });
111
126
 
112
- expect(fetchMock.calls().length).toBe(1);
113
- expect(fetchMock.lastUrl()).toMatch(/^https:\/\/analytics\.cloud\.coveo\.com\/rest\/ua/);
114
- });
127
+ it('uses the endpoint given if the options object include a non-empty string', async () => {
128
+ coveoua('init', 'SOME TOKEN', {endpoint: 'https://someendpoint.com'});
115
129
 
116
- it('uses the endpoint given if its a non-empty string', async () => {
117
- coveoua('init', 'SOME TOKEN', 'https://someendpoint.com');
130
+ await coveoua('send', 'pageview');
118
131
 
119
- await coveoua('send', 'pageview');
132
+ expect(fetchMock.calls().length).toBe(1);
133
+ expect(fetchMock.lastUrl()).toMatch(/^https:\/\/someendpoint\.com/);
134
+ });
120
135
 
121
- expect(fetchMock.calls().length).toBe(1);
122
- expect(fetchMock.lastUrl()).toMatch(/^https:\/\/someendpoint\.com/);
123
- });
136
+ it('uses EC, SVC and Link plugins by default', () => {
137
+ coveoua('init', 'SOME TOKEN');
138
+ expect(() => coveoua('callPlugin', 'ec', 'missingFunction')).toThrow(/does not exist/);
139
+ expect(() => coveoua('callPlugin', 'svc', 'missingFunction')).toThrow(/does not exist/);
140
+ expect(() => coveoua('callPlugin', 'link', 'missingFunction')).toThrow(/does not exist/);
141
+ });
124
142
 
125
- it('uses the endpoint given if the options object include a non-empty string', async () => {
126
- coveoua('init', 'SOME TOKEN', {endpoint: 'https://someendpoint.com'});
143
+ it('can accept no plugins', () => {
144
+ coveoua('init', 'SOME TOKEN', {plugins: []});
145
+ expect(() => coveoua('callPlugin', 'ec', 'missingFunction')).toThrow(/is not required/);
146
+ expect(() => coveoua('callPlugin', 'svc', 'missingFunction')).toThrow(/is not required/);
147
+ expect(() => coveoua('callPlugin', 'link', 'missingFunction')).toThrow(/is not required/);
148
+ });
127
149
 
128
- await coveoua('send', 'pageview');
150
+ it('can accept one plugin', () => {
151
+ coveoua('init', 'SOME TOKEN', {plugins: ['svc']});
152
+ expect(() => coveoua('callPlugin', 'ec', 'missingFunction')).toThrow(/is not required/);
153
+ expect(() => coveoua('callPlugin', 'link', 'missingFunction')).toThrow(/is not required/);
154
+ expect(() => coveoua('callPlugin', 'svc', 'missingFunction')).toThrow(/does not exist/);
155
+ });
129
156
 
130
- expect(fetchMock.calls().length).toBe(1);
131
- expect(fetchMock.lastUrl()).toMatch(/^https:\/\/someendpoint\.com/);
132
- });
133
-
134
- it('uses EC, SVC and Link plugins by default', () => {
135
- coveoua('init', 'SOME TOKEN');
136
- expect(() => coveoua('callPlugin', 'ec', 'nosuchfunction')).toThrow(/does not exist/);
137
- expect(() => coveoua('callPlugin', 'svc', 'nosuchfunction')).toThrow(/does not exist/);
138
- expect(() => coveoua('callPlugin', 'link', 'nosuchfunction')).toThrow(/does not exist/);
139
- });
140
-
141
- it('can accept no plugins', () => {
142
- coveoua('init', 'SOME TOKEN', {plugins: []});
143
- expect(() => coveoua('callPlugin', 'ec', 'nosuchfunction')).toThrow(/is not required/);
144
- expect(() => coveoua('callPlugin', 'svc', 'nosuchfunction')).toThrow(/is not required/);
145
- expect(() => coveoua('callPlugin', 'link', 'nosuchfunction')).toThrow(/is not required/);
146
- });
147
-
148
- it('can accept one plugin', () => {
149
- coveoua('init', 'SOME TOKEN', {plugins: ['svc']});
150
- expect(() => coveoua('callPlugin', 'ec', 'nosuchfunction')).toThrow(/is not required/);
151
- expect(() => coveoua('callPlugin', 'link', 'nosuchfunction')).toThrow(/is not required/);
152
- expect(() => coveoua('callPlugin', 'svc', 'nosuchfunction')).toThrow(/does not exist/);
153
- });
154
-
155
- it('can send pageview with analyticsClient', () => {
156
- coveoua('init', analyticsClientMock);
157
- expect(() => coveoua('send', 'pageview')).not.toThrow();
158
- });
159
- });
160
-
161
- describe('initForProxy', () => {
162
- it(`throw if the initForProxy don't receive an endpoint`, () => {
163
- expect(() => coveoua('initForProxy')).toThrow(`You must pass your endpoint when you call 'initForProxy'`);
164
- });
165
-
166
- it(`throw if the initForProxy receive an endpoint that is not a string`, () => {
167
- expect(() => coveoua('initForProxy', {})).toThrow(
168
- `You must pass a string as the endpoint parameter when you call 'initForProxy'`,
169
- );
170
- });
171
- });
172
-
173
- describe('send', () => {
174
- it('throws when not initialized', () => {
175
- expect(() => coveoua('send')).toThrow(`You must call init before sending an event`);
176
- });
177
-
178
- it('throws when send is called without any other arguments', () => {
179
- coveoua('init', 'MYTOKEN');
180
- expect(() => coveoua('send')).toThrow(`You must provide an event type when calling "send".`);
181
- });
182
-
183
- it('can send pageview', async () => {
184
- coveoua('init', 'MYTOKEN', {plugins: []});
185
- await coveoua('send', 'pageview');
186
-
187
- expect(fetchMock.calls().length).toBe(1);
188
- expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/pageview`);
189
- });
190
-
191
- it('can send pageview with customdata', async () => {
192
- coveoua('init', 'MYTOKEN', {plugins: []});
193
- await coveoua('send', 'pageview', {somedata: 'asd'});
194
-
195
- expect(fetchMock.calls().length).toBe(1);
196
- expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/pageview`);
197
- expect(JSON.parse(lastCallBody(fetchMock))).toEqual({somedata: 'asd'});
198
- });
199
-
200
- it('can send view event with clientId', async () => {
201
- coveoua('init', 'MYTOKEN', {plugins: []});
202
- await coveoua('send', 'view');
203
-
204
- expect(fetchMock.calls().length).toBe(1);
205
- expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/view?visitor=${visitorIdMock}`);
206
- expect(JSON.parse(lastCallBody(fetchMock)).clientId).toBe(visitorIdMock);
207
- });
208
-
209
- it('can send any event to the endpoint', async () => {
210
- coveoua('init', 'MYTOKEN');
211
- await coveoua('send', someRandomEventName);
212
-
213
- expect(fetchMock.calls().length).toBe(1);
214
- expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
215
- });
216
-
217
- it('can send an event with a proxy endpoint', async () => {
218
- coveoua('initForProxy', 'https://myProxyEndpoint.com');
219
- await coveoua('send', someRandomEventName);
220
-
221
- expect(fetchMock.calls().length).toBe(1);
222
- expect(fetchMock.lastUrl()).toBe(`https://myproxyendpoint.com/rest/v15/analytics/${someRandomEventName}`);
223
- });
224
-
225
- describe('will restrict referrers and url lengths', () => {
226
- const max: number = 2048;
227
- const reallyLongPath1: string = 'http://coveo.com/?q=' + 'a'.repeat(max);
228
- const reallyLongPath2: string = 'http://coveo.com/?q=' + 'b'.repeat(max);
229
- expect(reallyLongPath1.length).toBeGreaterThan(max);
230
- expect(reallyLongPath2.length).toBeGreaterThan(max);
231
- it('does this for pageviews', async () => {
232
- coveoua('init', 'MYTOKEN');
233
- await coveoua('send', 'pageview', reallyLongPath1);
234
- await coveoua('send', 'pageview', reallyLongPath2);
235
- const body = JSON.parse(lastCallBody(fetchMock));
236
- expect(body.dr.length).toBeLessThanOrEqual(max);
237
- expect(body.dl.length).toBeLessThanOrEqual(max);
238
- expect(body.dp.length).toBeLessThanOrEqual(max);
239
- });
240
- it('does this for generic events', async () => {
241
- coveoua('init', 'MYTOKEN');
242
- await coveoua('send', 'pageview', reallyLongPath1);
243
- await coveoua('send', 'pageview', reallyLongPath2);
244
- await coveoua('send', 'event');
245
- const body = JSON.parse(lastCallBody(fetchMock));
246
- expect(body.dr.length).toBeLessThanOrEqual(max);
247
- expect(body.dl.length).toBeLessThanOrEqual(max);
248
- });
249
- });
250
- });
251
-
252
- describe('set', () => {
253
- it('can set a new parameter', async () => {
254
- coveoua('init', 'MYTOKEN');
255
- coveoua('set', 'userId', 'something');
256
- await coveoua('send', someRandomEventName);
257
-
258
- expect(fetchMock.calls().length).toBe(1);
259
- expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
260
- expect(JSON.parse(lastCallBody(fetchMock))).toEqual({userId: 'something'});
261
- });
262
-
263
- it('can set parameters using an object', async () => {
264
- coveoua('init', 'MYTOKEN');
265
- coveoua('set', {
266
- userId: 'something',
267
- });
268
- await coveoua('send', someRandomEventName);
269
-
270
- expect(fetchMock.calls().length).toBe(1);
271
- expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
272
- expect(JSON.parse(lastCallBody(fetchMock))).toEqual({userId: 'something'});
273
- });
274
-
275
- it('can set a custom_website parameter on a collect event', async () => {
276
- coveoua('init', 'MYTOKEN', {plugins: ['ec']});
277
- coveoua('set', 'custom', {context_website: 'MY_WEBSITE'});
278
- await coveoua('send', 'pageview');
279
-
280
- expect(fetchMock.calls().length).toBe(1);
281
- let result = JSON.parse(lastCallBody(fetchMock));
282
- expect(result).toHaveProperty('context_website', 'MY_WEBSITE');
283
- });
284
-
285
- it('does not set custom parameters which are strings', async () => {
286
- coveoua('init', 'MYTOKEN', {plugins: ['ec']});
287
- coveoua('set', 'custom', 'test');
288
- await coveoua('send', 'pageview');
289
-
290
- expect(fetchMock.calls().length).toBe(1);
291
- let result = JSON.parse(lastCallBody(fetchMock));
292
- expect(Object.keys(result).length).toBe(11);
293
- });
294
-
295
- it('does not set custom parameters which are arrays', async () => {
296
- coveoua('init', 'MYTOKEN', {plugins: ['ec']});
297
- coveoua('set', 'custom', ['test']);
298
- await coveoua('send', 'pageview');
299
-
300
- expect(fetchMock.calls().length).toBe(1);
301
- let result = JSON.parse(lastCallBody(fetchMock));
302
- expect(Object.keys(result).length).toBe(11);
303
- });
304
-
305
- it('does not set custom parameters which are null', async () => {
306
- coveoua('init', 'MYTOKEN', {plugins: ['ec']});
307
- coveoua('set', 'custom', null);
308
- await coveoua('send', 'pageview');
309
-
310
- expect(fetchMock.calls().length).toBe(1);
311
- let result = JSON.parse(lastCallBody(fetchMock));
312
- expect(Object.keys(result).length).toBe(11);
313
- });
314
-
315
- it('does not set custom parameters which are undefined', async () => {
316
- coveoua('init', 'MYTOKEN', {plugins: ['ec']});
317
- coveoua('set', 'custom', undefined);
318
- await coveoua('send', 'pageview');
319
-
320
- expect(fetchMock.calls().length).toBe(1);
321
- let result = JSON.parse(lastCallBody(fetchMock));
322
- expect(Object.keys(result).length).toBe(11);
323
- });
324
-
325
- it('can set a custom_website parameter on a non-collect event', async () => {
326
- coveoua('init', 'MYTOKEN', {plugins: []});
327
- coveoua('set', 'custom', {context_website: 'MY_WEBSITE'});
328
- await coveoua('send', 'view', {somedata: 'something'});
329
-
330
- expect(fetchMock.calls().length).toBe(1);
331
- let result = JSON.parse(lastCallBody(fetchMock));
332
- expect(result).toHaveProperty('somedata', 'something');
333
- expect(result).toHaveProperty('customData.context_website', 'MY_WEBSITE');
334
- });
335
-
336
- it('does not add a customData entry for custom params which are null', async () => {
337
- coveoua('init', 'MYTOKEN', {plugins: []});
338
- coveoua('set', 'custom', null);
339
- await coveoua('send', 'view', {somedata: 'something'});
340
-
341
- expect(fetchMock.calls().length).toBe(1);
342
- let result = JSON.parse(lastCallBody(fetchMock));
343
- expect(result).toHaveProperty('somedata', 'something');
344
- expect(result).not.toHaveProperty('customData');
345
- });
346
-
347
- it('does not add a customData entry for custom params which are undefined', async () => {
348
- coveoua('init', 'MYTOKEN', {plugins: []});
349
- coveoua('set', 'custom', undefined);
350
- await coveoua('send', 'view', {somedata: 'something'});
351
-
352
- expect(fetchMock.calls().length).toBe(1);
353
- let result = JSON.parse(lastCallBody(fetchMock));
354
- expect(result).toHaveProperty('somedata', 'something');
355
- expect(result).not.toHaveProperty('customData');
356
- });
357
-
358
- it('does not add a customData entry for customData params which are strings', async () => {
359
- coveoua('init', 'MYTOKEN', {plugins: []});
360
- coveoua('set', 'custom', 'test');
361
- await coveoua('send', 'view', {somedata: 'something'});
362
-
363
- expect(fetchMock.calls().length).toBe(1);
364
- let result = JSON.parse(lastCallBody(fetchMock));
365
- expect(result).toHaveProperty('somedata', 'something');
366
- expect(result).not.toHaveProperty('customData');
367
- });
368
-
369
- it('does not add a customData entry for customData params which are arrays', async () => {
370
- coveoua('init', 'MYTOKEN', [{plugins: []}]);
371
- coveoua('set', 'custom', [test]);
372
- await coveoua('send', 'view', {somedata: 'something'});
373
-
374
- expect(fetchMock.calls().length).toBe(1);
375
- let result = JSON.parse(lastCallBody(fetchMock));
376
- expect(result).toHaveProperty('somedata', 'something');
377
- expect(result).not.toHaveProperty('customData');
378
- });
379
-
380
- it('will not override hardcoded customData parameters', async () => {
381
- coveoua('init', 'MYTOKEN', {plugins: []});
382
- coveoua('set', 'custom', {context_website: 'MY_WEBSITE'});
383
- await coveoua('send', 'view', {somedata: 'something', customData: {context_website: 'MY_OTHER_WEBSITE'}});
384
-
385
- expect(fetchMock.calls().length).toBe(1);
386
- let result = JSON.parse(lastCallBody(fetchMock));
387
- expect(result).toHaveProperty('somedata', 'something');
388
- expect(result).toHaveProperty('customData.context_website', 'MY_OTHER_WEBSITE');
389
- });
390
-
391
- it('can set trackingId', async () => {
392
- const trackingId = 'yourbestfriend';
393
- coveoua('init', 'MYTOKEN');
394
- coveoua('set', 'trackingId', trackingId);
395
- await coveoua('send', someRandomEventName);
396
-
397
- expect(fetchMock.calls().length).toBe(1);
398
- expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
399
- expect(JSON.parse(lastCallBody(fetchMock))).toHaveProperty('trackingId', trackingId);
400
- });
157
+ it('can send pageview with analyticsClient', () => {
158
+ coveoua('init', analyticsClientMock);
159
+ expect(() => coveoua('send', 'pageview')).not.toThrow();
160
+ });
161
+ });
401
162
 
402
- it('context_website does not overwrite trackingId', async () => {
403
- const trackingId = 'yourbestfriend';
404
- const contextWebsite = 'yoursite';
405
- coveoua('init', 'MYTOKEN');
406
- coveoua('set', 'custom', {context_website: contextWebsite});
407
- coveoua('set', 'trackingId', trackingId);
408
- await coveoua('send', someRandomEventName);
163
+ describe('initForProxy', () => {
164
+ it(`throw if the initForProxy don't receive an endpoint`, () => {
165
+ expect(() => coveoua('initForProxy')).toThrow(
166
+ `You must pass your endpoint when you call 'initForProxy'`
167
+ );
168
+ });
409
169
 
410
- expect(fetchMock.calls().length).toBe(1);
411
- expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
412
- expect(JSON.parse(lastCallBody(fetchMock))).toHaveProperty('trackingId', trackingId);
413
- });
170
+ it(`throw if the initForProxy receive an endpoint that is not a string`, () => {
171
+ expect(() => coveoua('initForProxy', {})).toThrow(
172
+ `You must pass a string as the endpoint parameter when you call 'initForProxy'`
173
+ );
414
174
  });
175
+ });
415
176
 
416
- describe('onLoad', () => {
417
- it('can execute callback with onLoad event', () => {
418
- var callback = jest.fn();
177
+ describe('send', () => {
178
+ it('throws when not initialized', () => {
179
+ expect(() => coveoua('send')).toThrow(`You must call init before sending an event`);
180
+ });
419
181
 
420
- coveoua('onLoad', callback);
182
+ it('throws when send is called without any other arguments', () => {
183
+ coveoua('init', 'MYTOKEN');
184
+ expect(() => coveoua('send')).toThrow(`You must provide an event type when calling "send".`);
185
+ });
421
186
 
422
- expect(callback).toHaveBeenCalledTimes(1);
423
- });
187
+ it('can send pageview', async () => {
188
+ coveoua('init', 'MYTOKEN', {plugins: []});
189
+ await coveoua('send', 'pageview');
424
190
 
425
- it('throws when registering an invalid onLoad event', () => {
426
- expect(() => coveoua('onLoad', undefined)).toThrow();
427
- });
191
+ expect(fetchMock.calls().length).toBe(1);
192
+ expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/pageview`);
428
193
  });
429
194
 
430
- describe('provide', () => {
431
- it('register properly', () => {
432
- coveoua('provide', 'test', TestPlugin);
195
+ it('can send pageview with customdata', async () => {
196
+ coveoua('init', 'MYTOKEN', {plugins: []});
197
+ await coveoua('send', 'pageview', {somedata: 'asd'});
433
198
 
434
- coveoua('init', 'MYTOKEN');
199
+ expect(fetchMock.calls().length).toBe(1);
200
+ expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/pageview`);
201
+ expect(JSON.parse(lastCallBody(fetchMock))).toEqual({somedata: 'asd'});
202
+ });
435
203
 
436
- expect(() => coveoua('require', 'test')).not.toThrow();
437
- });
204
+ it('can send view event with clientId', async () => {
205
+ coveoua('init', 'MYTOKEN', {plugins: []});
206
+ await coveoua('send', 'view');
207
+
208
+ expect(fetchMock.calls().length).toBe(1);
209
+ expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/view?visitor=${visitorIdMock}`);
210
+ expect(JSON.parse(lastCallBody(fetchMock)).clientId).toBe(visitorIdMock);
438
211
  });
439
212
 
440
- describe('version', () => {
441
- it('returns the current version string', () => {
442
- coveoua('init', 'MYTOKEN');
443
- expect(coveoua('version')).toBe(libVersion);
444
- });
213
+ it('can send any event to the endpoint', async () => {
214
+ coveoua('init', 'MYTOKEN');
215
+ await coveoua('send', someRandomEventName);
216
+
217
+ expect(fetchMock.calls().length).toBe(1);
218
+ expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
445
219
  });
446
220
 
447
- describe('callPlugin', () => {
448
- it('resolves properly plugin actions', () => {
449
- coveoua('provide', 'test', TestPluginWithSpy);
450
- coveoua('init', 'MYTOKEN', {plugins: ['test']});
221
+ it('can send an event with a proxy endpoint', async () => {
222
+ coveoua('initForProxy', 'https://myProxyEndpoint.com');
223
+ await coveoua('send', someRandomEventName);
451
224
 
452
- coveoua('callPlugin', 'test', 'testMethod', 'foo', 'bar');
225
+ expect(fetchMock.calls().length).toBe(1);
226
+ expect(fetchMock.lastUrl()).toBe(
227
+ `https://myproxyendpoint.com/rest/v15/analytics/${someRandomEventName}`
228
+ );
229
+ });
230
+
231
+ describe('will restrict referrers and url lengths', () => {
232
+ const max: number = 2048;
233
+ const reallyLongPath1: string = 'http://coveo.com/?q=' + 'a'.repeat(max);
234
+ const reallyLongPath2: string = 'http://coveo.com/?q=' + 'b'.repeat(max);
235
+ expect(reallyLongPath1.length).toBeGreaterThan(max);
236
+ expect(reallyLongPath2.length).toBeGreaterThan(max);
237
+ it('does this for pageviews', async () => {
238
+ coveoua('init', 'MYTOKEN');
239
+ await coveoua('send', 'pageview', reallyLongPath1);
240
+ await coveoua('send', 'pageview', reallyLongPath2);
241
+ const body = JSON.parse(lastCallBody(fetchMock));
242
+ expect(body.dr.length).toBeLessThanOrEqual(max);
243
+ expect(body.dl.length).toBeLessThanOrEqual(max);
244
+ expect(body.dp.length).toBeLessThanOrEqual(max);
245
+ });
246
+ it('does this for generic events', async () => {
247
+ coveoua('init', 'MYTOKEN');
248
+ await coveoua('send', 'pageview', reallyLongPath1);
249
+ await coveoua('send', 'pageview', reallyLongPath2);
250
+ await coveoua('send', 'event');
251
+ const body = JSON.parse(lastCallBody(fetchMock));
252
+ expect(body.dr.length).toBeLessThanOrEqual(max);
253
+ expect(body.dl.length).toBeLessThanOrEqual(max);
254
+ });
255
+ });
256
+ });
257
+
258
+ describe('set', () => {
259
+ it('can set a new parameter', async () => {
260
+ coveoua('init', 'MYTOKEN');
261
+ coveoua('set', 'userId', 'something');
262
+ await coveoua('send', someRandomEventName);
263
+
264
+ expect(fetchMock.calls().length).toBe(1);
265
+ expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
266
+ expect(JSON.parse(lastCallBody(fetchMock))).toEqual({
267
+ userId: 'something',
268
+ });
269
+ });
270
+
271
+ it('can set parameters using an object', async () => {
272
+ coveoua('init', 'MYTOKEN');
273
+ coveoua('set', {
274
+ userId: 'something',
275
+ });
276
+ await coveoua('send', someRandomEventName);
277
+
278
+ expect(fetchMock.calls().length).toBe(1);
279
+ expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
280
+ expect(JSON.parse(lastCallBody(fetchMock))).toEqual({
281
+ userId: 'something',
282
+ });
283
+ });
284
+
285
+ it('can set a custom_website parameter on a collect event', async () => {
286
+ coveoua('init', 'MYTOKEN', {plugins: ['ec']});
287
+ coveoua('set', 'custom', {context_website: 'MY_WEBSITE'});
288
+ await coveoua('send', 'pageview');
289
+
290
+ expect(fetchMock.calls().length).toBe(1);
291
+ let result = JSON.parse(lastCallBody(fetchMock));
292
+ expect(result).toHaveProperty('context_website', 'MY_WEBSITE');
293
+ });
294
+
295
+ it('does not set custom parameters which are strings', async () => {
296
+ coveoua('init', 'MYTOKEN', {plugins: ['ec']});
297
+ coveoua('set', 'custom', 'test');
298
+ await coveoua('send', 'pageview');
453
299
 
454
- expect(TestPluginWithSpy.spy).toHaveBeenCalledTimes(1);
455
- expect(TestPluginWithSpy.spy).toHaveBeenCalledWith(['foo', 'bar']);
456
- });
300
+ expect(fetchMock.calls().length).toBe(1);
301
+ let result = JSON.parse(lastCallBody(fetchMock));
302
+ expect(Object.keys(result).length).toBe(11);
303
+ });
304
+
305
+ it('does not set custom parameters which are arrays', async () => {
306
+ coveoua('init', 'MYTOKEN', {plugins: ['ec']});
307
+ coveoua('set', 'custom', ['test']);
308
+ await coveoua('send', 'pageview');
309
+
310
+ expect(fetchMock.calls().length).toBe(1);
311
+ let result = JSON.parse(lastCallBody(fetchMock));
312
+ expect(Object.keys(result).length).toBe(11);
313
+ });
314
+
315
+ it('does not set custom parameters which are null', async () => {
316
+ coveoua('init', 'MYTOKEN', {plugins: ['ec']});
317
+ coveoua('set', 'custom', null);
318
+ await coveoua('send', 'pageview');
457
319
 
458
- it('throws when a namespaced action is called and that the namespace/plugin is not required', () => {
459
- coveoua('init', 'SOME TOKEN', {plugins: ['ec']});
320
+ expect(fetchMock.calls().length).toBe(1);
321
+ let result = JSON.parse(lastCallBody(fetchMock));
322
+ expect(Object.keys(result).length).toBe(11);
323
+ });
324
+
325
+ it('does not set custom parameters which are undefined', async () => {
326
+ coveoua('init', 'MYTOKEN', {plugins: ['ec']});
327
+ coveoua('set', 'custom', undefined);
328
+ await coveoua('send', 'pageview');
460
329
 
461
- expect(() => coveoua('callPlugin', 'svc', 'setTicket')).toThrow(/is not required/);
462
- });
330
+ expect(fetchMock.calls().length).toBe(1);
331
+ let result = JSON.parse(lastCallBody(fetchMock));
332
+ expect(Object.keys(result).length).toBe(11);
333
+ });
463
334
 
464
- it('throws when a namespaced action is called and that this action does not exists on the plugin', () => {
465
- coveoua('init', 'SOME TOKEN', {plugins: ['svc']});
335
+ it('can set a custom_website parameter on a non-collect event', async () => {
336
+ coveoua('init', 'MYTOKEN', {plugins: []});
337
+ coveoua('set', 'custom', {context_website: 'MY_WEBSITE'});
338
+ await coveoua('send', 'view', {somedata: 'something'});
466
339
 
467
- expect(() => coveoua('callPlugin', 'svc', 'fooBarBaz')).toThrow(/does not exist/);
468
- });
340
+ expect(fetchMock.calls().length).toBe(1);
341
+ let result = JSON.parse(lastCallBody(fetchMock));
342
+ expect(result).toHaveProperty('somedata', 'something');
343
+ expect(result).toHaveProperty('customData.context_website', 'MY_WEBSITE');
469
344
  });
470
345
 
471
- describe('require', () => {
472
- it('can require a plugin', () => {
473
- coveoua('provide', 'test', TestPlugin);
474
- coveoua('init', 'MYTOKEN', {plugins: []});
346
+ it('does not add a customData entry for custom params which are null', async () => {
347
+ coveoua('init', 'MYTOKEN', {plugins: []});
348
+ coveoua('set', 'custom', null);
349
+ await coveoua('send', 'view', {somedata: 'something'});
350
+
351
+ expect(fetchMock.calls().length).toBe(1);
352
+ let result = JSON.parse(lastCallBody(fetchMock));
353
+ expect(result).toHaveProperty('somedata', 'something');
354
+ expect(result).not.toHaveProperty('customData');
355
+ });
475
356
 
476
- expect(() => coveoua('require', 'test')).not.toThrow();
477
- });
357
+ it('does not add a customData entry for custom params which are undefined', async () => {
358
+ coveoua('init', 'MYTOKEN', {plugins: []});
359
+ coveoua('set', 'custom', undefined);
360
+ await coveoua('send', 'view', {somedata: 'something'});
478
361
 
479
- it('throws if not initialized', () => {
480
- expect(() => coveoua('require', 'test')).toThrow(`You must call init before requiring a plugin`);
481
- });
362
+ expect(fetchMock.calls().length).toBe(1);
363
+ let result = JSON.parse(lastCallBody(fetchMock));
364
+ expect(result).toHaveProperty('somedata', 'something');
365
+ expect(result).not.toHaveProperty('customData');
366
+ });
482
367
 
483
- it('throws if the plugin is not registered first', () => {
484
- coveoua('init', 'MYTOKEN', {plugins: []});
368
+ it('does not add a customData entry for customData params which are strings', async () => {
369
+ coveoua('init', 'MYTOKEN', {plugins: []});
370
+ coveoua('set', 'custom', 'test');
371
+ await coveoua('send', 'view', {somedata: 'something'});
485
372
 
486
- expect(() => coveoua('require', 'test')).toThrow(
487
- `No plugin named "test" is currently registered. If you use a custom plugin, use 'provide' first.`,
488
- );
489
- });
373
+ expect(fetchMock.calls().length).toBe(1);
374
+ let result = JSON.parse(lastCallBody(fetchMock));
375
+ expect(result).toHaveProperty('somedata', 'something');
376
+ expect(result).not.toHaveProperty('customData');
490
377
  });
491
378
 
492
- describe('reset', () => {
493
- it('reset the client', () => {
494
- coveoua('init', 'MYTOKEN');
379
+ it('does not add a customData entry for customData params which are arrays', async () => {
380
+ coveoua('init', 'MYTOKEN', [{plugins: []}]);
381
+ coveoua('set', 'custom', [test]);
382
+ await coveoua('send', 'view', {somedata: 'something'});
495
383
 
496
- coveoua('reset');
384
+ expect(fetchMock.calls().length).toBe(1);
385
+ let result = JSON.parse(lastCallBody(fetchMock));
386
+ expect(result).toHaveProperty('somedata', 'something');
387
+ expect(result).not.toHaveProperty('customData');
388
+ });
497
389
 
498
- expect(() => coveoua('send')).toThrow(`You must call init before sending an event`);
499
- });
390
+ it('will not override hardcoded customData parameters', async () => {
391
+ coveoua('init', 'MYTOKEN', {plugins: []});
392
+ coveoua('set', 'custom', {context_website: 'MY_WEBSITE'});
393
+ await coveoua('send', 'view', {
394
+ somedata: 'something',
395
+ customData: {context_website: 'MY_OTHER_WEBSITE'},
396
+ });
397
+
398
+ expect(fetchMock.calls().length).toBe(1);
399
+ let result = JSON.parse(lastCallBody(fetchMock));
400
+ expect(result).toHaveProperty('somedata', 'something');
401
+ expect(result).toHaveProperty('customData.context_website', 'MY_OTHER_WEBSITE');
402
+ });
500
403
 
501
- it('reset the plugins', () => {
502
- const fakePlugin = TestPlugin;
503
- coveoua('provide', 'test', fakePlugin);
504
- coveoua('init', 'MYTOKEN', {plugins: ['test']});
404
+ it('can set trackingId', async () => {
405
+ const trackingId = 'yourbestfriend';
406
+ coveoua('init', 'MYTOKEN');
407
+ coveoua('set', 'trackingId', trackingId);
408
+ await coveoua('send', someRandomEventName);
505
409
 
506
- coveoua('reset');
410
+ expect(fetchMock.calls().length).toBe(1);
411
+ expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
412
+ expect(JSON.parse(lastCallBody(fetchMock))).toHaveProperty('trackingId', trackingId);
413
+ });
507
414
 
508
- expect(() => coveoua('init', 'MYTOKEN', {plugins: ['test']})).toThrow(
509
- `No plugin named "test" is currently registered. If you use a custom plugin, use 'provide' first.`,
510
- );
511
- });
415
+ it('context_website does not overwrite trackingId', async () => {
416
+ const trackingId = 'yourbestfriend';
417
+ const contextWebsite = 'yoursite';
418
+ coveoua('init', 'MYTOKEN');
419
+ coveoua('set', 'custom', {context_website: contextWebsite});
420
+ coveoua('set', 'trackingId', trackingId);
421
+ await coveoua('send', someRandomEventName);
422
+
423
+ expect(fetchMock.calls().length).toBe(1);
424
+ expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
425
+ expect(JSON.parse(lastCallBody(fetchMock))).toHaveProperty('trackingId', trackingId);
426
+ });
427
+ });
512
428
 
513
- it('reset the params', async () => {
514
- coveoua('init', 'MYTOKEN');
515
- coveoua('set', 'userId', 'something');
429
+ describe('onLoad', () => {
430
+ it('can execute callback with onLoad event', () => {
431
+ var callback = vi.fn();
516
432
 
517
- coveoua('reset');
433
+ coveoua('onLoad', callback);
518
434
 
519
- coveoua('init', 'MYTOKEN');
520
- await coveoua('send', someRandomEventName);
435
+ expect(callback).toHaveBeenCalledTimes(1);
436
+ });
521
437
 
522
- expect(fetchMock.calls().length).toBe(1);
523
- expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
524
- expect(JSON.parse(lastCallBody(fetchMock))).toEqual({});
525
- });
438
+ it('throws when registering an invalid onLoad event', () => {
439
+ expect(() => coveoua('onLoad', undefined)).toThrow();
526
440
  });
441
+ });
527
442
 
528
- it('throws when called with an unknown action', () => {
529
- coveoua('init', 'SOME TOKEN', {plugins: ['svc']});
443
+ describe('provide', () => {
444
+ it('register properly', () => {
445
+ coveoua('provide', 'test', TestPlugin);
530
446
 
531
- expect(() => coveoua('potato')).toThrow(
532
- `The action "potato" does not exist. Available actions: init, set, send, onLoad, callPlugin, reset, require, provide, version.`,
533
- );
447
+ coveoua('init', 'MYTOKEN');
448
+
449
+ expect(() => coveoua('require', 'test')).not.toThrow();
450
+ });
451
+ });
452
+
453
+ describe('version', () => {
454
+ it('returns the current version string', () => {
455
+ coveoua('init', 'MYTOKEN');
456
+ expect(coveoua('version')).toBe(libVersion);
534
457
  });
458
+ });
535
459
 
460
+ describe('callPlugin', () => {
536
461
  it('resolves properly plugin actions', () => {
537
- coveoua('provide', 'test', TestPluginWithSpy);
538
- coveoua('init', 'MYTOKEN', {plugins: ['test']});
462
+ coveoua('provide', 'test', TestPluginWithSpy);
463
+ coveoua('init', 'MYTOKEN', {plugins: ['test']});
539
464
 
540
- coveoua('test:testMethod', 'foo', 'bar');
465
+ coveoua('callPlugin', 'test', 'testMethod', 'foo', 'bar');
541
466
 
542
- expect(TestPluginWithSpy.spy).toHaveBeenCalledTimes(1);
543
- expect(TestPluginWithSpy.spy).toHaveBeenCalledWith(['foo', 'bar']);
467
+ expect(TestPluginWithSpy.spy).toHaveBeenCalledTimes(1);
468
+ expect(TestPluginWithSpy.spy).toHaveBeenCalledWith(['foo', 'bar']);
544
469
  });
470
+
471
+ it('throws when a namespaced action is called and that the namespace/plugin is not required', () => {
472
+ coveoua('init', 'SOME TOKEN', {plugins: ['ec']});
473
+
474
+ expect(() => coveoua('callPlugin', 'svc', 'setTicket')).toThrow(/is not required/);
475
+ });
476
+
477
+ it('throws when a namespaced action is called and that this action does not exists on the plugin', () => {
478
+ coveoua('init', 'SOME TOKEN', {plugins: ['svc']});
479
+
480
+ expect(() => coveoua('callPlugin', 'svc', 'fooBarBaz')).toThrow(/does not exist/);
481
+ });
482
+ });
483
+
484
+ describe('require', () => {
485
+ it('can require a plugin', () => {
486
+ coveoua('provide', 'test', TestPlugin);
487
+ coveoua('init', 'MYTOKEN', {plugins: []});
488
+
489
+ expect(() => coveoua('require', 'test')).not.toThrow();
490
+ });
491
+
492
+ it('throws if not initialized', () => {
493
+ expect(() => coveoua('require', 'test')).toThrow(
494
+ `You must call init before requiring a plugin`
495
+ );
496
+ });
497
+
498
+ it('throws if the plugin is not registered first', () => {
499
+ coveoua('init', 'MYTOKEN', {plugins: []});
500
+
501
+ expect(() => coveoua('require', 'test')).toThrow(
502
+ `No plugin named "test" is currently registered. If you use a custom plugin, use 'provide' first.`
503
+ );
504
+ });
505
+ });
506
+
507
+ describe('reset', () => {
508
+ it('reset the client', () => {
509
+ coveoua('init', 'MYTOKEN');
510
+
511
+ coveoua('reset');
512
+
513
+ expect(() => coveoua('send')).toThrow(`You must call init before sending an event`);
514
+ });
515
+
516
+ it('reset the plugins', () => {
517
+ const fakePlugin = TestPlugin;
518
+ coveoua('provide', 'test', fakePlugin);
519
+ coveoua('init', 'MYTOKEN', {plugins: ['test']});
520
+
521
+ coveoua('reset');
522
+
523
+ expect(() => coveoua('init', 'MYTOKEN', {plugins: ['test']})).toThrow(
524
+ `No plugin named "test" is currently registered. If you use a custom plugin, use 'provide' first.`
525
+ );
526
+ });
527
+
528
+ it('reset the params', async () => {
529
+ coveoua('init', 'MYTOKEN');
530
+ coveoua('set', 'userId', 'something');
531
+
532
+ coveoua('reset');
533
+
534
+ coveoua('init', 'MYTOKEN');
535
+ await coveoua('send', someRandomEventName);
536
+
537
+ expect(fetchMock.calls().length).toBe(1);
538
+ expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/${someRandomEventName}`);
539
+ expect(JSON.parse(lastCallBody(fetchMock))).toEqual({});
540
+ });
541
+ });
542
+
543
+ it('throws when called with an unknown action', () => {
544
+ coveoua('init', 'SOME TOKEN', {plugins: ['svc']});
545
+
546
+ expect(() => coveoua('potato')).toThrow(
547
+ `The action "potato" does not exist. Available actions: init, set, send, onLoad, callPlugin, reset, require, provide, version.`
548
+ );
549
+ });
550
+
551
+ it('resolves properly plugin actions', () => {
552
+ coveoua('provide', 'test', TestPluginWithSpy);
553
+ coveoua('init', 'MYTOKEN', {plugins: ['test']});
554
+
555
+ coveoua('test:testMethod', 'foo', 'bar');
556
+
557
+ expect(TestPluginWithSpy.spy).toHaveBeenCalledTimes(1);
558
+ expect(TestPluginWithSpy.spy).toHaveBeenCalledWith(['foo', 'bar']);
559
+ });
545
560
  });