Skip to content

Commit

Permalink
Seth - example test
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Jul 3, 2023
1 parent 30b942f commit a3b6589
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 82 deletions.
23 changes: 8 additions & 15 deletions packages/node/src/__tests__/callback.test.ts
Original file line number Diff line number Diff line change
@@ -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<Context>((resolve, reject) =>
ajs.track(
Expand All @@ -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(
{
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/__tests__/disable.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 12 additions & 17 deletions packages/node/src/__tests__/emitter.integration.test.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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) =>
Expand All @@ -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) =>
Expand Down
7 changes: 0 additions & 7 deletions packages/node/src/__tests__/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AnalyticsSettings> = {}
settings: Partial<AnalyticsSettings> = {},
{ withError }: TestFetchClientOptions = {}
) => {
return new Analytics({ writeKey: 'foo', flushInterval: 100, ...settings })
return new Analytics({
writeKey: 'foo',
flushInterval: 100,
customClient: new TestFetchClient({ withError }),
...settings,
})
}
40 changes: 0 additions & 40 deletions packages/node/src/__tests__/test-helpers/factories.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { CustomHTTPClient } from '../../lib/customhttpclient'

export const createSuccess = (body?: any) => {
return Promise.resolve({
json: () => Promise.resolve(body),
Expand All @@ -17,41 +15,3 @@ export const createError = (overrides: Partial<Response> = {}) => {
...overrides,
}) as Promise<Response>
}

export class TestFetchClient implements CustomHTTPClient {
public callCount = 0

public calls = <any[]>[]
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<Response> => {
this.calls.push([_resource, _options])
this.callCount++
if (this._errorValue) {
throw this._errorValue
}
if (this._returnValue) {
return this._returnValue
}
return createSuccess()
}
}
19 changes: 19 additions & 0 deletions packages/node/src/__tests__/test-helpers/test-fetch-client.ts
Original file line number Diff line number Diff line change
@@ -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())
}
}

0 comments on commit a3b6589

Please sign in to comment.