From 1b081910af75f9e9956ec448ccc00ae98c360d8f Mon Sep 17 00:00:00 2001 From: Segment Github Date: Thu, 4 Aug 2022 16:13:01 -0500 Subject: [PATCH] add unit tests --- .../src/core/buffer/__tests__/index.test.ts | 52 +++++++++++++++++++ .../src/test-helpers/type-assertions.ts | 8 +-- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/packages/browser/src/core/buffer/__tests__/index.test.ts b/packages/browser/src/core/buffer/__tests__/index.test.ts index 6088fd1fa..04950e766 100644 --- a/packages/browser/src/core/buffer/__tests__/index.test.ts +++ b/packages/browser/src/core/buffer/__tests__/index.test.ts @@ -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', () => { @@ -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' }) diff --git a/packages/browser/src/test-helpers/type-assertions.ts b/packages/browser/src/test-helpers/type-assertions.ts index 030ccb0e9..b25a09e7e 100644 --- a/packages/browser/src/test-helpers/type-assertions.ts +++ b/packages/browser/src/test-helpers/type-assertions.ts @@ -5,11 +5,7 @@ type NotUnknown = unknown extends T ? never : T type NotTopType = NotAny & NotUnknown // this is not meant to be run, just for type tests -export function assertNotAny(val: NotTopType) { - void val -} +export function assertNotAny(_val: NotTopType) {} // this is not meant to be run, just for type tests -export function assertIs(val: T) { - void val -} +export function assertIs(_val: T) {}