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.
- package/README.md +37 -43
- package/dist/browser.mjs +513 -357
- package/dist/coveoua.browser.js +1 -1
- package/dist/coveoua.browser.js.map +1 -1
- package/dist/coveoua.debug.js +542 -359
- package/dist/coveoua.debug.js.map +1 -1
- package/dist/coveoua.js +1 -1
- package/dist/coveoua.js.map +1 -1
- package/dist/definitions/client/analytics.d.ts +3 -2
- package/dist/definitions/client/runtimeEnvironment.d.ts +1 -1
- package/dist/definitions/donottrack.d.ts +1 -0
- package/dist/definitions/react-native/index.d.ts +1 -2
- package/dist/definitions/version.d.ts +1 -1
- package/dist/library.cjs +15350 -3699
- package/dist/library.es.js +513 -357
- package/dist/library.js +15350 -3699
- package/dist/library.mjs +15350 -3699
- package/dist/react-native.es.js +634 -460
- package/modules/package.json +9 -9
- package/package.json +60 -91
- package/react-native/package.json +7 -7
- package/src/caseAssist/caseAssistActions.ts +51 -50
- package/src/caseAssist/caseAssistClient.spec.ts +328 -297
- package/src/caseAssist/caseAssistClient.ts +201 -185
- package/src/client/analytics.spec.ts +724 -703
- package/src/client/analytics.ts +677 -602
- package/src/client/analyticsBeaconClient.spec.ts +195 -188
- package/src/client/analyticsBeaconClient.ts +99 -88
- package/src/client/analyticsFetchClient.ts +77 -61
- package/src/client/analyticsRequestClient.ts +17 -17
- package/src/client/location.ts +3 -3
- package/src/client/measurementProtocolMapper.ts +54 -45
- package/src/client/measurementProtocolMapping/baseMeasurementProtocolMapper.ts +48 -44
- package/src/client/measurementProtocolMapping/commerceMeasurementProtocolMapper.ts +133 -121
- package/src/client/measurementProtocolMapping/serviceMeasurementProtocolMapper.ts +17 -17
- package/src/client/noopAnalytics.ts +80 -68
- package/src/client/runtimeEnvironment.ts +50 -41
- package/src/client/utils.spec.ts +112 -97
- package/src/client/utils.ts +39 -39
- package/src/cookieutils.spec.ts +59 -0
- package/src/cookieutils.ts +40 -39
- package/src/coveoua/browser.ts +14 -12
- package/src/coveoua/library.ts +4 -4
- package/src/coveoua/plugins.ts +36 -34
- package/src/coveoua/simpleanalytics.spec.ts +467 -452
- package/src/coveoua/simpleanalytics.ts +146 -140
- package/src/detector.ts +17 -17
- package/src/donottrack.spec.ts +199 -121
- package/src/donottrack.ts +31 -8
- package/src/events.ts +86 -79
- package/src/formatting/format-array-for-coveo-custom-data.spec.ts +13 -12
- package/src/formatting/format-array-for-coveo-custom-data.ts +18 -18
- package/src/formatting/format-omnibox-metadata.ts +16 -12
- package/src/history.spec.ts +197 -195
- package/src/history.ts +143 -133
- package/src/hook/addDefaultValues.ts +13 -8
- package/src/hook/enhanceViewEvent.ts +17 -17
- package/src/insight/insightClient.spec.ts +1848 -1717
- package/src/insight/insightClient.ts +866 -761
- package/src/insight/insightEvents.ts +57 -56
- package/src/plugins/BasePlugin.ts +125 -121
- package/src/plugins/ec.spec.ts +457 -429
- package/src/plugins/ec.ts +202 -199
- package/src/plugins/link.spec.ts +183 -159
- package/src/plugins/link.ts +88 -85
- package/src/plugins/svc.spec.ts +161 -155
- package/src/plugins/svc.ts +93 -91
- package/src/react-native/index.ts +8 -1
- package/src/react-native/react-native-runtime.ts +33 -33
- package/src/react-native/react-native-utils.ts +1 -1
- package/src/react-native/react-native.spec.ts +31 -21
- package/src/searchPage/searchPageClient.spec.ts +1944 -1794
- package/src/searchPage/searchPageClient.ts +1261 -1040
- package/src/searchPage/searchPageEvents.ts +524 -511
- package/src/storage.spec.ts +51 -51
- package/src/storage.ts +47 -47
- package/dist/definitions/bundle/browser-fetch.d.ts +0 -1
- package/dist/definitions/src/coveoua/browser.d.ts +0 -6
- package/dist/definitions/src/coveoua/headless.d.ts +0 -6
- package/dist/definitions/src/coveoua/library.d.ts +0 -17
- package/dist/definitions/src/coveoua/plugins.d.ts +0 -10
- package/dist/definitions/src/coveoua/simpleanalytics.d.ts +0 -33
- package/dist/definitions/src/react-native/index.d.ts +0 -3
- 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 =
|
|
11
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
47
|
-
|
|
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
|
-
|
|
54
|
-
|
|
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
|
-
|
|
60
|
-
|
|
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
|
-
|
|
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
|
-
|
|
71
|
-
|
|
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
|
-
|
|
75
|
-
|
|
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
|
-
|
|
79
|
-
coveoua('init', 'SOME TOKEN');
|
|
93
|
+
await coveoua('send', 'pageview');
|
|
80
94
|
|
|
81
|
-
|
|
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
|
-
|
|
84
|
-
|
|
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
|
-
|
|
89
|
-
coveoua('init', 'SOME TOKEN', '');
|
|
103
|
+
await coveoua('send', 'pageview');
|
|
90
104
|
|
|
91
|
-
|
|
105
|
+
expect(fetchMock.calls().length).toBe(1);
|
|
106
|
+
expect(fetchMock.lastUrl()).toMatch(/^https:\/\/analytics\.cloud\.coveo\.com\/rest\/ua/);
|
|
107
|
+
});
|
|
92
108
|
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
99
|
-
coveoua('init', 'SOME TOKEN', {});
|
|
112
|
+
await coveoua('send', 'pageview');
|
|
100
113
|
|
|
101
|
-
|
|
114
|
+
expect(fetchMock.calls().length).toBe(1);
|
|
115
|
+
expect(fetchMock.lastUrl()).toMatch(/^https:\/\/analytics\.cloud\.coveo\.com\/rest\/ua/);
|
|
116
|
+
});
|
|
102
117
|
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
108
|
-
coveoua('init', 'SOME TOKEN', {endpoint: ''});
|
|
121
|
+
await coveoua('send', 'pageview');
|
|
109
122
|
|
|
110
|
-
|
|
123
|
+
expect(fetchMock.calls().length).toBe(1);
|
|
124
|
+
expect(fetchMock.lastUrl()).toMatch(/^https:\/\/someendpoint\.com/);
|
|
125
|
+
});
|
|
111
126
|
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
117
|
-
coveoua('init', 'SOME TOKEN', 'https://someendpoint.com');
|
|
130
|
+
await coveoua('send', 'pageview');
|
|
118
131
|
|
|
119
|
-
|
|
132
|
+
expect(fetchMock.calls().length).toBe(1);
|
|
133
|
+
expect(fetchMock.lastUrl()).toMatch(/^https:\/\/someendpoint\.com/);
|
|
134
|
+
});
|
|
120
135
|
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
126
|
-
|
|
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
|
-
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
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
|
-
|
|
411
|
-
|
|
412
|
-
|
|
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
|
-
|
|
417
|
-
|
|
418
|
-
|
|
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
|
-
|
|
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
|
-
|
|
423
|
-
|
|
187
|
+
it('can send pageview', async () => {
|
|
188
|
+
coveoua('init', 'MYTOKEN', {plugins: []});
|
|
189
|
+
await coveoua('send', 'pageview');
|
|
424
190
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
});
|
|
191
|
+
expect(fetchMock.calls().length).toBe(1);
|
|
192
|
+
expect(fetchMock.lastUrl()).toBe(`${analyticsEndpoint}/pageview`);
|
|
428
193
|
});
|
|
429
194
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
195
|
+
it('can send pageview with customdata', async () => {
|
|
196
|
+
coveoua('init', 'MYTOKEN', {plugins: []});
|
|
197
|
+
await coveoua('send', 'pageview', {somedata: 'asd'});
|
|
433
198
|
|
|
434
|
-
|
|
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
|
-
|
|
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
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
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
|
-
|
|
448
|
-
|
|
449
|
-
|
|
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
|
-
|
|
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
|
-
|
|
455
|
-
|
|
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
|
-
|
|
459
|
-
|
|
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
|
-
|
|
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
|
-
|
|
465
|
-
|
|
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
|
-
|
|
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
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
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
|
-
|
|
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
|
-
|
|
480
|
-
|
|
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
|
-
|
|
484
|
-
|
|
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
|
-
|
|
487
|
-
|
|
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
|
-
|
|
493
|
-
|
|
494
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
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
|
-
|
|
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
|
-
|
|
509
|
-
|
|
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
|
-
|
|
514
|
-
|
|
515
|
-
|
|
429
|
+
describe('onLoad', () => {
|
|
430
|
+
it('can execute callback with onLoad event', () => {
|
|
431
|
+
var callback = vi.fn();
|
|
516
432
|
|
|
517
|
-
|
|
433
|
+
coveoua('onLoad', callback);
|
|
518
434
|
|
|
519
|
-
|
|
520
|
-
|
|
435
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
436
|
+
});
|
|
521
437
|
|
|
522
|
-
|
|
523
|
-
|
|
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
|
-
|
|
529
|
-
|
|
443
|
+
describe('provide', () => {
|
|
444
|
+
it('register properly', () => {
|
|
445
|
+
coveoua('provide', 'test', TestPlugin);
|
|
530
446
|
|
|
531
|
-
|
|
532
|
-
|
|
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
|
-
|
|
538
|
-
|
|
462
|
+
coveoua('provide', 'test', TestPluginWithSpy);
|
|
463
|
+
coveoua('init', 'MYTOKEN', {plugins: ['test']});
|
|
539
464
|
|
|
540
|
-
|
|
465
|
+
coveoua('callPlugin', 'test', 'testMethod', 'foo', 'bar');
|
|
541
466
|
|
|
542
|
-
|
|
543
|
-
|
|
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
|
});
|