Skip to content

Commit b14099e

Browse files
committed
test: added jest tests and updated test content
1 parent b7230d6 commit b14099e

3 files changed

Lines changed: 43 additions & 33 deletions

File tree

src/persistence.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,10 @@ export default function _Persistence(mpInstance) {
8888

8989
// https://go.mparticle.com/work/SQDSDKS-6048
9090
try {
91-
if (mpInstance._Store.isLocalStorageAvailable) {
92-
if (mpInstance._Store.getNoTargeting()) {
93-
localStorage.removeItem(
94-
mpInstance._Store.prodStorageName
95-
);
96-
mpInstance._Store.cartProducts = [];
97-
return;
98-
}
91+
if (
92+
mpInstance._Store.isLocalStorageAvailable &&
93+
!mpInstance._Store.getNoTargeting()
94+
) {
9995
var encodedProducts = localStorage.getItem(
10096
mpInstance._Store.prodStorageName
10197
);
@@ -363,11 +359,7 @@ export default function _Persistence(mpInstance) {
363359
};
364360

365361
if (mpInstance._Store.getNoTargeting()) {
366-
try {
367-
window.localStorage.removeItem(
368-
mpInstance._Store.prodStorageName
369-
);
370-
} catch (e) {}
362+
return;
371363
}
372364
if (mpid) {
373365
allLocalStorageProducts = allLocalStorageProducts || {};
@@ -945,13 +937,10 @@ export default function _Persistence(mpInstance) {
945937
};
946938

947939
this.setCartProducts = function(allProducts) {
948-
if (!mpInstance._Store.isLocalStorageAvailable) {
949-
return;
950-
}
951-
if (mpInstance._Store.getNoTargeting()) {
952-
try {
953-
localStorage.removeItem(mpInstance._Store.prodStorageName);
954-
} catch (e) {}
940+
if (
941+
!mpInstance._Store.isLocalStorageAvailable &&
942+
mpInstance._Store.getNoTargeting()
943+
) {
955944
return;
956945
}
957946

test/jest/persistence.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {
2+
mParticle,
3+
MPConfig,
4+
testMPID,
5+
} from '../src/config/constants';
6+
7+
describe('Products Persistence', () => {
8+
let mpInstance = mParticle.getInstance();
9+
beforeEach(() => {
10+
mParticle._resetForTests(MPConfig);
11+
});
12+
13+
it('should update Products localStorage when noTargeting is false by default', () => {
14+
const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 };
15+
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any);
16+
expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1);
17+
});
18+
19+
it('should NOT update Products localStorage when noTargeting is true', () => {
20+
mpInstance._Store.setNoTargeting(true);
21+
const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 };
22+
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any);
23+
expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(0);
24+
expect(mpInstance._Persistence.getCartProducts(testMPID)).toEqual([]);
25+
});
26+
27+
it('should update Products localStorage when noTargeting is false', () => {
28+
mpInstance._Store.setNoTargeting(false);
29+
const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 };
30+
mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any);
31+
expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1);
32+
});
33+
});

test/src/tests-persistence.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,18 +1674,6 @@ describe('persistence', () => {
16741674
const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName);
16751675
expect(localStorageProducts).to.not.be.ok;
16761676
});
1677-
1678-
it('should clear existing products LS when noTargeting is true', async () => {
1679-
localStorage.setItem(
1680-
LocalStorageProductsV4WithWorkSpaceName,
1681-
btoa(JSON.stringify({ testMPID: { cp: [{ Name: 'x' }] } }))
1682-
);
1683-
mParticle.config.noTargeting = true;
1684-
mParticle.init(apiKey, mParticle.config);
1685-
await waitForCondition(hasIdentifyReturned);
1686-
const localStorageProducts = localStorage.getItem(LocalStorageProductsV4WithWorkSpaceName);
1687-
expect(localStorageProducts).to.not.be.ok;
1688-
});
16891677

16901678
it('should save products LS when noTargeting is false', async () => {
16911679
mParticle.config.noTargeting = false;
@@ -1704,7 +1692,7 @@ describe('persistence', () => {
17041692
expect(localStorageProducts).to.be.ok;
17051693
});
17061694

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

0 commit comments

Comments
 (0)