Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Segment Github committed Aug 4, 2022
1 parent 3eeed84 commit 1b08191
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
52 changes: 52 additions & 0 deletions packages/browser/src/core/buffer/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { Analytics } from '../../analytics'
import { Context } from '../../context'
import { sleep } from '@/test-helpers/sleep'
import { User } from '../../user'

describe('PreInitMethodCallBuffer', () => {
describe('push', () => {
Expand Down Expand Up @@ -99,6 +100,57 @@ describe('AnalyticsBuffered', () => {
expect(context).toEqual(ctx)
})

it('should wrap async methods in a promise', async () => {
const ajs = new Analytics({ writeKey: 'foo' })
const ctx = new Context({ type: 'track' })

ajs.track = () => Promise.resolve(ctx)

const buffered = new AnalyticsBuffered((buffer) => {
return new Promise((resolve) =>
setTimeout(() => {
flushAnalyticsCallsInNewTask(ajs, buffer)
resolve([ajs, ctx])
}, 0)
)
})
const result = buffered.track('foo')
expect(result).toBeInstanceOf(Promise)

await buffered

const result2 = buffered.track('bar')
expect(result2).toBeInstanceOf(Promise)

expect(await result).toBeInstanceOf(Context)
expect(await result2).toBeInstanceOf(Context)
})

it('should wrap synchronous methods in a promise', async () => {
const ajs = new Analytics({ writeKey: 'foo' })
ajs.user = () => new User()
const ctx = new Context({ type: 'track' })

const buffered = new AnalyticsBuffered((buffer) => {
return new Promise((resolve) =>
setTimeout(() => {
flushAnalyticsCallsInNewTask(ajs, buffer)
resolve([ajs, ctx])
}, 0)
)
})
const result = buffered.user()
expect(result).toBeInstanceOf(Promise)

await buffered

const result2 = buffered.user()
expect(result2).toBeInstanceOf(Promise)

expect(await result).toBeInstanceOf(User)
expect(await result2).toBeInstanceOf(User)
})

describe('the "this" value of proxied analytics methods', () => {
test('should be the ajs instance for non-chainable methods (that return a promise)', async () => {
const ajs = new Analytics({ writeKey: 'foo' })
Expand Down
8 changes: 2 additions & 6 deletions packages/browser/src/test-helpers/type-assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ type NotUnknown<T> = unknown extends T ? never : T
type NotTopType<T> = NotAny<T> & NotUnknown<T>

// this is not meant to be run, just for type tests
export function assertNotAny<T>(val: NotTopType<T>) {
void val
}
export function assertNotAny<T>(_val: NotTopType<T>) {}

// this is not meant to be run, just for type tests
export function assertIs<T extends SomeType, SomeType = any>(val: T) {
void val
}
export function assertIs<T extends SomeType, SomeType = any>(_val: T) {}

0 comments on commit 1b08191

Please sign in to comment.