Skip to content

Commit 2b371ee

Browse files
Address PR Comments
1 parent 537e2ea commit 2b371ee

File tree

2 files changed

+94
-40
lines changed

2 files changed

+94
-40
lines changed

src/cookieSyncManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface PixelConfig {
1616
filteringConsentRuleValues: IConsentRules;
1717
}
1818

19-
const DAYS_IN_MILLISECONDS = 1000 * 60 * 60 * 24;
19+
export const DAYS_IN_MILLISECONDS = 1000 * 60 * 60 * 24;
2020

2121
const hasFrequencyCapExpired = (
2222
lastSyncDate: number,

test/jest/cookieSyncManager.spec.ts

Lines changed: 93 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import CookieSyncManager from '../../src/cookieSyncManager';
1+
import CookieSyncManager, { DAYS_IN_MILLISECONDS } from '../../src/cookieSyncManager';
22
import { MParticleWebSDK } from '../../src/sdkRuntimeModels';
33
import { PixelConfiguration } from '../../src/store';
44
import { testMPID } from '../src/config/constants';
@@ -18,7 +18,7 @@ const pixelSettings: PixelConfiguration = {
1818
describe.only('CookieSyncManager', () => {
1919
describe('#attemptCookieSync', () => {
2020
it('should perform a cookie sync with defaults', () => {
21-
const mockSDK = ({
21+
const mockMPInstance = ({
2222
_Store: {
2323
webviewBridgeEnabled: false,
2424
pixelConfigurations: [pixelSettings],
@@ -30,7 +30,7 @@ describe.only('CookieSyncManager', () => {
3030
},
3131
} as unknown) as MParticleWebSDK;
3232

33-
const cookieSyncManager = new CookieSyncManager(mockSDK);
33+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
3434
cookieSyncManager.performCookieSync = jest.fn();
3535

3636
cookieSyncManager.attemptCookieSync(null, testMPID, true);
@@ -47,7 +47,7 @@ describe.only('CookieSyncManager', () => {
4747
});
4848

4949
it('should return early if mpid is not defined', () => {
50-
const mockSDK = ({
50+
const mockMPInstance = ({
5151
_Store: {
5252
webviewBridgeEnabled: false,
5353
pixelConfigurations: [pixelSettings],
@@ -59,7 +59,7 @@ describe.only('CookieSyncManager', () => {
5959
},
6060
} as unknown) as MParticleWebSDK;
6161

62-
const cookieSyncManager = new CookieSyncManager(mockSDK);
62+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
6363
cookieSyncManager.performCookieSync = jest.fn();
6464

6565
cookieSyncManager.attemptCookieSync(null, null, true);
@@ -68,7 +68,7 @@ describe.only('CookieSyncManager', () => {
6868
});
6969

7070
it('should return early if webviewBridgeEnabled is true', () => {
71-
const mockSDK = ({
71+
const mockMPInstance = ({
7272
_Store: {
7373
webviewBridgeEnabled: true,
7474
pixelConfigurations: [pixelSettings],
@@ -80,7 +80,7 @@ describe.only('CookieSyncManager', () => {
8080
},
8181
} as unknown) as MParticleWebSDK;
8282

83-
const cookieSyncManager = new CookieSyncManager(mockSDK);
83+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
8484
cookieSyncManager.performCookieSync = jest.fn();
8585

8686
cookieSyncManager.attemptCookieSync(null, testMPID, true);
@@ -95,7 +95,7 @@ describe.only('CookieSyncManager', () => {
9595
},
9696
};
9797

98-
const mockSDK = ({
98+
const mockMPInstance = ({
9999
_Store: {
100100
webviewBridgeEnabled: false,
101101
pixelConfigurations: [{...pixelSettings, ...myPixelSettings}],
@@ -107,7 +107,7 @@ describe.only('CookieSyncManager', () => {
107107
},
108108
} as unknown) as MParticleWebSDK;
109109

110-
const cookieSyncManager = new CookieSyncManager(mockSDK);
110+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
111111
cookieSyncManager.performCookieSync = jest.fn();
112112

113113
cookieSyncManager.attemptCookieSync(null, testMPID, true);
@@ -129,7 +129,7 @@ describe.only('CookieSyncManager', () => {
129129
redirectUrl: '?redirect=https://redirect.com&mpid=%%mpid%%',
130130
};
131131

132-
const mockSDK = ({
132+
const mockMPInstance = ({
133133
_Store: {
134134
webviewBridgeEnabled: false,
135135
pixelConfigurations: [{...pixelSettings, ...myPixelSettings}],
@@ -141,7 +141,7 @@ describe.only('CookieSyncManager', () => {
141141
},
142142
} as unknown) as MParticleWebSDK;
143143

144-
const cookieSyncManager = new CookieSyncManager(mockSDK);
144+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
145145
cookieSyncManager.performCookieSync = jest.fn();
146146

147147
cookieSyncManager.attemptCookieSync(null, testMPID, true);
@@ -159,7 +159,7 @@ describe.only('CookieSyncManager', () => {
159159

160160
// QUESTION: What is the purpose of this code path?
161161
it('should call performCookieSync with mpid if previousMpid and mpid do not match', () => {
162-
const mockSDK = ({
162+
const mockMPInstance = ({
163163
_Store: {
164164
webviewBridgeEnabled: false,
165165
pixelConfigurations: [pixelSettings],
@@ -170,7 +170,7 @@ describe.only('CookieSyncManager', () => {
170170
},
171171
} as unknown) as MParticleWebSDK;
172172

173-
const cookieSyncManager = new CookieSyncManager(mockSDK);
173+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
174174
cookieSyncManager.performCookieSync = jest.fn();
175175

176176
cookieSyncManager.attemptCookieSync('other-mpid', testMPID, true);
@@ -187,7 +187,7 @@ describe.only('CookieSyncManager', () => {
187187
});
188188

189189
it('should call performCookieSync with mpid and csd if previousMpid and mpid do not match', () => {
190-
const mockSDK = ({
190+
const mockMPInstance = ({
191191
_Store: {
192192
webviewBridgeEnabled: false,
193193
pixelConfigurations: [pixelSettings],
@@ -199,7 +199,7 @@ describe.only('CookieSyncManager', () => {
199199
},
200200
} as unknown) as MParticleWebSDK;
201201

202-
const cookieSyncManager = new CookieSyncManager(mockSDK);
202+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
203203
cookieSyncManager.performCookieSync = jest.fn();
204204

205205
cookieSyncManager.attemptCookieSync('other-mpid', testMPID, true);
@@ -219,7 +219,7 @@ describe.only('CookieSyncManager', () => {
219219

220220
// QUESTION: What is the purpose of this code path?
221221
it('should call performCookieSync with mpid if previousMpid and mpid match', () => {
222-
const mockSDK = ({
222+
const mockMPInstance = ({
223223
_Store: {
224224
webviewBridgeEnabled: false,
225225
pixelConfigurations: [pixelSettings],
@@ -230,7 +230,7 @@ describe.only('CookieSyncManager', () => {
230230
},
231231
} as unknown) as MParticleWebSDK;
232232

233-
const cookieSyncManager = new CookieSyncManager(mockSDK);
233+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
234234
cookieSyncManager.performCookieSync = jest.fn();
235235

236236
cookieSyncManager.attemptCookieSync(testMPID, testMPID, true);
@@ -246,13 +246,13 @@ describe.only('CookieSyncManager', () => {
246246
);
247247
});
248248

249-
it('should perform a cookie sync if lastSyncDateForModule is passed the frequency cap', () => {
249+
it('should perform a cookie sync if lastSyncDateForModule has passed the frequency cap', () => {
250250
const now = new Date().getTime();
251251

252252
// Rev the date by one
253-
const cookieSyncDateInPast = now - ( pixelSettings.frequencyCap + 1 ) * 60 * 60 * 24 * 1000;
253+
const cookieSyncDateInPast = now - ( pixelSettings.frequencyCap + 1 ) * DAYS_IN_MILLISECONDS;
254254

255-
const mockSDK = ({
255+
const mockMPInstance = ({
256256
_Store: {
257257
webviewBridgeEnabled: false,
258258
pixelConfigurations: [pixelSettings],
@@ -264,7 +264,7 @@ describe.only('CookieSyncManager', () => {
264264
},
265265
} as unknown) as MParticleWebSDK;
266266

267-
const cookieSyncManager = new CookieSyncManager(mockSDK);
267+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
268268
cookieSyncManager.performCookieSync = jest.fn();
269269

270270
cookieSyncManager.attemptCookieSync(null, testMPID, true);
@@ -285,7 +285,7 @@ describe.only('CookieSyncManager', () => {
285285
it('should perform a cookie sync if lastSyncDateForModule is past the frequency cap even if csd is empty', () => {
286286
const now = new Date().getTime();
287287

288-
const mockSDK = ({
288+
const mockMPInstance = ({
289289
_Store: {
290290
webviewBridgeEnabled: false,
291291
pixelConfigurations: [pixelSettings],
@@ -297,7 +297,7 @@ describe.only('CookieSyncManager', () => {
297297
},
298298
} as unknown) as MParticleWebSDK;
299299

300-
const cookieSyncManager = new CookieSyncManager(mockSDK);
300+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
301301
cookieSyncManager.performCookieSync = jest.fn();
302302

303303
cookieSyncManager.attemptCookieSync(null, testMPID, true);
@@ -314,7 +314,7 @@ describe.only('CookieSyncManager', () => {
314314
});
315315

316316
it('should sync cookies when there was not a previous cookie-sync', () => {
317-
const mockSDK = ({
317+
const mockMPInstance = ({
318318
_Store: {
319319
webviewBridgeEnabled: false,
320320
pixelConfigurations: [pixelSettings],
@@ -324,11 +324,11 @@ describe.only('CookieSyncManager', () => {
324324
},
325325
} as unknown) as MParticleWebSDK;
326326

327-
const cookieSyncManager = new CookieSyncManager(mockSDK);
327+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
328328

329329
cookieSyncManager.attemptCookieSync(null, '456', true);
330-
expect(mockSDK._Store.pixelConfigurations.length).toBe(1);
331-
expect(mockSDK._Store.pixelConfigurations[0].moduleId).toBe(5);
330+
expect(mockMPInstance._Store.pixelConfigurations.length).toBe(1);
331+
expect(mockMPInstance._Store.pixelConfigurations[0].moduleId).toBe(5);
332332
});
333333
});
334334

@@ -342,7 +342,7 @@ describe.only('CookieSyncManager', () => {
342342
(mockImage as unknown) as HTMLImageElement
343343
);
344344

345-
const mockSDK = ({
345+
const mockMPInstance = ({
346346
_Store: {
347347
webviewBridgeEnabled: false,
348348
pixelConfigurations: [pixelSettings],
@@ -365,7 +365,7 @@ describe.only('CookieSyncManager', () => {
365365
},
366366
} as unknown) as MParticleWebSDK;
367367

368-
const cookieSyncManager = new CookieSyncManager(mockSDK);
368+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
369369

370370
let cookieSyncDates = [];
371371
cookieSyncManager.performCookieSync(
@@ -397,7 +397,7 @@ describe.only('CookieSyncManager', () => {
397397

398398
const loggerSpy = jest.fn();
399399

400-
const mockSDK = ({
400+
const mockMPInstance = ({
401401
_Store: {
402402
webviewBridgeEnabled: false,
403403
pixelConfigurations: [pixelSettings],
@@ -420,7 +420,7 @@ describe.only('CookieSyncManager', () => {
420420
},
421421
} as unknown) as MParticleWebSDK;
422422

423-
const cookieSyncManager = new CookieSyncManager(mockSDK);
423+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
424424

425425
let cookieSyncDates = [];
426426
cookieSyncManager.performCookieSync(
@@ -439,7 +439,7 @@ describe.only('CookieSyncManager', () => {
439439
expect(loggerSpy).toHaveBeenCalledWith('Performing cookie sync');
440440
});
441441

442-
it('should return early if the user has not consented to the cookie sync', () => {
442+
it.only('should return early if the user has not consented to the cookie sync', () => {
443443
const mockImage = {
444444
onload: jest.fn(),
445445
src: '',
@@ -450,7 +450,61 @@ describe.only('CookieSyncManager', () => {
450450

451451
const loggerSpy = jest.fn();
452452

453-
const mockSDK = ({
453+
const mockMPInstance = ({
454+
_Store: {
455+
webviewBridgeEnabled: false,
456+
pixelConfigurations: [pixelSettings],
457+
},
458+
_Persistence: {
459+
setCookieSyncDates: jest.fn(),
460+
getPersistence: jest.fn(),
461+
saveUserCookieSyncDatesToPersistence: jest.fn(),
462+
},
463+
_Consent: {
464+
isEnabledForUserConsent: jest.fn().mockReturnValue(false),
465+
},
466+
Identity: {
467+
getCurrentUser: jest.fn().mockReturnValue({
468+
getMPID: () => '123',
469+
}),
470+
},
471+
Logger: {
472+
verbose: loggerSpy,
473+
},
474+
} as unknown) as MParticleWebSDK;
475+
476+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
477+
478+
let cookieSyncDates = [];
479+
cookieSyncManager.performCookieSync(
480+
'https://test.com',
481+
42,
482+
'1234',
483+
cookieSyncDates,
484+
null,
485+
false,
486+
false,
487+
);
488+
489+
// Simulate image load
490+
mockImage.onload();
491+
492+
expect(mockImage.src).toBe('');
493+
expect(cookieSyncDates[42]).toBeUndefined();
494+
});
495+
496+
it.only('should return early if requiresConsent and mpidIsNotInCookies are both true', () => {
497+
const mockImage = {
498+
onload: jest.fn(),
499+
src: '',
500+
};
501+
jest.spyOn(document, 'createElement').mockReturnValue(
502+
(mockImage as unknown) as HTMLImageElement
503+
);
504+
505+
const loggerSpy = jest.fn();
506+
507+
const mockMPInstance = ({
454508
_Store: {
455509
webviewBridgeEnabled: false,
456510
pixelConfigurations: [pixelSettings],
@@ -473,7 +527,7 @@ describe.only('CookieSyncManager', () => {
473527
},
474528
} as unknown) as MParticleWebSDK;
475529

476-
const cookieSyncManager = new CookieSyncManager(mockSDK);
530+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
477531

478532
let cookieSyncDates = [];
479533
cookieSyncManager.performCookieSync(
@@ -483,7 +537,7 @@ describe.only('CookieSyncManager', () => {
483537
cookieSyncDates,
484538
null,
485539
true,
486-
true
540+
true,
487541
);
488542

489543
// Simulate image load
@@ -496,14 +550,14 @@ describe.only('CookieSyncManager', () => {
496550

497551
describe('#combineUrlWithRedirect', () => {
498552
it('should combine a pixelUrl with a redirectUrl', () => {
499-
const mockSDK = ({
553+
const mockMPInstance = ({
500554
_Store: {
501555
webviewBridgeEnabled: false,
502556
pixelConfigurations: [pixelSettings],
503557
},
504558
} as unknown) as MParticleWebSDK;
505559

506-
const cookieSyncManager = new CookieSyncManager(mockSDK);
560+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
507561

508562
const result = cookieSyncManager.combineUrlWithRedirect(
509563
'1234',
@@ -515,14 +569,14 @@ describe.only('CookieSyncManager', () => {
515569
});
516570

517571
it('should return the pixelUrl if no redirectUrl is defined', () => {
518-
const mockSDK = ({
572+
const mockMPInstance = ({
519573
_Store: {
520574
webviewBridgeEnabled: false,
521575
pixelConfigurations: [pixelSettings],
522576
},
523577
} as unknown) as MParticleWebSDK;
524578

525-
const cookieSyncManager = new CookieSyncManager(mockSDK);
579+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
526580

527581
const result = cookieSyncManager.combineUrlWithRedirect(
528582
'1234',

0 commit comments

Comments
 (0)