Skip to content

Commit

Permalink
refactor: Add test cases for setting isLoggedIn value via persistence (
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle committed Apr 25, 2024
1 parent 6b83adb commit 68f9d23
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/persistence.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export interface IGlobalStoreV2MinifiedKeys {
export interface IPersistenceMinified extends Dictionary {
cu: MPID; // Current User MPID
gs: IGlobalStoreV2MinifiedKeys;

// Stored as 0 or 1 in device persistence but returned as a
// boolean when decoding from device persistence via
// _Persistence.getPersistence and _Persistence.decodePersistence
l: boolean; // IsLoggedIn

// Persistence Minified can also store optional dictionaries with
Expand Down
67 changes: 67 additions & 0 deletions test/src/tests-persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
IPersistenceMinified,
} from '../../src/persistence.interfaces';
import { ConsentState } from '@mparticle/web-sdk';
import { MParticleUser } from '../../src/sdkRuntimeModels';

const {
findCookie,
Expand Down Expand Up @@ -1483,6 +1484,72 @@ describe('persistence', () => {
done();
});

it('get/set isLoggedIn for localStorage', done => {
mParticle._resetForTests(MPConfig);

mockServer.respondWith(urls.login, [
200,
{},
JSON.stringify({ mpid: 'mpid1', is_logged_in: true }),
]);


mParticle.init(apiKey, mParticle.config);
let user: MParticleUser = mParticle
.getInstance()
.Identity.getCurrentUser()
expect(user).to.be.ok;
expect(user.isLoggedIn()).to.be.false;

let localStorageData = mParticle.getInstance()._Persistence.getPersistence();

// The `l` property of Persistence is a boolean, but when saved
// to local storage, Persistence encodes this as a 0 or 1.
// It is then re-encoded as a boolean when retrieved from local storage.
expect(localStorageData.l).to.equal(false);

mParticle.Identity.login();

localStorageData = mParticle.getInstance()._Persistence.getPersistence();
expect(localStorageData.l).to.equal(true);

done();
});


it('get/set isLoggedIn for cookies', done => {
mParticle._resetForTests(MPConfig);
mParticle.config.useCookieStorage = true;

mockServer.respondWith(urls.login, [
200,
{},
JSON.stringify({ mpid: 'mpid1', is_logged_in: true }),
]);

mParticle.init(apiKey, mParticle.config);

let user: MParticleUser = mParticle
.getInstance()
.Identity.getCurrentUser()
expect(user).to.be.ok;
expect(user.isLoggedIn()).to.be.false;

let cookieData = findCookie();

// The `l` property of Persistence is a boolean, but when saved
// to cookie storage, Persistence encodes this as a 0 or 1.
// It is then re-encoded as a boolean when retrieved from cookies storage
cookieData.l.should.equal(false);

mParticle.Identity.login();

cookieData = findCookie();
cookieData.l.should.equal(true);

done();
});

it('get/set consent state for single user', done => {
mParticle._resetForTests(MPConfig);

Expand Down

0 comments on commit 68f9d23

Please sign in to comment.