-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathutil.test.js
More file actions
253 lines (213 loc) · 9.28 KB
/
Copy pathutil.test.js
File metadata and controls
253 lines (213 loc) · 9.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { NAME, DISPLAY_NAME } from '../../../src/integrations/Amplitude/constants';
import {
getTraitsToSetOnce,
getTraitsToIncrement,
getDestinationOptions,
formatUrl,
getAmplitudeSdkVersion,
getAutoCapturePageViews,
getPageUrlEnrichment,
getTrackSessionEvents,
getWebVitals,
getFileDownloads,
getFrustrationInteractions,
getNetworkTracking,
getElementInteractions,
getFormInteractions,
} from '../../../src/integrations/Amplitude/utils';
describe('getTraitsToSetOnce', () => {
// Returns an empty array when no traitsToSetOnce are provided in the config object
it('should return an empty array when no traitsToSetOnce are provided in the config object', () => {
const config = {};
const result = getTraitsToSetOnce(config);
expect(result).toEqual([]);
});
// Returns an array of traits when traitsToSetOnce are provided in the config object
it('should return an array of traits when traitsToSetOnce are provided in the config object', () => {
const config = {
traitsToSetOnce: [{ traits: { name: 'John' } }, { traits: { age: 25 } }],
};
const result = getTraitsToSetOnce(config);
expect(result).toEqual([{ name: 'John' }, { age: 25 }]);
});
// Filters out elements with empty traits and returns an array of non-empty traits
it('should not filter out elements with empty traits', () => {
const config = {
traitsToSetOnce: [
{ traits: { name: 'John' } },
{ traits: {} },
{ traits: { age: 25 } },
{ traits: {} },
],
};
const result = getTraitsToSetOnce(config);
expect(result).toEqual([{ name: 'John' }, {}, { age: 25 }, {}]);
});
// Returns an empty array when config object is not provided
it('should return an empty array when config object is not provided', () => {
try {
getTraitsToSetOnce();
} catch (err) {
expect(err).toEqual(
new TypeError("Cannot read properties of undefined (reading 'traitsToSetOnce')"),
);
}
});
// Returns an empty array when traitsToSetOnce is not an array in the config object
it('should return an empty array when traitsToSetOnce is not an array in the config object', () => {
const config = {
traitsToSetOnce: 'not an array',
};
try {
getTraitsToSetOnce(config);
} catch (err) {
expect(err).toEqual(new TypeError('config.traitsToSetOnce.forEach is not a function'));
}
});
// Returns an empty array when all elements in traitsToSetOnce have empty traits
it('should return an empty array when all elements in traitsToSetOnce have empty traits', () => {
const config = {
traitsToSetOnce: [{ traits: {} }, { traits: {} }, { traits: {} }],
};
const result = getTraitsToSetOnce(config);
expect(result).toEqual([{}, {}, {}]);
});
});
describe('getTraitsToIncrement', () => {
it('should return an empty array when config.traitsToIncrement is undefined', () => {
const config = {};
const result = getTraitsToIncrement(config);
expect(result).toEqual([]);
});
it('should return an array of traits when config.traitsToIncrement is defined and contains at least one element with a non-empty traits property', () => {
const config = {
traitsToIncrement: [{ traits: 'trait1' }, { traits: 'trait2' }],
};
const result = getTraitsToIncrement(config);
expect(result).toEqual(['trait1', 'trait2']);
});
it('should return an empty array when config.traitsToIncrement is defined but contains no elements with a non-empty traits property', () => {
const config = {
traitsToIncrement: [{ traits: '' }, { traits: null }, { traits: undefined }],
};
const result = getTraitsToIncrement(config);
expect(result).toEqual([]);
});
});
describe('getDestinationOptions', () => {
// Returns destination specific options for the given integrations options object using its display name
it('should return destination specific options for the given integrations options object using its display name', () => {
const integrationsOptions = {
[DISPLAY_NAME]: { option1: 'value1', option2: 'value2' },
[NAME]: { option3: 'value3', option4: 'value4' },
};
const result = getDestinationOptions(integrationsOptions);
expect(result).toEqual({ option1: 'value1', option2: 'value2' });
});
// Returns destination specific options for the given integrations options object using its name if display name is not present
it('should return destination specific options for the given integrations options object using its name if display name is not present', () => {
const integrationsOptions = {
[NAME]: { option3: 'value3', option4: 'value4' },
};
const result = getDestinationOptions(integrationsOptions);
expect(result).toEqual({ option3: 'value3', option4: 'value4' });
});
// Returns null if integrations options object is null or undefined
it('should return null if integrations options object is null', () => {
const integrationsOptions = null;
const result = getDestinationOptions(integrationsOptions);
expect(result).toBeNull();
});
// Returns null if integrations options object is an empty object
it('should return null if integrations options object is an empty object', () => {
const integrationsOptions = {};
const result = getDestinationOptions(integrationsOptions);
expect(result).toEqual(undefined);
});
// Returns null if integrations options object does not have options for the given destination
it('should return null if integrations options object does not have options for the given destination', () => {
const integrationsOptions = {
[DISPLAY_NAME]: { option1: 'value1', option2: 'value2' },
};
const result = getDestinationOptions(integrationsOptions);
expect(result).toEqual({ option1: 'value1', option2: 'value2' });
});
// Returns options for the destination using its name if display name is not present and name is not 'AM'
it("should return options for the destination using its name if display name is not present and name is not 'AM'", () => {
const integrationsOptions = {
[NAME]: { option3: 'value3', option4: 'value4' },
};
const result = getDestinationOptions(integrationsOptions);
expect(result).toEqual({ option3: 'value3', option4: 'value4' });
});
});
describe('formatUrl', () => {
// Returns the same URL if it starts with "http://" or "https://"
it('should return the same URL when it starts with "https://"', () => {
const url = 'https://example.com';
expect(formatUrl(url)).toBe(url);
});
// Adds "https://" prefix to the URL if it doesn't start with "http://" or "https://"
it('should add https:// prefix to the URL if it does not start with "https://"', () => {
const url = 'example.com';
expect(formatUrl(url)).toBe('https://example.com');
});
// Returns "https://" if the input URL is an empty string
it('should return "https://" when the input URL is an empty string', () => {
const url = '';
expect(formatUrl(url)).toBe('https://');
});
// Returns "https://example.com" if the input URL is "example.com"
it('should return "https://example.com" when the input URL is "example.com"', () => {
const url = 'example.com';
expect(formatUrl(url)).toBe('https://example.com');
});
});
describe('getAmplitudeSdkVersion', () => {
it('should fallback to v1 when sdkVersion is absent', () => {
expect(getAmplitudeSdkVersion({})).toBe(1);
});
it('should resolve v2 for sdkVersion as 2', () => {
expect(getAmplitudeSdkVersion({ sdkVersion: 2 })).toBe(2);
});
it('should resolve v1 for sdkVersion as 1', () => {
expect(getAmplitudeSdkVersion({ sdkVersion: 1 })).toBe(1);
});
it('should fallback to v1 for an invalid sdkVersion value', () => {
expect(getAmplitudeSdkVersion({ sdkVersion: 'foo' })).toBe(1);
});
it('should fallback to v1 when sdkVersion is null', () => {
expect(getAmplitudeSdkVersion({ sdkVersion: null })).toBe(1);
});
it('should parse numeric strings in sdkVersion', () => {
expect(getAmplitudeSdkVersion({ sdkVersion: '2' })).toBe(2);
});
});
describe('Amplitude v2 autocapture config helpers', () => {
const helperCases = [
{
name: 'getAutoCapturePageViews',
getter: getAutoCapturePageViews,
key: 'pageViews',
},
{ name: 'getPageUrlEnrichment', getter: getPageUrlEnrichment, key: 'pageUrlEnrichment' },
{ name: 'getWebVitals', getter: getWebVitals, key: 'webVitals' },
{ name: 'getFileDownloads', getter: getFileDownloads, key: 'fileDownloads' },
{
name: 'getFrustrationInteractions',
getter: getFrustrationInteractions,
key: 'frustrationInteractions',
},
{ name: 'getNetworkTracking', getter: getNetworkTracking, key: 'networkTracking' },
{ name: 'getElementInteractions', getter: getElementInteractions, key: 'elementInteractions' },
{ name: 'getFormInteractions', getter: getFormInteractions, key: 'formInteractions' },
{ name: 'getTrackSessionEvents', getter: getTrackSessionEvents, key: 'trackSessionEvents' },
];
it.each(helperCases)('$name should default missing config to false', ({ getter }) => {
expect(getter({})).toBe(false);
});
it.each(helperCases)('$name should read true and false config values', ({ getter, key }) => {
expect(getter({ [key]: true })).toBe(true);
expect(getter({ [key]: false })).toBe(false);
});
});