Skip to content

Commit d72b41c

Browse files
Address PR Comments
1 parent 6dd5dbb commit d72b41c

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

src/cookieSyncManager.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const hasFrequencyCapExpired = (
1414
frequencyCap: number,
1515
lastSyncDate?: number,
1616
): boolean => {
17+
if (!lastSyncDate) {
18+
return true;
19+
}
20+
1721
return (
1822
new Date().getTime() >
1923
new Date(lastSyncDate).getTime() + frequencyCap * DAYS_IN_MILLISECONDS
@@ -61,7 +65,6 @@ export default function CookieSyncManager(
6165
// Url for cookie sync pixel
6266
const pixelUrl = replaceAmpWithAmpersand(pixelSettings.pixelUrl);
6367

64-
// TODO: Document requirements for redirectUrl
6568
const redirectUrl = pixelSettings.redirectUrl
6669
? replaceAmpWithAmpersand(pixelSettings.redirectUrl)
6770
: null;
@@ -72,8 +75,6 @@ export default function CookieSyncManager(
7275
redirectUrl
7376
);
7477

75-
// TODO: Is there a historic reason for checking for previousMPID?
76-
// it does not appear to be passed in anywhere
7778
if (previousMPID && previousMPID !== mpid) {
7879
if (persistence && persistence[mpid]) {
7980
if (!persistence[mpid].csd) {

test/jest/cookieSyncManager.spec.ts

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ describe('CookieSyncManager', () => {
218218
);
219219
});
220220

221-
// QUESTION: What is the purpose of this code path?
222221
it('should call performCookieSync with mpid if previousMpid and mpid match', () => {
223222
const mockMPInstance = ({
224223
_Store: {
@@ -247,6 +246,33 @@ describe('CookieSyncManager', () => {
247246
);
248247
});
249248

249+
it('should perform a cookie sync if lastSyncDateForModule is null', () => {
250+
const mockMPInstance = ({
251+
_Store: {
252+
webviewBridgeEnabled: false,
253+
pixelConfigurations: [pixelSettings],
254+
},
255+
_Persistence: {
256+
getPersistence: () => ({testMPID: {}}),
257+
},
258+
} as unknown) as MParticleWebSDK;
259+
260+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
261+
cookieSyncManager.performCookieSync = jest.fn();
262+
263+
cookieSyncManager.attemptCookieSync(null, testMPID, true);
264+
265+
expect(cookieSyncManager.performCookieSync).toHaveBeenCalledWith(
266+
'',
267+
'5',
268+
testMPID,
269+
{},
270+
undefined,
271+
true,
272+
false,
273+
);
274+
});
275+
250276
it('should perform a cookie sync if lastSyncDateForModule has passed the frequency cap', () => {
251277
const now = new Date().getTime();
252278

@@ -331,6 +357,25 @@ describe('CookieSyncManager', () => {
331357
expect(mockMPInstance._Store.pixelConfigurations.length).toBe(1);
332358
expect(mockMPInstance._Store.pixelConfigurations[0].moduleId).toBe(5);
333359
});
360+
361+
it('should not perform a cookie sync if persistence is empty', () => {
362+
const mockMPInstance = ({
363+
_Store: {
364+
webviewBridgeEnabled: false,
365+
pixelConfigurations: [pixelSettings],
366+
},
367+
_Persistence: {
368+
getPersistence: () => ({}),
369+
},
370+
} as unknown) as MParticleWebSDK;
371+
372+
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
373+
cookieSyncManager.performCookieSync = jest.fn();
374+
375+
cookieSyncManager.attemptCookieSync(null, testMPID, true);
376+
377+
expect(cookieSyncManager.performCookieSync).not.toHaveBeenCalled();
378+
});
334379
});
335380

336381
describe('#performCookieSync', () => {
@@ -560,13 +605,16 @@ describe('CookieSyncManager', () => {
560605

561606
const cookieSyncManager = new CookieSyncManager(mockMPInstance);
562607

608+
// Note: We expect that the input of the pixelUrl will include a `?`
609+
// (ideally at the end of the string)
610+
// so that the redirectUrl can be processed by the backend correctly
563611
const result = cookieSyncManager.combineUrlWithRedirect(
564612
'1234',
565-
'https://test.com/some/path',
613+
'https://test.com/some/path?',
566614
'https://redirect.mparticle.com/v1/sync?esid=1234&MPID=%%mpid%%&ID=$UID&Key=testMPID&env=2'
567615
);
568616

569-
expect(result).toBe('https://test.com/some/pathhttps%3A%2F%2Fredirect.mparticle.com%2Fv1%2Fsync%3Fesid%3D1234%26amp%3BMPID%3D1234%26amp%3BID%3D%24UID%26amp%3BKey%3DtestMPID%26amp%3Benv%3D2');
617+
expect(result).toBe('https://test.com/some/path?https%3A%2F%2Fredirect.mparticle.com%2Fv1%2Fsync%3Fesid%3D1234%26amp%3BMPID%3D1234%26amp%3BID%3D%24UID%26amp%3BKey%3DtestMPID%26amp%3Benv%3D2');
570618
});
571619

572620
it('should return the pixelUrl if no redirectUrl is defined', () => {

0 commit comments

Comments
 (0)