Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pooyaj committed Oct 24, 2022
1 parent e0aca81 commit 7e0a87e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
11 changes: 6 additions & 5 deletions packages/browser/src/core/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 ?? {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -24,6 +25,7 @@ const testPlugin: Plugin = {
}

const ajs = {} as Analytics
const storage = {} as UniversalStorage

describe('Registration', () => {
test('can register plugins', async () => {
Expand All @@ -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 () => {
Expand All @@ -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(`"👻"`)
})

Expand All @@ -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')
Expand All @@ -93,7 +95,8 @@ describe('Plugin flushing', () => {
...testPlugin,
type: 'before',
},
ajs
ajs,
storage
)

const flushed = await eq.dispatch(fruitBasket)
Expand All @@ -109,7 +112,8 @@ describe('Plugin flushing', () => {
throw new Error('aaay')
},
},
ajs
ajs,
storage
)

const failedFlush: Context = await eq
Expand Down Expand Up @@ -137,7 +141,8 @@ describe('Plugin flushing', () => {
throw new Error('aaay')
},
},
ajs
ajs,
storage
)

const flushed = await eq.dispatch(
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -295,7 +300,8 @@ describe('Plugin flushing', () => {
return ctx
},
},
ajs
ajs,
storage
)

await eq.register(
Expand All @@ -309,7 +315,8 @@ describe('Plugin flushing', () => {
return ctx
},
},
ajs
ajs,
storage
)

await eq.register(
Expand All @@ -323,7 +330,8 @@ describe('Plugin flushing', () => {
return ctx
},
},
ajs
ajs,
storage
)

const flushed = await eq.dispatch(
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 6 additions & 2 deletions packages/browser/src/core/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 7e0a87e

Please sign in to comment.