Skip to content

Commit aeeb3bd

Browse files
Jaissica HoraJaissica Hora
authored andcommitted
test: updated tests for batchUploader
1 parent 662dd95 commit aeeb3bd

4 files changed

Lines changed: 65 additions & 30 deletions

File tree

src/batchUploader.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class BatchUploader {
4343
batchingEnabled: boolean;
4444
private eventVault: SessionStorageVault<SDKEvent[]>;
4545
private batchVault: LocalStorageVault<Batch[]>;
46-
private offlineStorageEnabled: boolean = false;
46+
private OfflineStorage: boolean = false;
4747
private uploader: AsyncUploader;
4848
private lastASTEventTime: number = 0;
4949
private readonly AST_DEBOUNCE_MS: number = 1000; // 1 second debounce
@@ -72,11 +72,11 @@ export class BatchUploader {
7272

7373
// Cache Offline Storage Availability boolean
7474
// so that we don't have to check it every time
75-
this.offlineStorageEnabled =
75+
this.OfflineStorage =
7676
this.isOfflineStorageAvailable() &&
7777
!mpInstance._Store.getPrivacyFlagForStorage('Events');
7878

79-
if (this.offlineStorageEnabled) {
79+
if (this.OfflineStorage) {
8080
this.eventVault = new SessionStorageVault<SDKEvent[]>(
8181
`${mpInstance._Store.storageName}-events`,
8282
{
@@ -259,7 +259,7 @@ export class BatchUploader {
259259
const { verbose } = this.mpInstance.Logger;
260260

261261
this.eventsQueuedForProcessing.push(event);
262-
if (this.offlineStorageEnabled && this.eventVault) {
262+
if (this.OfflineStorage && this.eventVault) {
263263
this.eventVault.store(this.eventsQueuedForProcessing);
264264
}
265265

@@ -375,7 +375,7 @@ export class BatchUploader {
375375
const currentEvents: SDKEvent[] = this.eventsQueuedForProcessing;
376376

377377
this.eventsQueuedForProcessing = [];
378-
if (this.offlineStorageEnabled && this.eventVault) {
378+
if (this.OfflineStorage && this.eventVault) {
379379
this.eventVault.store([]);
380380
}
381381

@@ -389,7 +389,7 @@ export class BatchUploader {
389389
}
390390

391391
// Top Load any older Batches from Offline Storage so they go out first
392-
if (this.offlineStorageEnabled && this.batchVault) {
392+
if (this.OfflineStorage && this.batchVault) {
393393
this.batchesQueuedForProcessing.unshift(
394394
...this.batchVault.retrieve()
395395
);
@@ -422,7 +422,7 @@ export class BatchUploader {
422422
}
423423

424424
// Update Offline Storage with current state of batch queue
425-
if (!useBeacon && this.offlineStorageEnabled && this.batchVault) {
425+
if (!useBeacon && this.OfflineStorage && this.batchVault) {
426426
// Note: since beacon is "Fire and forget" it will empty `batchesThatDidNotUplod`
427427
// regardless of whether the batches were successfully uploaded or not. We should
428428
// therefore NOT overwrite Offline Storage when beacon returns, so that we can retry

src/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ export const HTTP_SERVER_ERROR = 500 as const;
219219
// Privacy dependency map for storage keys
220220
export type PrivacyControl = 'functional' | 'targeting';
221221

222-
export const StorageTypes = ['UserData', 'Products', 'Events', 'Batches', 'IdCache', 'TimeOnSite'];
222+
export const StorageTypes = ['SDKState', 'Products', 'Events', 'Batches', 'IdCache', 'TimeOnSite'];
223223

224224
export const StoragePrivacyMap: Record<typeof StorageTypes[number], PrivacyControl> = {
225-
UserData : 'functional',
225+
SDKState : 'functional',
226226
Products: 'targeting',
227227
Events: 'functional',
228228
Batches: 'functional',

test/jest/batchUploader.spec.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ describe('BatchUploader', () => {
1212
now: now,
1313
advanceTimers: true // This improves the performance of nested timers, equivalent to Sinon's shouldAdvanceTime
1414
});
15-
15+
global.fetch = jest.fn().mockResolvedValue({ ok: true, status: 200 });
16+
1617
// Create a mock mParticle instance with mocked methods for instantiating a BatchUploader
1718
mockMPInstance = {
1819
_Store: {
@@ -36,11 +37,13 @@ describe('BatchUploader', () => {
3637
},
3738
_Helpers: {
3839
getFeatureFlag: jest.fn().mockReturnValue('100'),
39-
createServiceUrl: jest.fn().mockReturnValue('https://mock-url.com')
40+
createServiceUrl: jest.fn().mockReturnValue('https://mock-url.com'),
41+
generateUniqueId: jest.fn().mockReturnValue('req-1'),
4042
},
4143
Identity: {
4244
getCurrentUser: jest.fn().mockReturnValue({
43-
getMPID: () => 'test-mpid'
45+
getMPID: () => 'test-mpid',
46+
getConsentState: jest.fn().mockReturnValue(null),
4447
})
4548
},
4649
Logger: {
@@ -55,6 +58,7 @@ describe('BatchUploader', () => {
5558

5659
afterEach(() => {
5760
jest.useRealTimers();
61+
delete global.fetch;
5862
});
5963

6064
describe('shouldDebounceAST', () => {
@@ -135,29 +139,39 @@ describe('BatchUploader', () => {
135139
mockMPInstance._Store.noFunctional = true;
136140

137141
const uploader = new BatchUploader(mockMPInstance, 1000);
138-
expect(uploader['offlineStorageEnabled']).toBe(false);
142+
expect(uploader['OfflineStorage']).toBe(false);
139143

140144
uploader.queueEvent({ EventDataType: 4 } as any);
141145
expect(sessionStorage.getItem('mprtcl-v4_abcdef-events')).toBeNull();
142146
expect(localStorage.getItem('mprtcl-v4_abcdef-batches')).toBeNull();
143147
});
144148

145-
it('should enable offline storage when noFunctional is default (false)', () => {
149+
it('should enable offline storage when noFunctional is default (false)', async () => {
146150
const uploader = new BatchUploader(mockMPInstance, 1000);
147151

148-
expect(uploader['offlineStorageEnabled']).toBe(true);
152+
expect(uploader['OfflineStorage']).toBe(true);
149153

150-
uploader.queueEvent({ EventDataType: 4 } as any);
154+
uploader.queueEvent({ EventDataType: 4, SessionId: 's1' } as any);
151155
expect(sessionStorage.getItem('mprtcl-v4_abcdef-events')).not.toBeNull();
156+
157+
jest.advanceTimersByTime(1000);
158+
await Promise.resolve();
159+
160+
expect(localStorage.getItem('mprtcl-v4_abcdef-batches')).not.toBeNull();
152161
});
153162

154-
it('should enable offline storage when noFunctional is false', () => {
163+
it('should enable offline storage when noFunctional is false', async () => {
155164
mockMPInstance._Store.noFunctional = false;
156165

157166
const uploader = new BatchUploader(mockMPInstance, 1000);
158167

159-
uploader.queueEvent({ EventDataType: 4 } as any);
168+
uploader.queueEvent({ EventDataType: 4, SessionId: 's1' } as any);
160169
expect(sessionStorage.getItem('mprtcl-v4_abcdef-events')).not.toBeNull();
170+
171+
jest.advanceTimersByTime(1000);
172+
await Promise.resolve();
173+
174+
expect(localStorage.getItem('mprtcl-v4_abcdef-batches')).not.toBeNull();
161175
});
162176
});
163177
});

test/src/tests-batchUploader.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,9 +1721,11 @@ describe('batch uploader', () => {
17211721
describe('noFunctional', () => {
17221722
const eventStorageKey = 'mprtcl-v4_abcdef-events';
17231723
const batchStorageKey = 'mprtcl-v4_abcdef-batches';
1724-
1725-
it('should write session storage when noFunctional is default (false)', async () => {
1724+
beforeEach(() => {
17261725
window.mParticle._resetForTests(MPConfig);
1726+
});
1727+
1728+
it('should store events in session storage when noFunctional is default (false)', async () => {
17271729
window.mParticle.config.flags = {
17281730
offlineStorage: '100',
17291731
...enableBatchingConfigFlags,
@@ -1736,12 +1738,15 @@ describe('batch uploader', () => {
17361738
expect(!!window.sessionStorage.getItem(eventStorageKey)).to.equal(true);
17371739
});
17381740

1739-
it('should NOT write session storage when noFunctional is true', async () => {
1740-
window.mParticle._resetForTests({ ...MPConfig, noFunctional: true });
1741+
it('should NOT store events in session storage when noFunctional is true', async () => {
17411742
window.mParticle.config.flags = {
17421743
offlineStorage: '100',
17431744
...enableBatchingConfigFlags,
17441745
};
1746+
window.mParticle.config.launcherOptions = {
1747+
...(window.mParticle.config.launcherOptions || {}),
1748+
noFunctional: true,
1749+
};
17451750
window.mParticle.init(apiKey, window.mParticle.config);
17461751
await waitForCondition(hasIdentifyReturned);
17471752
const mpInstance = window.mParticle.getInstance();
@@ -1751,12 +1756,15 @@ describe('batch uploader', () => {
17511756
expect(window.sessionStorage.getItem(eventStorageKey) === '').to.equal(true);
17521757
});
17531758

1754-
it('should write session storage when noFunctional is false', async () => {
1755-
window.mParticle._resetForTests({ ...MPConfig, noFunctional: false });
1759+
it('should store events in session storage when noFunctional is false', async () => {
17561760
window.mParticle.config.flags = {
17571761
offlineStorage: '100',
17581762
...enableBatchingConfigFlags,
17591763
};
1764+
window.mParticle.config.launcherOptions = {
1765+
...(window.mParticle.config.launcherOptions || {}),
1766+
noFunctional: false,
1767+
};
17601768
window.mParticle.init(apiKey, window.mParticle.config);
17611769
await waitForCondition(hasIdentifyReturned);
17621770
const mpInstance = window.mParticle.getInstance();
@@ -1765,8 +1773,7 @@ describe('batch uploader', () => {
17651773
expect(!!window.sessionStorage.getItem(eventStorageKey)).to.equal(true);
17661774
});
17671775

1768-
it('should write local storage when noFunctional is default (false)', async () => {
1769-
window.mParticle._resetForTests(MPConfig);
1776+
it('should store batches in local storage when noFunctional is default (false)', async () => {
17701777
window.mParticle.init(apiKey, window.mParticle.config);
17711778
await waitForCondition(hasIdentifyReturned);
17721779
const mpInstance = window.mParticle.getInstance();
@@ -1777,8 +1784,15 @@ describe('batch uploader', () => {
17771784
expect(!!window.localStorage.getItem(batchStorageKey)).to.equal(true);
17781785
});
17791786

1780-
it('should NOT write local storage when noFunctional is true', async () => {
1781-
window.mParticle._resetForTests({ ...MPConfig, noFunctional: true });
1787+
it('should NOT store batches in local storage when noFunctional is true', async () => {
1788+
window.mParticle.config.flags = {
1789+
offlineStorage: '100',
1790+
...enableBatchingConfigFlags,
1791+
};
1792+
window.mParticle.config.launcherOptions = {
1793+
...(window.mParticle.config.launcherOptions || {}),
1794+
noFunctional: true,
1795+
};
17821796
window.mParticle.init(apiKey, window.mParticle.config);
17831797
await waitForCondition(hasIdentifyReturned);
17841798
const mpInstance = window.mParticle.getInstance();
@@ -1788,8 +1802,15 @@ describe('batch uploader', () => {
17881802
expect(window.localStorage.getItem(batchStorageKey) === '').to.equal(true);
17891803
});
17901804

1791-
it('should write local storage when noFunctional is false', async () => {
1792-
window.mParticle._resetForTests({ ...MPConfig, noFunctional: false });
1805+
it('should store batches in local storage when noFunctional is false', async () => {
1806+
window.mParticle.config.flags = {
1807+
offlineStorage: '100',
1808+
...enableBatchingConfigFlags,
1809+
};
1810+
window.mParticle.config.launcherOptions = {
1811+
...(window.mParticle.config.launcherOptions || {}),
1812+
noFunctional: false,
1813+
};
17931814
window.mParticle.init(apiKey, window.mParticle.config);
17941815
await waitForCondition(hasIdentifyReturned);
17951816
const mpInstance = window.mParticle.getInstance();

0 commit comments

Comments
 (0)