-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.test.ts
More file actions
469 lines (421 loc) · 14.7 KB
/
utils.test.ts
File metadata and controls
469 lines (421 loc) · 14.7 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// utils.test.ts
import { beforeEach, describe, expect, test, vi } from "vitest";
import {
mockProjectId,
mockSurveyId,
} from "@/lib/common/tests/__mocks__/config.mock";
import {
delayedResult,
diffInDays,
filterSurveys,
getDefaultLanguageCode,
getLanguageCode,
getStyling,
shouldDisplayBasedOnPercentage,
wrapThrowsAsync,
} from "@/lib/common/utils";
import type {
TEnvironmentState,
TEnvironmentStateProject,
TUserState,
} from "@/types/config";
import { TSurvey } from "@/types/survey";
const mockSurveyId1 = "e3kxlpnzmdp84op9qzxl9olj";
const mockSurveyId2 = "qo9rwjmms42hoy3k85fp8vgu";
const mockSegmentId1 = "p6yrnz3s2tvoe5r0l28unq7k";
const mockSegmentId2 = "wz43zrxeddhb1uo9cicustar";
describe("utils.ts", () => {
// ---------------------------------------------------------------------------------
// diffInDays
// ---------------------------------------------------------------------------------
describe("diffInDays()", () => {
test("calculates correct day difference", () => {
const date1 = new Date("2023-01-01");
const date2 = new Date("2023-01-05");
expect(diffInDays(date1, date2)).toBe(4); // four days apart
});
test("handles negative differences (abs)", () => {
const date1 = new Date("2023-01-10");
const date2 = new Date("2023-01-05");
expect(diffInDays(date1, date2)).toBe(5);
});
test("0 if same day", () => {
const date = new Date("2023-01-01");
expect(diffInDays(date, date)).toBe(0);
});
});
// ---------------------------------------------------------------------------------
// wrapThrowsAsync
// ---------------------------------------------------------------------------------
describe("wrapThrowsAsync()", () => {
test("returns ok on success", async () => {
const fn = vi.fn(async (x: number) => {
await delayedResult(null, 10);
return x * 2;
});
const wrapped = wrapThrowsAsync(fn);
const result = await wrapped(5);
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.data).toBe(10);
}
});
test("returns err on error", async () => {
const fn = vi.fn(async () => {
await delayedResult(null, 10);
throw new Error("Something broke");
});
const wrapped = wrapThrowsAsync(fn);
const result = await wrapped();
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error.message).toBe("Something broke");
}
});
});
// ---------------------------------------------------------------------------------
// filterSurveys
// ---------------------------------------------------------------------------------
describe("filterSurveys()", () => {
// We'll create a minimal environment state
let environment: TEnvironmentState;
let user: TUserState;
const baseSurvey: Partial<TSurvey> = {
id: mockSurveyId,
displayOption: "displayOnce",
displayLimit: 1,
recontactDays: null,
languages: [],
};
beforeEach(() => {
environment = {
expiresAt: new Date(),
data: {
project: {
id: mockProjectId,
recontactDays: 7, // fallback if survey doesn't have it
clickOutsideClose: false,
overlay: "none",
placement: "bottomRight",
inAppSurveyBranding: true,
styling: { allowStyleOverwrite: false },
} as TEnvironmentStateProject,
surveys: [],
actionClasses: [],
},
};
user = {
expiresAt: null,
data: {
userId: null,
contactId: null,
segments: [],
displays: [],
responses: [],
lastDisplayAt: null,
},
};
});
test("returns no surveys if user has no segments and userId is set", () => {
user.data.userId = "user_abc";
// environment has a single survey
environment.data.surveys = [
{
...baseSurvey,
id: mockSurveyId1,
segment: { id: mockSegmentId1 },
} as TSurvey,
];
const result = filterSurveys(environment, user);
expect(result).toEqual([]); // no segments => none pass
});
test("returns surveys if user has no userId but displayOnce and no displays yet", () => {
// userId is null => it won't segment filter
environment.data.surveys = [
{
...baseSurvey,
id: mockSurveyId1,
displayOption: "displayOnce",
} as TSurvey,
];
const result = filterSurveys(environment, user);
expect(result).toHaveLength(1);
expect(result[0].id).toBe(mockSurveyId1);
});
test("filters out surveys that have a segment with filters if userId is not set", () => {
environment.data.surveys = [
{
...baseSurvey,
id: mockSurveyId1,
segment: {
id: mockSegmentId1,
filters: [{ type: "string", key: "name", value: "John" }],
},
} as TSurvey,
];
const result = filterSurveys(environment, user);
expect(result).toHaveLength(0);
});
test("includes surveys without segment filters for anonymous users", () => {
environment.data.surveys = [
{
...baseSurvey,
id: mockSurveyId1,
segment: undefined, // No segment at all
} as TSurvey,
{
...baseSurvey,
id: mockSurveyId2,
segment: { id: mockSegmentId1 }, // Segment but no filters
} as TSurvey,
];
const result = filterSurveys(environment, user);
expect(result).toHaveLength(2);
});
test("skips surveys that already displayed if displayOnce is used", () => {
environment.data.surveys = [
{
...baseSurvey,
id: mockSurveyId1,
displayOption: "displayOnce",
} as TSurvey,
];
user.data.displays = [{ surveyId: mockSurveyId1, createdAt: new Date() }];
const result = filterSurveys(environment, user);
expect(result).toEqual([]);
});
test("skips surveys if user responded to them and displayOption=displayMultiple", () => {
environment.data.surveys = [
{
...baseSurvey,
id: mockSurveyId1,
displayOption: "displayMultiple",
} as TSurvey,
];
user.data.responses = [mockSurveyId1];
const result = filterSurveys(environment, user);
expect(result).toEqual([]);
});
test("handles displaySome logic with displayLimit", () => {
environment.data.surveys = [
{
...baseSurvey,
id: mockSurveyId1,
displayOption: "displaySome",
displayLimit: 2,
} as TSurvey,
];
// user has 1 display of s1
user.data.displays = [{ surveyId: mockSurveyId1, createdAt: new Date() }];
// No responses => so it's still allowed
const result = filterSurveys(environment, user);
expect(result).toHaveLength(1);
});
test("filters out surveys if recontactDays not met", () => {
// Suppose survey uses project fallback (7 days)
environment.data.surveys = [
{
...baseSurvey,
id: mockSurveyId1,
displayOption: "displayOnce",
} as TSurvey,
];
// user last displayAt is only 3 days ago
user.data.lastDisplayAt = new Date(Date.now() - 3 * 24 * 60 * 60 * 1000);
const result = filterSurveys(environment, user);
expect(result).toHaveLength(0);
});
test("passes surveys if enough days have passed since lastDisplayAt", () => {
// user last displayAt is 8 days ago
user.data.lastDisplayAt = new Date(Date.now() - 8 * 24 * 60 * 60 * 1000);
environment.data.surveys = [
{
...baseSurvey,
id: mockSurveyId1,
displayOption: "respondMultiple",
recontactDays: null,
} as TSurvey,
];
const result = filterSurveys(environment, user);
expect(result).toHaveLength(1);
});
test("filters by segment if userId is set and user has segments", () => {
user.data.userId = "user_abc";
user.data.segments = [mockSegmentId1];
environment.data.surveys = [
{
...baseSurvey,
id: mockSurveyId1,
segment: { id: mockSegmentId1 },
displayOption: "respondMultiple",
} as TSurvey,
{
...baseSurvey,
id: mockSurveyId2,
segment: { id: mockSegmentId2 },
displayOption: "respondMultiple",
} as TSurvey,
];
const result = filterSurveys(environment, user);
// only the one that matches user's segment
expect(result).toHaveLength(1);
expect(result[0].id).toBe(mockSurveyId1);
});
});
// ---------------------------------------------------------------------------------
// getStyling
// ---------------------------------------------------------------------------------
describe("getStyling()", () => {
test("returns project styling if allowStyleOverwrite=false", () => {
const project = {
id: "p1",
styling: { allowStyleOverwrite: false, brandColor: { light: "#fff" } },
} as TEnvironmentStateProject;
const survey = {
styling: {
overwriteThemeStyling: true,
brandColor: { light: "#000" },
} as TSurvey["styling"],
} as TSurvey;
const result = getStyling(project, survey);
// should get project styling
expect(result).toEqual(project.styling);
});
test("returns project styling if allowStyleOverwrite=true but survey overwriteThemeStyling=false", () => {
const project = {
id: "p1",
styling: { allowStyleOverwrite: true, brandColor: { light: "#fff" } },
} as TEnvironmentStateProject;
const survey = {
styling: {
overwriteThemeStyling: false,
brandColor: { light: "#000" },
} as TSurvey["styling"],
} as TSurvey;
const result = getStyling(project, survey);
// should get project styling still
expect(result).toEqual(project.styling);
});
test("returns survey styling if allowStyleOverwrite=true and survey overwriteThemeStyling=true", () => {
const project = {
id: "p1",
styling: { allowStyleOverwrite: true, brandColor: { light: "#fff" } },
} as TEnvironmentStateProject;
const survey = {
styling: {
overwriteThemeStyling: true,
brandColor: { light: "#000" },
} as TSurvey["styling"],
} as TSurvey;
const result = getStyling(project, survey);
expect(result).toEqual(survey.styling);
});
});
// ---------------------------------------------------------------------------------
// getDefaultLanguageCode
// ---------------------------------------------------------------------------------
describe("getDefaultLanguageCode()", () => {
test("returns code of the language if it is flagged default", () => {
const survey = {
languages: [
{
language: { code: "en" },
default: false,
enabled: true,
},
{
language: { code: "fr" },
default: true,
enabled: true,
},
],
} as unknown as TSurvey;
expect(getDefaultLanguageCode(survey)).toBe("fr");
});
test("returns undefined if no default language found", () => {
const survey = {
languages: [
{ language: { code: "en" }, default: false, enabled: true },
{ language: { code: "fr" }, default: false, enabled: true },
],
} as unknown as TSurvey;
expect(getDefaultLanguageCode(survey)).toBeUndefined();
});
});
// ---------------------------------------------------------------------------------
// getLanguageCode
// ---------------------------------------------------------------------------------
describe("getLanguageCode()", () => {
test("returns 'default' if no language param is passed", () => {
const survey = {
languages: [{ language: { code: "en" }, default: true, enabled: true }],
} as unknown as TSurvey;
const code = getLanguageCode(survey);
expect(code).toBe("default");
});
test("returns 'default' if the chosen language is the default one", () => {
const survey = {
languages: [
{ language: { code: "en" }, default: true, enabled: true },
{ language: { code: "fr" }, default: false, enabled: true },
],
} as unknown as TSurvey;
const code = getLanguageCode(survey, "en");
expect(code).toBe("default");
});
test("returns undefined if language not found or disabled", () => {
const survey = {
languages: [
{ language: { code: "en" }, default: true, enabled: true },
{ language: { code: "fr" }, default: false, enabled: false },
],
} as unknown as TSurvey;
const code = getLanguageCode(survey, "fr");
expect(code).toBeUndefined();
});
test("returns the language code if found and enabled", () => {
const survey = {
languages: [
{
language: { code: "en", alias: "English" },
default: true,
enabled: true,
},
{
language: { code: "fr", alias: "fr-FR" },
default: false,
enabled: true,
},
],
} as unknown as TSurvey;
expect(getLanguageCode(survey, "fr")).toBe("fr");
expect(getLanguageCode(survey, "fr-FR")).toBe("fr");
});
});
// ---------------------------------------------------------------------------------
// shouldDisplayBasedOnPercentage
// ---------------------------------------------------------------------------------
describe("shouldDisplayBasedOnPercentage()", () => {
test("returns true if random number <= displayPercentage", () => {
// We'll mock Math.random to return something
const mockedRandom = vi.spyOn(Math, "random").mockReturnValue(0.2); // 0.2 => 20%
// displayPercentage = 30 => 30% => we should display
expect(shouldDisplayBasedOnPercentage(30)).toBe(true);
mockedRandom.mockReturnValue(0.5); // 50%
expect(shouldDisplayBasedOnPercentage(30)).toBe(false);
// restore
mockedRandom.mockRestore();
});
});
// ---------------------------------------------------------------------------------
// delayedResult
// ---------------------------------------------------------------------------------
describe("delayedResult()", () => {
test("returns the value after the delay", async () => {
const result = await delayedResult(10, 10);
expect(result).toBe(10);
const result2 = await delayedResult(20, 20);
expect(result2).toBe(20);
});
});
});