Skip to content

Commit 507b3e5

Browse files
committed
refactor: updated getPrivacyFlag in persistence and updated test content
1 parent bbe4f08 commit 507b3e5

3 files changed

Lines changed: 18 additions & 18 deletions

File tree

src/persistence.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export default function _Persistence(mpInstance) {
9090
try {
9191
if (
9292
mpInstance._Store.isLocalStorageAvailable &&
93-
!mpInstance._Store.getNoTargeting()
93+
!mpInstance._Store.getPrivacyFlag('Products')
9494
) {
9595
var encodedProducts = localStorage.getItem(
9696
mpInstance._Store.prodStorageName
@@ -273,7 +273,7 @@ export default function _Persistence(mpInstance) {
273273

274274
this.getUserProductsFromLS = function(mpid) {
275275
if (
276-
mpInstance._Store.getNoTargeting() ||
276+
mpInstance._Store.getPrivacyFlag('Products') ||
277277
!mpInstance._Store.isLocalStorageAvailable
278278
) {
279279
return [];
@@ -314,7 +314,7 @@ export default function _Persistence(mpInstance) {
314314
};
315315

316316
this.getAllUserProductsFromLS = function() {
317-
if (mpInstance._Store.getNoTargeting()) {
317+
if (mpInstance._Store.getPrivacyFlag('Products')) {
318318
var currentUser = mpInstance.Identity.getCurrentUser();
319319
var mpid = currentUser ? currentUser.getMPID() : null;
320320
var result = {};
@@ -358,14 +358,14 @@ export default function _Persistence(mpInstance) {
358358
: [],
359359
};
360360

361-
if (mpInstance._Store.getNoTargeting()) {
361+
if (mpInstance._Store.getPrivacyFlag('Products')) {
362362
return;
363363
}
364364
if (mpid) {
365365
allLocalStorageProducts = allLocalStorageProducts || {};
366366
allLocalStorageProducts[mpid] = currentUserProducts;
367367
try {
368-
if (!mpInstance._Store.getNoTargeting()) {
368+
if (!mpInstance._Store.getPrivacyFlag('Products')) {
369369
window.localStorage.setItem(
370370
encodeURIComponent(mpInstance._Store.prodStorageName),
371371
Base64.encode(JSON.stringify(allLocalStorageProducts))
@@ -915,7 +915,7 @@ export default function _Persistence(mpInstance) {
915915
};
916916

917917
this.getCartProducts = function(mpid) {
918-
if (mpInstance._Store.getNoTargeting()) {
918+
if (mpInstance._Store.getPrivacyFlag('Products')) {
919919
return [];
920920
}
921921
var allCartProducts,
@@ -938,8 +938,8 @@ export default function _Persistence(mpInstance) {
938938

939939
this.setCartProducts = function(allProducts) {
940940
if (
941-
!mpInstance._Store.isLocalStorageAvailable &&
942-
mpInstance._Store.getNoTargeting()
941+
!mpInstance._Store.isLocalStorageAvailable ||
942+
mpInstance._Store.getPrivacyFlag('Products')
943943
) {
944944
return;
945945
}

test/jest/persistence.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@ import {
33
MPConfig,
44
testMPID,
55
} from '../src/config/constants';
6+
import { Product } from '@mparticle/web-sdk';
67

78
describe('Products Persistence', () => {
89
let mpInstance = mParticle.getInstance();
910
beforeEach(() => {
1011
mParticle._resetForTests(MPConfig);
1112
});
1213

13-
it('should update Products localStorage when noTargeting is false by default', () => {
14+
it('should save products to localStorage when noTargeting is false by default', () => {
1415
const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 };
15-
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any);
16+
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]);
1617
expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1);
1718
});
1819

19-
it('should NOT update Products localStorage when noTargeting is true', () => {
20+
it('should NOT save products to localStorage when noTargeting is true', () => {
2021
mpInstance._Store.setNoTargeting(true);
2122
const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 };
22-
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any);
23+
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]);
2324
expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(0);
24-
expect(mpInstance._Persistence.getCartProducts(testMPID)).toEqual([]);
2525
});
2626

27-
it('should update Products localStorage when noTargeting is false', () => {
27+
it('should save products to localStorage when noTargeting is false', () => {
2828
mpInstance._Store.setNoTargeting(false);
2929
const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 };
30-
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any);
30+
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]);
3131
expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1);
3232
});
3333
});

test/src/tests-persistence.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ describe('persistence', () => {
16661666
mParticle._resetForTests(MPConfig);
16671667
});
16681668

1669-
it('should not save products LS when noTargeting is true', async () => {
1669+
it('should NOT save products to LS when noTargeting is true', async () => {
16701670
mParticle.config.noTargeting = true;
16711671

16721672
mParticle.init(apiKey, mParticle.config);
@@ -1675,7 +1675,7 @@ describe('persistence', () => {
16751675
expect(localStorageProducts).to.not.be.ok;
16761676
});
16771677

1678-
it('should save products LS when noTargeting is false', async () => {
1678+
it('should save products to LS when noTargeting is false', async () => {
16791679
mParticle.config.noTargeting = false;
16801680

16811681
mParticle.init(apiKey, mParticle.config);
@@ -1692,7 +1692,7 @@ describe('persistence', () => {
16921692
expect(localStorageProducts).to.be.ok;
16931693
});
16941694

1695-
it('should save products LS when noTargeting is false by default', async () => {
1695+
it('should save products to LS when noTargeting is false by default', async () => {
16961696
mParticle.init(apiKey, mParticle.config);
16971697
await waitForCondition(hasIdentifyReturned);
16981698
const iphone = mParticle.eCommerce.createProduct(

0 commit comments

Comments
 (0)