From a3b65898b2a09a46cc438ec5b2962b0887c5065e Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Fri, 30 Jun 2023 14:42:27 -0500 Subject: [PATCH] Seth - example test --- packages/node/src/__tests__/callback.test.ts | 23 ++++------- .../src/__tests__/disable.integration.test.ts | 2 +- .../src/__tests__/emitter.integration.test.ts | 29 ++++++-------- packages/node/src/__tests__/plugins.test.ts | 7 ---- .../test-helpers/create-test-analytics.ts | 11 ++++- .../src/__tests__/test-helpers/factories.ts | 40 ------------------- .../test-helpers/test-fetch-client.ts | 19 +++++++++ 7 files changed, 49 insertions(+), 82 deletions(-) create mode 100644 packages/node/src/__tests__/test-helpers/test-fetch-client.ts diff --git a/packages/node/src/__tests__/callback.test.ts b/packages/node/src/__tests__/callback.test.ts index 52a74f8fd..e0fea6054 100644 --- a/packages/node/src/__tests__/callback.test.ts +++ b/packages/node/src/__tests__/callback.test.ts @@ -1,21 +1,13 @@ -import { - createError, - createSuccess, - TestFetchClient, -} from './test-helpers/factories' import { createTestAnalytics } from './test-helpers/create-test-analytics' import { Context } from '../app/context' +import { TestFetchClient } from './test-helpers/test-fetch-client' describe('Callback behavior', () => { - const testClient = new TestFetchClient() - beforeEach(() => { - testClient.returnValue = createSuccess() - }) + beforeEach(() => {}) it('should handle success', async () => { const ajs = createTestAnalytics({ maxEventsInBatch: 1, - customClient: testClient, }) const ctx = await new Promise((resolve, reject) => ajs.track( @@ -34,11 +26,12 @@ describe('Callback behavior', () => { }) it('should handle errors', async () => { - testClient.returnValue = createError() - const ajs = createTestAnalytics({ - maxEventsInBatch: 1, - customClient: testClient, - }) + const ajs = createTestAnalytics( + { + maxEventsInBatch: 1, + }, + { withError: true } + ) const [err, ctx] = await new Promise<[any, Context]>((resolve) => ajs.track( { diff --git a/packages/node/src/__tests__/disable.integration.test.ts b/packages/node/src/__tests__/disable.integration.test.ts index 7a3adc29f..50cb01cac 100644 --- a/packages/node/src/__tests__/disable.integration.test.ts +++ b/packages/node/src/__tests__/disable.integration.test.ts @@ -2,7 +2,7 @@ import { createTestAnalytics } from './test-helpers/create-test-analytics' import { createSuccess } from './test-helpers/factories' describe('disable', () => { - const mockSend: jest.Mock = jest.fn().mockResolvedValue(createSuccess()) + const mockSend = jest.fn().mockResolvedValue(createSuccess()) const checkFetchClient = { send: mockSend, diff --git a/packages/node/src/__tests__/emitter.integration.test.ts b/packages/node/src/__tests__/emitter.integration.test.ts index 25ed8cbfa..3f86e59ac 100644 --- a/packages/node/src/__tests__/emitter.integration.test.ts +++ b/packages/node/src/__tests__/emitter.integration.test.ts @@ -1,15 +1,8 @@ -import { - createError, - createSuccess, - TestFetchClient, -} from './test-helpers/factories' import { createTestAnalytics } from './test-helpers/create-test-analytics' import { assertHttpRequestEmittedEvent } from './test-helpers/assert-shape' describe('http_request', () => { - const testClient = new TestFetchClient() it('emits an http_request event if success', async () => { - testClient.returnValue = createSuccess() const analytics = createTestAnalytics() const fn = jest.fn() analytics.on('http_request', fn) @@ -21,11 +14,12 @@ describe('http_request', () => { }) it('emits an http_request event if error', async () => { - testClient.returnValue = createError() - const analytics = createTestAnalytics({ - maxRetries: 0, - customClient: testClient, - }) + const analytics = createTestAnalytics( + { + maxRetries: 0, + }, + { withError: true } + ) const fn = jest.fn() analytics.on('http_request', fn) await new Promise((resolve) => @@ -35,11 +29,12 @@ describe('http_request', () => { }) it('if error, emits an http_request event on every retry', async () => { - testClient.returnValue = createError() - const analytics = createTestAnalytics({ - maxRetries: 2, - customClient: testClient, - }) + const analytics = createTestAnalytics( + { + maxRetries: 2, + }, + { withError: true } + ) const fn = jest.fn() analytics.on('http_request', fn) await new Promise((resolve) => diff --git a/packages/node/src/__tests__/plugins.test.ts b/packages/node/src/__tests__/plugins.test.ts index a653c49fd..18a3ef8c9 100644 --- a/packages/node/src/__tests__/plugins.test.ts +++ b/packages/node/src/__tests__/plugins.test.ts @@ -1,13 +1,6 @@ -import { TestFetchClient } from './test-helpers/factories' import { createTestAnalytics } from './test-helpers/create-test-analytics' -const testClient = new TestFetchClient() - describe('Plugins', () => { - beforeEach(() => { - testClient.reset() - }) - describe('Initialize', () => { it('loads analytics-node-next plugin', async () => { const analytics = createTestAnalytics() diff --git a/packages/node/src/__tests__/test-helpers/create-test-analytics.ts b/packages/node/src/__tests__/test-helpers/create-test-analytics.ts index e97e35f5a..94e272392 100644 --- a/packages/node/src/__tests__/test-helpers/create-test-analytics.ts +++ b/packages/node/src/__tests__/test-helpers/create-test-analytics.ts @@ -1,8 +1,15 @@ import { Analytics } from '../../app/analytics-node' import { AnalyticsSettings } from '../../app/settings' +import { TestFetchClient, TestFetchClientOptions } from './test-fetch-client' export const createTestAnalytics = ( - settings: Partial = {} + settings: Partial = {}, + { withError }: TestFetchClientOptions = {} ) => { - return new Analytics({ writeKey: 'foo', flushInterval: 100, ...settings }) + return new Analytics({ + writeKey: 'foo', + flushInterval: 100, + customClient: new TestFetchClient({ withError }), + ...settings, + }) } diff --git a/packages/node/src/__tests__/test-helpers/factories.ts b/packages/node/src/__tests__/test-helpers/factories.ts index 72249f8a3..b2c670bf0 100644 --- a/packages/node/src/__tests__/test-helpers/factories.ts +++ b/packages/node/src/__tests__/test-helpers/factories.ts @@ -1,5 +1,3 @@ -import { CustomHTTPClient } from '../../lib/customhttpclient' - export const createSuccess = (body?: any) => { return Promise.resolve({ json: () => Promise.resolve(body), @@ -17,41 +15,3 @@ export const createError = (overrides: Partial = {}) => { ...overrides, }) as Promise } - -export class TestFetchClient implements CustomHTTPClient { - public callCount = 0 - - public calls = [] - get lastCall() { - return this.calls.slice(-1)[0] - } - - private _returnValue: any - set returnValue(value: any) { - this._returnValue = value - } - - private _errorValue: any - set errorValue(value: any) { - this._errorValue = value - } - - public reset() { - this.callCount = 0 - this.calls = [] - this._returnValue = null - this._errorValue = null - } - - send = async (_resource: any, _options: any): Promise => { - this.calls.push([_resource, _options]) - this.callCount++ - if (this._errorValue) { - throw this._errorValue - } - if (this._returnValue) { - return this._returnValue - } - return createSuccess() - } -} diff --git a/packages/node/src/__tests__/test-helpers/test-fetch-client.ts b/packages/node/src/__tests__/test-helpers/test-fetch-client.ts new file mode 100644 index 000000000..886fce5eb --- /dev/null +++ b/packages/node/src/__tests__/test-helpers/test-fetch-client.ts @@ -0,0 +1,19 @@ +import { CustomHTTPClient } from '../../lib/customhttpclient' +import { createError, createSuccess } from './factories' + +export type TestFetchClientOptions = { + withError?: boolean +} +/** + * Test client. + * Try not to use this directly -- use createTestAnalytics instead. + */ +export class TestFetchClient implements CustomHTTPClient { + private throwError: boolean + constructor({ withError }: TestFetchClientOptions = {}) { + this.throwError = withError || false + } + send() { + return Promise.resolve(this.throwError ? createError() : createSuccess()) + } +}