Skip to content

Commit

Permalink
refactor: Refactor nullifySession function and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle committed Mar 29, 2024
1 parent 3ae0f68 commit 7e2eb68
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/sessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default function SessionManager(
messageType: Types.MessageType.SessionEnd,
});

nullifySession();
mpInstance._Store.nullifySession();
return;
}

Expand Down Expand Up @@ -175,7 +175,7 @@ export default function SessionManager(
});

mpInstance._Store.sessionStartDate = null;
nullifySession();
mpInstance._Store.nullifySession();
}
}
};
Expand Down Expand Up @@ -204,7 +204,7 @@ export default function SessionManager(
clearTimeout(mpInstance._Store.globalTimer);
};

this.startNewSessionIfNeeded = function(): void {
this.startNewSessionIfNeeded = function (): void {
if (!mpInstance._Store.webviewBridgeEnabled) {
const persistence = mpInstance._Persistence.getPersistence();

Expand All @@ -217,11 +217,4 @@ export default function SessionManager(
}
}
};

function nullifySession(): void {
mpInstance._Store.sessionId = null;
mpInstance._Store.dateLastEventSent = null;
mpInstance._Store.sessionAttributes = {};
mpInstance._Persistence.update();
}
}
9 changes: 9 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ export interface IStore {
integrationDelayTimeoutStart: number; // UNIX Timestamp
webviewBridgeEnabled?: boolean;
wrapperSDKInfo: WrapperSDKInfo;

nullifySession?: () => void;
}

// TODO: Merge this with SDKStoreApi in sdkRuntimeModels
Expand Down Expand Up @@ -418,6 +420,13 @@ export default function Store(
}
}
}

this.nullifySession = (): void => {
this.sessionId = null;
this.dateLastEventSent = null;
this.sessionAttributes = {};
mpInstance._Persistence.update();
};
}

export function processFlags(
Expand Down
93 changes: 93 additions & 0 deletions test/src/tests-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { MPConfig, apiKey } from './config/constants';
import Utils from './config/utils';
import { Dictionary } from '../../src/utils';
import Constants from '../../src/constants';
import { IGlobalStoreV2MinifiedKeys } from '../../src/persistence.interfaces';
const MockSideloadedKit = Utils.MockSideloadedKit;

describe('Store', () => {
Expand Down Expand Up @@ -248,6 +249,98 @@ describe('Store', () => {
});
});

describe('#nullifySessionData', () => {
it('should nullify session data', () => {
const store: IStore = new Store(
sampleConfig,
window.mParticle.getInstance()
);

store.sessionId = '123';
store.dateLastEventSent = new Date();
store.sessionAttributes = { foo: 'bar' };

store.nullifySession();

expect(store.sessionId).to.be.null;
expect(store.dateLastEventSent).to.be.null;
expect(store.sessionAttributes).to.deep.equal({});
});

it('should nullify session data in persistence', () => {
const persistenceData = {
gs: {
csm: ['mpid3'],
sid: 'abcd',
ie: true,
dt: apiKey,
cgid: 'cgid1',
das: 'das1',
les: 1234567890,
sa: { foo: 'bar' },
ss: {},
av: '1.0',
ia: {},
c: null,
ssd: 8675309,
} as IGlobalStoreV2MinifiedKeys,
cu: 'mpid3',
l: false,
mpid1: {
ua: {
gender: 'female',
age: 29,
height: '65',
color: 'blue',
id: 'abcdefghijklmnopqrstuvwxyz',
},
ui: { 1: 'customerid123', 2: 'facebookid123' },
},
mpid2: {
ua: { gender: 'male', age: 20, height: '68', color: 'red' },
ui: {
1: 'customerid234',
2: 'facebookid234',
id: 'abcdefghijklmnopqrstuvwxyz',
},
},
mpid3: {
ua: { gender: 'male', age: 20, height: '68', color: 'red' },
ui: {
1: 'customerid234',
2: 'facebookid234',
id: 'abcdefghijklmnopqrstuvwxyz',
},
},
};

window.mParticle
.getInstance()
._Persistence.savePersistence(persistenceData);

let fromPersistence = window.mParticle
.getInstance()
._Persistence.getPersistence();

expect(fromPersistence.gs).to.be.ok;
expect(fromPersistence.gs.sid).to.equal('abcd');
expect(fromPersistence.gs.les).to.equal(1234567890);
expect(fromPersistence.gs.sa).to.deep.equal({ foo: 'bar' });

// Grab the store directly from mPInstance to make sure they share scope
window.mParticle.getInstance()._Store.nullifySession();

fromPersistence = window.mParticle
.getInstance()
._Persistence.getPersistence();

expect(fromPersistence.gs).to.be.ok;
expect(fromPersistence.gs.sid).to.be.undefined;
expect(fromPersistence.gs.les).to.be.undefined;
expect(fromPersistence.gs.sa).to.be.undefined;
});
});

describe('#processFlags', () => {
it('should return an empty object if no featureFlags are passed', () => {
const flags = processFlags({} as SDKInitConfig, {} as SDKConfig);
Expand Down

0 comments on commit 7e2eb68

Please sign in to comment.