From 7e0a87e3dfddbb0bb97ab9422d9f2775bf3a7bd5 Mon Sep 17 00:00:00 2001 From: Pooya Jaferian Date: Mon, 24 Oct 2022 11:31:48 -0700 Subject: [PATCH] minor fixes --- packages/browser/src/core/analytics/index.ts | 11 +++--- .../__tests__/extension-flushing.test.ts | 38 +++++++++++-------- packages/browser/src/core/user/index.ts | 8 +++- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/packages/browser/src/core/analytics/index.ts b/packages/browser/src/core/analytics/index.ts index 5702578fd..0c3579351 100644 --- a/packages/browser/src/core/analytics/index.ts +++ b/packages/browser/src/core/analytics/index.ts @@ -119,6 +119,12 @@ export class Analytics this.settings.timeout = this.settings.timeout ?? 300 this.queue = queue ?? createDefaultQueue(options?.retryQueue, disablePersistance) + + this._universalStorage = UniversalStorage.getUniversalStorage( + disablePersistance !== false, + cookieOptions + ) + this._user = user ?? new User( @@ -135,11 +141,6 @@ export class Analytics : options?.group, cookieOptions ).load() - - this._universalStorage = UniversalStorage.getUniversalStorage( - !disablePersistance, - cookieOptions - ) this.eventFactory = new EventFactory(this._user) this.integrations = options?.integrations ?? {} this.options = options ?? {} diff --git a/packages/browser/src/core/queue/__tests__/extension-flushing.test.ts b/packages/browser/src/core/queue/__tests__/extension-flushing.test.ts index bae1ddf73..1dd7bbed1 100644 --- a/packages/browser/src/core/queue/__tests__/extension-flushing.test.ts +++ b/packages/browser/src/core/queue/__tests__/extension-flushing.test.ts @@ -4,6 +4,7 @@ import { PriorityQueue } from '../../../lib/priority-queue' import { Context } from '../../context' import { Plugin } from '../../plugin' import { EventQueue } from '../event-queue' +import { UniversalStorage } from '../../user' const fruitBasket = new Context({ type: 'track', @@ -24,6 +25,7 @@ const testPlugin: Plugin = { } const ajs = {} as Analytics +const storage = {} as UniversalStorage describe('Registration', () => { test('can register plugins', async () => { @@ -39,9 +41,9 @@ describe('Registration', () => { } const ctx = Context.system() - await eq.register(ctx, plugin, ajs) + await eq.register(ctx, plugin, ajs, storage) - expect(load).toHaveBeenCalledWith(ctx, ajs) + expect(load).toHaveBeenCalledWith(ctx, ajs, { storage }) }) test('fails if plugin cant be loaded', async () => { @@ -57,7 +59,7 @@ describe('Registration', () => { const ctx = Context.system() await expect( - eq.register(ctx, plugin, ajs) + eq.register(ctx, plugin, ajs, storage) ).rejects.toThrowErrorMatchingInlineSnapshot(`"👻"`) }) @@ -73,7 +75,7 @@ describe('Registration', () => { } const ctx = Context.system() - await eq.register(ctx, plugin, ajs) + await eq.register(ctx, plugin, ajs, storage) expect(ctx.logs()[0].level).toEqual('warn') expect(ctx.logs()[0].message).toEqual('Failed to load destination') @@ -93,7 +95,8 @@ describe('Plugin flushing', () => { ...testPlugin, type: 'before', }, - ajs + ajs, + storage ) const flushed = await eq.dispatch(fruitBasket) @@ -109,7 +112,8 @@ describe('Plugin flushing', () => { throw new Error('aaay') }, }, - ajs + ajs, + storage ) const failedFlush: Context = await eq @@ -137,7 +141,8 @@ describe('Plugin flushing', () => { throw new Error('aaay') }, }, - ajs + ajs, + storage ) const flushed = await eq.dispatch( @@ -168,8 +173,8 @@ describe('Plugin flushing', () => { type: 'destination', } - await eq.register(Context.system(), amplitude, ajs) - await eq.register(Context.system(), fullstory, ajs) + await eq.register(Context.system(), amplitude, ajs, storage) + await eq.register(Context.system(), fullstory, ajs, storage) const flushed = await eq.dispatch( new Context({ @@ -234,8 +239,8 @@ describe('Plugin flushing', () => { type: 'after', } - await eq.register(Context.system(), afterFailed, ajs) - await eq.register(Context.system(), after, ajs) + await eq.register(Context.system(), afterFailed, ajs, storage) + await eq.register(Context.system(), after, ajs, storage) const flushed = await eq.dispatch( new Context({ @@ -295,7 +300,8 @@ describe('Plugin flushing', () => { return ctx }, }, - ajs + ajs, + storage ) await eq.register( @@ -309,7 +315,8 @@ describe('Plugin flushing', () => { return ctx }, }, - ajs + ajs, + storage ) await eq.register( @@ -323,7 +330,8 @@ describe('Plugin flushing', () => { return ctx }, }, - ajs + ajs, + storage ) const flushed = await eq.dispatch( @@ -392,7 +400,7 @@ describe('Plugin flushing', () => { // shuffle plugins so we can verify order const plugins = shuffle([before, enrichment, enrichmentTwo, destination]) for (const xt of plugins) { - await eq.register(Context.system(), xt, ajs) + await eq.register(Context.system(), xt, ajs, storage) } await eq.dispatch( diff --git a/packages/browser/src/core/user/index.ts b/packages/browser/src/core/user/index.ts index fab4c4067..68c4a543a 100644 --- a/packages/browser/src/core/user/index.ts +++ b/packages/browser/src/core/user/index.ts @@ -459,8 +459,12 @@ const groupDefaults: UserOptions = { } export class Group extends User { - constructor(options: UserOptions = groupDefaults, cookie?: CookieOptions) { - super(options, cookie) + constructor( + options: UserOptions = groupDefaults, + cookie?: CookieOptions, + store?: UniversalStorage + ) { + super(options, cookie, store) autoBind(this) }