Skip to content

Commit 68f9d23

Browse files
refactor: Add test cases for setting isLoggedIn value via persistence (#872)
1 parent 6b83adb commit 68f9d23

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/persistence.interfaces.ts

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export interface IGlobalStoreV2MinifiedKeys {
4444
export interface IPersistenceMinified extends Dictionary {
4545
cu: MPID; // Current User MPID
4646
gs: IGlobalStoreV2MinifiedKeys;
47+
48+
// Stored as 0 or 1 in device persistence but returned as a
49+
// boolean when decoding from device persistence via
50+
// _Persistence.getPersistence and _Persistence.decodePersistence
4751
l: boolean; // IsLoggedIn
4852

4953
// Persistence Minified can also store optional dictionaries with

test/src/tests-persistence.ts

+67
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
IPersistenceMinified,
2121
} from '../../src/persistence.interfaces';
2222
import { ConsentState } from '@mparticle/web-sdk';
23+
import { MParticleUser } from '../../src/sdkRuntimeModels';
2324

2425
const {
2526
findCookie,
@@ -1483,6 +1484,72 @@ describe('persistence', () => {
14831484
done();
14841485
});
14851486

1487+
it('get/set isLoggedIn for localStorage', done => {
1488+
mParticle._resetForTests(MPConfig);
1489+
1490+
mockServer.respondWith(urls.login, [
1491+
200,
1492+
{},
1493+
JSON.stringify({ mpid: 'mpid1', is_logged_in: true }),
1494+
]);
1495+
1496+
1497+
mParticle.init(apiKey, mParticle.config);
1498+
let user: MParticleUser = mParticle
1499+
.getInstance()
1500+
.Identity.getCurrentUser()
1501+
expect(user).to.be.ok;
1502+
expect(user.isLoggedIn()).to.be.false;
1503+
1504+
let localStorageData = mParticle.getInstance()._Persistence.getPersistence();
1505+
1506+
// The `l` property of Persistence is a boolean, but when saved
1507+
// to local storage, Persistence encodes this as a 0 or 1.
1508+
// It is then re-encoded as a boolean when retrieved from local storage.
1509+
expect(localStorageData.l).to.equal(false);
1510+
1511+
mParticle.Identity.login();
1512+
1513+
localStorageData = mParticle.getInstance()._Persistence.getPersistence();
1514+
expect(localStorageData.l).to.equal(true);
1515+
1516+
done();
1517+
});
1518+
1519+
1520+
it('get/set isLoggedIn for cookies', done => {
1521+
mParticle._resetForTests(MPConfig);
1522+
mParticle.config.useCookieStorage = true;
1523+
1524+
mockServer.respondWith(urls.login, [
1525+
200,
1526+
{},
1527+
JSON.stringify({ mpid: 'mpid1', is_logged_in: true }),
1528+
]);
1529+
1530+
mParticle.init(apiKey, mParticle.config);
1531+
1532+
let user: MParticleUser = mParticle
1533+
.getInstance()
1534+
.Identity.getCurrentUser()
1535+
expect(user).to.be.ok;
1536+
expect(user.isLoggedIn()).to.be.false;
1537+
1538+
let cookieData = findCookie();
1539+
1540+
// The `l` property of Persistence is a boolean, but when saved
1541+
// to cookie storage, Persistence encodes this as a 0 or 1.
1542+
// It is then re-encoded as a boolean when retrieved from cookies storage
1543+
cookieData.l.should.equal(false);
1544+
1545+
mParticle.Identity.login();
1546+
1547+
cookieData = findCookie();
1548+
cookieData.l.should.equal(true);
1549+
1550+
done();
1551+
});
1552+
14861553
it('get/set consent state for single user', done => {
14871554
mParticle._resetForTests(MPConfig);
14881555

0 commit comments

Comments
 (0)