Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/consent/__tests__/sdkUserConsent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ test('createUserConsentAPI', () => {
const syncManager = { submitterManager: syncTaskFactory() };
const storage = {
events: { clear: jest.fn() },
impressions: { clear: jest.fn() }
impressions: { clear: jest.fn() },
impressionCounts: { clear: jest.fn() },
uniqueKeys: { clear: jest.fn() }
};

// @ts-ignore
Expand Down
5 changes: 3 additions & 2 deletions src/consent/sdkUserConsent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const ConsentStatus = {
* The public user consent API exposed via SplitFactory, used to control if the SDK tracks and sends impressions and events or not.
*/
export function createUserConsentAPI(params: ISdkFactoryContext) {
const { settings, settings: { log }, syncManager, storage: { events, impressions, impressionCounts } } = params;
const { settings, settings: { log }, syncManager, storage: { events, impressions, impressionCounts, uniqueKeys } } = params;

if (!isConsentGranted(settings)) log.info(USER_CONSENT_INITIAL, [settings.userConsent]);

Expand All @@ -41,7 +41,8 @@ export function createUserConsentAPI(params: ISdkFactoryContext) {
// @ts-ignore, clear method is present in storage for standalone and partial consumer mode
if (events.clear) events.clear(); // @ts-ignore
if (impressions.clear) impressions.clear(); // @ts-ignore
if (impressionCounts && impressionCounts.clear) impressionCounts.clear();
if (impressionCounts.clear) impressionCounts.clear(); // @ts-ignore
if (uniqueKeys.clear) uniqueKeys.clear();
}
} else {
log.info(USER_CONSENT_NOT_UPDATED, [newConsentStatus]);
Expand Down
32 changes: 19 additions & 13 deletions src/listeners/__tests__/browser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BrowserSignalListener } from '../browser';
import { IEventsCacheSync, IImpressionCountsCacheSync, IImpressionsCacheSync, IStorageSync, ITelemetryCacheSync, IUniqueKeysCacheBase } from '../../storages/types';
import { ISplitApi } from '../../services/types';
import { fullSettings } from '../../utils/settingsValidation/__tests__/settings.mocks';

Expand Down Expand Up @@ -30,42 +29,48 @@ const fakeUniqueKeys = {
};

// Storage with impressionsCount and telemetry cache
const fakeStorageOptimized = { // @ts-expect-error
const fakeStorageOptimized = {
impressions: {
isEmpty: jest.fn(),
pop() {
return [fakeImpression];
}
} as IImpressionsCacheSync, // @ts-expect-error
},
events: {
isEmpty: jest.fn(),
pop() {
return [fakeEvent];
}
} as IEventsCacheSync, // @ts-expect-error
},
impressionCounts: {
isEmpty: jest.fn(),
pop() {
return fakeImpressionCounts;
}
} as IImpressionCountsCacheSync, // @ts-expect-error
},
uniqueKeys: {
isEmpty: jest.fn(),
pop() {
return fakeUniqueKeys;
}
} as IUniqueKeysCacheBase, // @ts-expect-error
},
telemetry: {
isEmpty: jest.fn(),
pop() {
return 'fake telemetry';
}
} as ITelemetryCacheSync
}
};

const fakeStorageDebug = {
impressions: fakeStorageOptimized.impressions,
events: fakeStorageOptimized.events
events: fakeStorageOptimized.events,
impressionCounts: {
isEmpty: jest.fn(() => true)
},
uniqueKeys: {
isEmpty: jest.fn(() => true)
}
};

// @ts-expect-error
Expand Down Expand Up @@ -155,7 +160,8 @@ function assertStop(listener: BrowserSignalListener) {

test('Browser JS listener / consumer mode', () => {
// No SyncManager ==> consumer mode
const listener = new BrowserSignalListener(undefined, fullSettings, fakeStorageOptimized as IStorageSync, fakeSplitApi);
// @ts-expect-error
const listener = new BrowserSignalListener(undefined, fullSettings, fakeStorageOptimized, fakeSplitApi);

listener.start();
assertStart(listener);
Expand All @@ -180,7 +186,7 @@ test('Browser JS listener / standalone mode / Impressions optimized mode with te
const syncManagerMock = {};

// @ts-expect-error
const listener = new BrowserSignalListener(syncManagerMock, fullSettings, fakeStorageOptimized as IStorageSync, fakeSplitApi);
const listener = new BrowserSignalListener(syncManagerMock, fullSettings, fakeStorageOptimized, fakeSplitApi);

listener.start();
assertStart(listener);
Expand All @@ -205,7 +211,7 @@ test('Browser JS listener / standalone mode / Impressions debug mode', () => {
const syncManagerMock = {};

// @ts-expect-error
const listener = new BrowserSignalListener(syncManagerMock, fullSettings, fakeStorageDebug as IStorageSync, fakeSplitApi);
const listener = new BrowserSignalListener(syncManagerMock, fullSettings, fakeStorageDebug, fakeSplitApi);

listener.start();
assertStart(listener);
Expand Down Expand Up @@ -234,7 +240,7 @@ test('Browser JS listener / standalone mode / Impressions debug mode', () => {
test('Browser JS listener / standalone mode / Fallback to regular Fetch transport', () => {

function runBrowserListener() { // @ts-expect-error
const listener = new BrowserSignalListener({}, fullSettings, fakeStorageDebug as IStorageSync, fakeSplitApi);
const listener = new BrowserSignalListener({}, fullSettings, fakeStorageDebug, fakeSplitApi);
listener.start();
// Trigger data flush
triggerEvent(VISIBILITYCHANGE_EVENT, 'hidden');
Expand Down Expand Up @@ -270,7 +276,7 @@ test('Browser JS listener / standalone mode / user consent status', () => {
const settings = { ...fullSettings };

// @ts-expect-error
const listener = new BrowserSignalListener(syncManagerMock, settings, fakeStorageOptimized as IStorageSync, fakeSplitApi);
const listener = new BrowserSignalListener(syncManagerMock, settings, fakeStorageOptimized, fakeSplitApi);

listener.start();

Expand Down
5 changes: 2 additions & 3 deletions src/listeners/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ export class BrowserSignalListener implements ISignalListener {

this._flushData(events + '/testImpressions/beacon', this.storage.impressions, this.serviceApi.postTestImpressionsBulk, this.fromImpressionsCollector, extraMetadata);
this._flushData(events + '/events/beacon', this.storage.events, this.serviceApi.postEventsBulk);
if (this.storage.impressionCounts) this._flushData(events + '/testImpressions/count/beacon', this.storage.impressionCounts, this.serviceApi.postTestImpressionsCount, fromImpressionCountsCollector);
// @ts-ignore
if (this.storage.uniqueKeys) this._flushData(telemetry + '/v1/keys/cs/beacon', this.storage.uniqueKeys, this.serviceApi.postUniqueKeysBulkCs);
this._flushData(events + '/testImpressions/count/beacon', this.storage.impressionCounts, this.serviceApi.postTestImpressionsCount, fromImpressionCountsCollector);
this._flushData(telemetry + '/v1/keys/cs/beacon', this.storage.uniqueKeys, this.serviceApi.postUniqueKeysBulkCs);
}

// Flush telemetry data
Expand Down