From b7230d6435dc8b150c51cab57a1e4fd9a6a272c2 Mon Sep 17 00:00:00 2001 From: Jaissica Date: Tue, 16 Sep 2025 11:39:14 -0400 Subject: [PATCH 1/9] feat: disable product cookie when persistence is set and updated test --- src/persistence.js | 48 +++++++++++++++++++++++++--- test/src/tests-persistence.ts | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 5 deletions(-) diff --git a/src/persistence.js b/src/persistence.js index cb40e2f8e..8c47094db 100644 --- a/src/persistence.js +++ b/src/persistence.js @@ -89,6 +89,13 @@ export default function _Persistence(mpInstance) { // https://go.mparticle.com/work/SQDSDKS-6048 try { if (mpInstance._Store.isLocalStorageAvailable) { + if (mpInstance._Store.getNoTargeting()) { + localStorage.removeItem( + mpInstance._Store.prodStorageName + ); + mpInstance._Store.cartProducts = []; + return; + } var encodedProducts = localStorage.getItem( mpInstance._Store.prodStorageName ); @@ -269,7 +276,10 @@ export default function _Persistence(mpInstance) { }; this.getUserProductsFromLS = function(mpid) { - if (!mpInstance._Store.isLocalStorageAvailable) { + if ( + mpInstance._Store.getNoTargeting() || + !mpInstance._Store.isLocalStorageAvailable + ) { return []; } @@ -308,6 +318,15 @@ export default function _Persistence(mpInstance) { }; this.getAllUserProductsFromLS = function() { + if (mpInstance._Store.getNoTargeting()) { + var currentUser = mpInstance.Identity.getCurrentUser(); + var mpid = currentUser ? currentUser.getMPID() : null; + var result = {}; + if (mpid) { + result[mpid] = { cp: [] }; + } + return result; + } var decodedProducts, encodedProducts = localStorage.getItem( mpInstance._Store.prodStorageName @@ -342,14 +361,24 @@ export default function _Persistence(mpInstance) { ? allLocalStorageProducts[mpid].cp : [], }; + + if (mpInstance._Store.getNoTargeting()) { + try { + window.localStorage.removeItem( + mpInstance._Store.prodStorageName + ); + } catch (e) {} + } if (mpid) { allLocalStorageProducts = allLocalStorageProducts || {}; allLocalStorageProducts[mpid] = currentUserProducts; try { - window.localStorage.setItem( - encodeURIComponent(mpInstance._Store.prodStorageName), - Base64.encode(JSON.stringify(allLocalStorageProducts)) - ); + if (!mpInstance._Store.getNoTargeting()) { + window.localStorage.setItem( + encodeURIComponent(mpInstance._Store.prodStorageName), + Base64.encode(JSON.stringify(allLocalStorageProducts)) + ); + } } catch (e) { mpInstance.Logger.error( 'Error with setting products on localStorage.' @@ -894,6 +923,9 @@ export default function _Persistence(mpInstance) { }; this.getCartProducts = function(mpid) { + if (mpInstance._Store.getNoTargeting()) { + return []; + } var allCartProducts, cartProductsString = localStorage.getItem( mpInstance._Store.prodStorageName @@ -916,6 +948,12 @@ export default function _Persistence(mpInstance) { if (!mpInstance._Store.isLocalStorageAvailable) { return; } + if (mpInstance._Store.getNoTargeting()) { + try { + localStorage.removeItem(mpInstance._Store.prodStorageName); + } catch (e) {} + return; + } try { window.localStorage.setItem( diff --git a/test/src/tests-persistence.ts b/test/src/tests-persistence.ts index 424f4d616..de76140a0 100644 --- a/test/src/tests-persistence.ts +++ b/test/src/tests-persistence.ts @@ -1661,6 +1661,65 @@ describe('persistence', () => { parsedProductsAfter['testMPID'].cp.length.should.equal(0); }); + describe('products persistence with noTargeting privacy flag', () => { + beforeEach(() => { + mParticle._resetForTests(MPConfig); + }); + + it('should not save products LS when noTargeting is true', async () => { + mParticle.config.noTargeting = true; + + mParticle.init(apiKey, mParticle.config); + await waitForCondition(hasIdentifyReturned); + const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName); + expect(localStorageProducts).to.not.be.ok; + }); + + it('should clear existing products LS when noTargeting is true', async () => { + localStorage.setItem( + LocalStorageProductsV4WithWorkSpaceName, + btoa(JSON.stringify({ testMPID: { cp: [{ Name: 'x' }] } })) + ); + mParticle.config.noTargeting = true; + mParticle.init(apiKey, mParticle.config); + await waitForCondition(hasIdentifyReturned); + const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName); + expect(localStorageProducts).to.not.be.ok; + }); + + it('should save products LS when noTargeting is false', async () => { + mParticle.config.noTargeting = false; + + mParticle.init(apiKey, mParticle.config); + await waitForCondition(hasIdentifyReturned); + const iphone = mParticle.eCommerce.createProduct( + 'iphone', + 'iphonesku', + 599, + 1 + ); + mParticle.eCommerce.Cart.add(iphone, true); + + const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName); + expect(localStorageProducts).to.be.ok; + }); + + it('should save products LS when noTargeting is default (false)', async () => { + mParticle.init(apiKey, mParticle.config); + await waitForCondition(hasIdentifyReturned); + const iphone = mParticle.eCommerce.createProduct( + 'iphone', + 'iphonesku', + 599, + 1 + ); + mParticle.eCommerce.Cart.add(iphone, true); + + const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName); + expect(localStorageProducts).to.be.ok; + }); + }); + it('should only set setFirstSeenTime() once', async () => { const cookies = JSON.stringify({ gs: { From b14099e9dd549aa6cac44ceb7b6ca05d12b2b7bb Mon Sep 17 00:00:00 2001 From: Jaissica Date: Tue, 16 Sep 2025 16:47:33 -0400 Subject: [PATCH 2/9] test: added jest tests and updated test content --- src/persistence.js | 29 +++++++++-------------------- test/jest/persistence.spec.ts | 33 +++++++++++++++++++++++++++++++++ test/src/tests-persistence.ts | 14 +------------- 3 files changed, 43 insertions(+), 33 deletions(-) create mode 100644 test/jest/persistence.spec.ts diff --git a/src/persistence.js b/src/persistence.js index 8c47094db..46427f2b8 100644 --- a/src/persistence.js +++ b/src/persistence.js @@ -88,14 +88,10 @@ export default function _Persistence(mpInstance) { // https://go.mparticle.com/work/SQDSDKS-6048 try { - if (mpInstance._Store.isLocalStorageAvailable) { - if (mpInstance._Store.getNoTargeting()) { - localStorage.removeItem( - mpInstance._Store.prodStorageName - ); - mpInstance._Store.cartProducts = []; - return; - } + if ( + mpInstance._Store.isLocalStorageAvailable && + !mpInstance._Store.getNoTargeting() + ) { var encodedProducts = localStorage.getItem( mpInstance._Store.prodStorageName ); @@ -363,11 +359,7 @@ export default function _Persistence(mpInstance) { }; if (mpInstance._Store.getNoTargeting()) { - try { - window.localStorage.removeItem( - mpInstance._Store.prodStorageName - ); - } catch (e) {} + return; } if (mpid) { allLocalStorageProducts = allLocalStorageProducts || {}; @@ -945,13 +937,10 @@ export default function _Persistence(mpInstance) { }; this.setCartProducts = function(allProducts) { - if (!mpInstance._Store.isLocalStorageAvailable) { - return; - } - if (mpInstance._Store.getNoTargeting()) { - try { - localStorage.removeItem(mpInstance._Store.prodStorageName); - } catch (e) {} + if ( + !mpInstance._Store.isLocalStorageAvailable && + mpInstance._Store.getNoTargeting() + ) { return; } diff --git a/test/jest/persistence.spec.ts b/test/jest/persistence.spec.ts new file mode 100644 index 000000000..2ead56d20 --- /dev/null +++ b/test/jest/persistence.spec.ts @@ -0,0 +1,33 @@ +import { + mParticle, + MPConfig, + testMPID, +} from '../src/config/constants'; + +describe('Products Persistence', () => { + let mpInstance = mParticle.getInstance(); + beforeEach(() => { + mParticle._resetForTests(MPConfig); + }); + + it('should update Products localStorage when noTargeting is false by default', () => { + const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; + mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any); + expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); + }); + + it('should NOT update Products localStorage when noTargeting is true', () => { + mpInstance._Store.setNoTargeting(true); + const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; + mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any); + expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(0); + expect(mpInstance._Persistence.getCartProducts(testMPID)).toEqual([]); + }); + + it('should update Products localStorage when noTargeting is false', () => { + mpInstance._Store.setNoTargeting(false); + const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; + mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any); + expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); + }); +}); \ No newline at end of file diff --git a/test/src/tests-persistence.ts b/test/src/tests-persistence.ts index de76140a0..56409fbd8 100644 --- a/test/src/tests-persistence.ts +++ b/test/src/tests-persistence.ts @@ -1674,18 +1674,6 @@ describe('persistence', () => { const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName); expect(localStorageProducts).to.not.be.ok; }); - - it('should clear existing products LS when noTargeting is true', async () => { - localStorage.setItem( - LocalStorageProductsV4WithWorkSpaceName, - btoa(JSON.stringify({ testMPID: { cp: [{ Name: 'x' }] } })) - ); - mParticle.config.noTargeting = true; - mParticle.init(apiKey, mParticle.config); - await waitForCondition(hasIdentifyReturned); - const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName); - expect(localStorageProducts).to.not.be.ok; - }); it('should save products LS when noTargeting is false', async () => { mParticle.config.noTargeting = false; @@ -1704,7 +1692,7 @@ describe('persistence', () => { expect(localStorageProducts).to.be.ok; }); - it('should save products LS when noTargeting is default (false)', async () => { + it('should save products LS when noTargeting is false by default', async () => { mParticle.init(apiKey, mParticle.config); await waitForCondition(hasIdentifyReturned); const iphone = mParticle.eCommerce.createProduct( From 507b3e5d7f89c752f0a3c32da4f869aad11f73cc Mon Sep 17 00:00:00 2001 From: Jaissica Date: Fri, 19 Sep 2025 14:41:15 -0400 Subject: [PATCH 3/9] refactor: updated getPrivacyFlag in persistence and updated test content --- src/persistence.js | 16 ++++++++-------- test/jest/persistence.spec.ts | 14 +++++++------- test/src/tests-persistence.ts | 6 +++--- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/persistence.js b/src/persistence.js index 46427f2b8..463f01618 100644 --- a/src/persistence.js +++ b/src/persistence.js @@ -90,7 +90,7 @@ export default function _Persistence(mpInstance) { try { if ( mpInstance._Store.isLocalStorageAvailable && - !mpInstance._Store.getNoTargeting() + !mpInstance._Store.getPrivacyFlag('Products') ) { var encodedProducts = localStorage.getItem( mpInstance._Store.prodStorageName @@ -273,7 +273,7 @@ export default function _Persistence(mpInstance) { this.getUserProductsFromLS = function(mpid) { if ( - mpInstance._Store.getNoTargeting() || + mpInstance._Store.getPrivacyFlag('Products') || !mpInstance._Store.isLocalStorageAvailable ) { return []; @@ -314,7 +314,7 @@ export default function _Persistence(mpInstance) { }; this.getAllUserProductsFromLS = function() { - if (mpInstance._Store.getNoTargeting()) { + if (mpInstance._Store.getPrivacyFlag('Products')) { var currentUser = mpInstance.Identity.getCurrentUser(); var mpid = currentUser ? currentUser.getMPID() : null; var result = {}; @@ -358,14 +358,14 @@ export default function _Persistence(mpInstance) { : [], }; - if (mpInstance._Store.getNoTargeting()) { + if (mpInstance._Store.getPrivacyFlag('Products')) { return; } if (mpid) { allLocalStorageProducts = allLocalStorageProducts || {}; allLocalStorageProducts[mpid] = currentUserProducts; try { - if (!mpInstance._Store.getNoTargeting()) { + if (!mpInstance._Store.getPrivacyFlag('Products')) { window.localStorage.setItem( encodeURIComponent(mpInstance._Store.prodStorageName), Base64.encode(JSON.stringify(allLocalStorageProducts)) @@ -915,7 +915,7 @@ export default function _Persistence(mpInstance) { }; this.getCartProducts = function(mpid) { - if (mpInstance._Store.getNoTargeting()) { + if (mpInstance._Store.getPrivacyFlag('Products')) { return []; } var allCartProducts, @@ -938,8 +938,8 @@ export default function _Persistence(mpInstance) { this.setCartProducts = function(allProducts) { if ( - !mpInstance._Store.isLocalStorageAvailable && - mpInstance._Store.getNoTargeting() + !mpInstance._Store.isLocalStorageAvailable || + mpInstance._Store.getPrivacyFlag('Products') ) { return; } diff --git a/test/jest/persistence.spec.ts b/test/jest/persistence.spec.ts index 2ead56d20..9adc2d2df 100644 --- a/test/jest/persistence.spec.ts +++ b/test/jest/persistence.spec.ts @@ -3,6 +3,7 @@ import { MPConfig, testMPID, } from '../src/config/constants'; +import { Product } from '@mparticle/web-sdk'; describe('Products Persistence', () => { let mpInstance = mParticle.getInstance(); @@ -10,24 +11,23 @@ describe('Products Persistence', () => { mParticle._resetForTests(MPConfig); }); - it('should update Products localStorage when noTargeting is false by default', () => { + it('should save products to localStorage when noTargeting is false by default', () => { const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; - mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any); + mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); }); - it('should NOT update Products localStorage when noTargeting is true', () => { + it('should NOT save products to localStorage when noTargeting is true', () => { mpInstance._Store.setNoTargeting(true); const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; - mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any); + mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(0); - expect(mpInstance._Persistence.getCartProducts(testMPID)).toEqual([]); }); - it('should update Products localStorage when noTargeting is false', () => { + it('should save products to localStorage when noTargeting is false', () => { mpInstance._Store.setNoTargeting(false); const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; - mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any); + mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); }); }); \ No newline at end of file diff --git a/test/src/tests-persistence.ts b/test/src/tests-persistence.ts index 56409fbd8..278b050cf 100644 --- a/test/src/tests-persistence.ts +++ b/test/src/tests-persistence.ts @@ -1666,7 +1666,7 @@ describe('persistence', () => { mParticle._resetForTests(MPConfig); }); - it('should not save products LS when noTargeting is true', async () => { + it('should NOT save products to LS when noTargeting is true', async () => { mParticle.config.noTargeting = true; mParticle.init(apiKey, mParticle.config); @@ -1675,7 +1675,7 @@ describe('persistence', () => { expect(localStorageProducts).to.not.be.ok; }); - it('should save products LS when noTargeting is false', async () => { + it('should save products to LS when noTargeting is false', async () => { mParticle.config.noTargeting = false; mParticle.init(apiKey, mParticle.config); @@ -1692,7 +1692,7 @@ describe('persistence', () => { expect(localStorageProducts).to.be.ok; }); - it('should save products LS when noTargeting is false by default', async () => { + it('should save products to LS when noTargeting is false by default', async () => { mParticle.init(apiKey, mParticle.config); await waitForCondition(hasIdentifyReturned); const iphone = mParticle.eCommerce.createProduct( From 9f062cacbd1b8c4750f2459d649ce72db83f3f1e Mon Sep 17 00:00:00 2001 From: Jaissica Date: Fri, 19 Sep 2025 15:09:04 -0400 Subject: [PATCH 4/9] test: updated jest test for persistence --- test/jest/persistence.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/jest/persistence.spec.ts b/test/jest/persistence.spec.ts index 9adc2d2df..f84400897 100644 --- a/test/jest/persistence.spec.ts +++ b/test/jest/persistence.spec.ts @@ -9,6 +9,7 @@ describe('Products Persistence', () => { let mpInstance = mParticle.getInstance(); beforeEach(() => { mParticle._resetForTests(MPConfig); + localStorage.removeItem(mpInstance._Store?.prodStorageName as string); }); it('should save products to localStorage when noTargeting is false by default', () => { From d09d4c94e493ec6f90f1a4105a805a7ec8cfc0c6 Mon Sep 17 00:00:00 2001 From: Jaissica Date: Fri, 19 Sep 2025 15:32:00 -0400 Subject: [PATCH 5/9] test: updated jest test for persistence failure --- test/jest/persistence.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jest/persistence.spec.ts b/test/jest/persistence.spec.ts index f84400897..994b0b4be 100644 --- a/test/jest/persistence.spec.ts +++ b/test/jest/persistence.spec.ts @@ -9,7 +9,7 @@ describe('Products Persistence', () => { let mpInstance = mParticle.getInstance(); beforeEach(() => { mParticle._resetForTests(MPConfig); - localStorage.removeItem(mpInstance._Store?.prodStorageName as string); + mpInstance._Persistence.resetPersistence(); }); it('should save products to localStorage when noTargeting is false by default', () => { From 0847f1e1c912abcfbe6a79b129c631ba6df589fc Mon Sep 17 00:00:00 2001 From: Jaissica Date: Tue, 23 Sep 2025 13:55:00 -0400 Subject: [PATCH 6/9] test: source privacy flag from launcherOptions --- test/src/tests-persistence.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/src/tests-persistence.ts b/test/src/tests-persistence.ts index 278b050cf..06133277c 100644 --- a/test/src/tests-persistence.ts +++ b/test/src/tests-persistence.ts @@ -1667,7 +1667,7 @@ describe('persistence', () => { }); it('should NOT save products to LS when noTargeting is true', async () => { - mParticle.config.noTargeting = true; + mParticle.config.launcherOptions = { noTargeting: true }; mParticle.init(apiKey, mParticle.config); await waitForCondition(hasIdentifyReturned); @@ -1676,7 +1676,7 @@ describe('persistence', () => { }); it('should save products to LS when noTargeting is false', async () => { - mParticle.config.noTargeting = false; + mParticle.config.launcherOptions = { noTargeting: false }; mParticle.init(apiKey, mParticle.config); await waitForCondition(hasIdentifyReturned); From bfcdb2bf4caeca2d33c67906fe414be10ca27a95 Mon Sep 17 00:00:00 2001 From: Jaissica Date: Thu, 25 Sep 2025 16:39:03 -0400 Subject: [PATCH 7/9] test: move/rename test to persistence.products.spec --- test/jest/persistence.products.spec.ts | 34 ++++++++++++++++++++++++++ test/jest/persistence.spec.ts | 34 -------------------------- 2 files changed, 34 insertions(+), 34 deletions(-) create mode 100644 test/jest/persistence.products.spec.ts delete mode 100644 test/jest/persistence.spec.ts diff --git a/test/jest/persistence.products.spec.ts b/test/jest/persistence.products.spec.ts new file mode 100644 index 000000000..7a702acce --- /dev/null +++ b/test/jest/persistence.products.spec.ts @@ -0,0 +1,34 @@ +import { + mParticle, + MPConfig, + testMPID, + } from '../src/config/constants'; + import { Product } from '@mparticle/web-sdk'; + + describe('Products Persistence', () => { + let mpInstance = mParticle.getInstance(); + beforeEach(() => { + mParticle._resetForTests(MPConfig); + mpInstance._Persistence.resetPersistence(); + }); + + it('should save products to localStorage when noTargeting is false by default', () => { + const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; + mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); + expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); + }); + + it('should NOT save products to localStorage when noTargeting is true', () => { + mpInstance._Store.setNoTargeting(true); + const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; + mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); + expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(0); + }); + + it('should save products to localStorage when noTargeting is false', () => { + mpInstance._Store.setNoTargeting(false); + const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; + mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); + expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); + }); + }); \ No newline at end of file diff --git a/test/jest/persistence.spec.ts b/test/jest/persistence.spec.ts deleted file mode 100644 index 994b0b4be..000000000 --- a/test/jest/persistence.spec.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { - mParticle, - MPConfig, - testMPID, -} from '../src/config/constants'; -import { Product } from '@mparticle/web-sdk'; - -describe('Products Persistence', () => { - let mpInstance = mParticle.getInstance(); - beforeEach(() => { - mParticle._resetForTests(MPConfig); - mpInstance._Persistence.resetPersistence(); - }); - - it('should save products to localStorage when noTargeting is false by default', () => { - const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; - mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); - expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); - }); - - it('should NOT save products to localStorage when noTargeting is true', () => { - mpInstance._Store.setNoTargeting(true); - const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; - mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); - expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(0); - }); - - it('should save products to localStorage when noTargeting is false', () => { - mpInstance._Store.setNoTargeting(false); - const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; - mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); - expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); - }); -}); \ No newline at end of file From 6c0c0c81b15204caa4560cc3c68dcbb2bf1da30a Mon Sep 17 00:00:00 2001 From: Jaissica Date: Fri, 26 Sep 2025 09:44:27 -0400 Subject: [PATCH 8/9] chore: removed redundant if block --- src/persistence.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/persistence.js b/src/persistence.js index 463f01618..5f5eb7562 100644 --- a/src/persistence.js +++ b/src/persistence.js @@ -365,12 +365,10 @@ export default function _Persistence(mpInstance) { allLocalStorageProducts = allLocalStorageProducts || {}; allLocalStorageProducts[mpid] = currentUserProducts; try { - if (!mpInstance._Store.getPrivacyFlag('Products')) { - window.localStorage.setItem( - encodeURIComponent(mpInstance._Store.prodStorageName), - Base64.encode(JSON.stringify(allLocalStorageProducts)) - ); - } + window.localStorage.setItem( + encodeURIComponent(mpInstance._Store.prodStorageName), + Base64.encode(JSON.stringify(allLocalStorageProducts)) + ); } catch (e) { mpInstance.Logger.error( 'Error with setting products on localStorage.' From 95d8fd80252055148470b68b2650c53d35c0d215 Mon Sep 17 00:00:00 2001 From: Jaissica Date: Fri, 26 Sep 2025 10:28:45 -0400 Subject: [PATCH 9/9] test: updated products persistence test --- src/persistence.js | 9 ++++++--- test/jest/persistence.products.spec.ts | 6 +++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/persistence.js b/src/persistence.js index 5f5eb7562..8f2bcacef 100644 --- a/src/persistence.js +++ b/src/persistence.js @@ -365,10 +365,13 @@ export default function _Persistence(mpInstance) { allLocalStorageProducts = allLocalStorageProducts || {}; allLocalStorageProducts[mpid] = currentUserProducts; try { - window.localStorage.setItem( - encodeURIComponent(mpInstance._Store.prodStorageName), - Base64.encode(JSON.stringify(allLocalStorageProducts)) + const encodedKey = encodeURIComponent( + mpInstance._Store.prodStorageName + ); + const encodedValue = Base64.encode( + JSON.stringify(allLocalStorageProducts) ); + window.localStorage.setItem(encodedKey, encodedValue); } catch (e) { mpInstance.Logger.error( 'Error with setting products on localStorage.' diff --git a/test/jest/persistence.products.spec.ts b/test/jest/persistence.products.spec.ts index 7a702acce..ddeba7b58 100644 --- a/test/jest/persistence.products.spec.ts +++ b/test/jest/persistence.products.spec.ts @@ -13,21 +13,21 @@ import { }); it('should save products to localStorage when noTargeting is false by default', () => { - const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; + const product: Partial = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); }); it('should NOT save products to localStorage when noTargeting is true', () => { mpInstance._Store.setNoTargeting(true); - const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; + const product: Partial = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(0); }); it('should save products to localStorage when noTargeting is false', () => { mpInstance._Store.setNoTargeting(false); - const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; + const product: Partial = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); });