-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pluggable custom http client wrapper #877
Conversation
|
import { createTestAnalytics } from './test-helpers/create-test-analytics' | ||
import { CustomHTTPClient } from '../lib/customhttpclient' | ||
|
||
export class CheckFetchClient implements CustomHTTPClient { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like where your heads at. I think instead of implementing our own custom spy logic to keep track of calls, it would be less boilerplate / mantainenance to leverage the existing jest library support for mocks/spies and all sorts of goodies. https://jestjs.io/docs/jest-object
Also, with that jest automatically clears all mock state between tests, so no need to do any reset in a beforeEach().
(See comment below -- could also slightly to tweak this to integrate a MockHttpClient class)
e.g.
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 mockHttpClient: CustomHTTPClient = {
send: mockSend,
}
it('should dispatch callbacks and emit an http request, even if disabled', async () => {
const analytics = createTestAnalytics({
disable: true,
customClient: mockHttpClient,
})
const emitterCb = jest.fn()
analytics.on('http_request', emitterCb)
await new Promise((resolve) =>
analytics.track({ anonymousId: 'foo', event: 'bar' }, resolve)
)
expect(emitterCb).toBeCalledTimes(1)
})
it('should call fetch if disabled is false', async () => {
const analytics = createTestAnalytics({
disable: false,
customClient: mockHttpClient,
})
await new Promise((resolve) =>
analytics.track({ anonymousId: 'foo', event: 'bar' }, resolve)
)
expect(mockSend).toBeCalledTimes(1)
})
it('should not call fetch if disabled is true', async () => {
const analytics = createTestAnalytics({
disable: true,
customClient: mockHttpClient,
})
await new Promise((resolve) =>
analytics.track({ anonymousId: 'foo', event: 'bar' }, resolve)
)
expect(mockSend).toBeCalledTimes(0)
})
})
@@ -15,3 +17,41 @@ export const createError = (overrides: Partial<Response> = {}) => { | |||
...overrides, | |||
}) as Promise<Response> | |||
} | |||
|
|||
export class TestFetchClient implements CustomHTTPClient { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like where your heads at in general using the mock interface
- This is not a factory, so I would put it in another file maybe just called
mock-http-client
. - I am not entirely sure this mock is actually necessary... or at least, it can be dramatically simplified -- see earlier earlier comment.
The mock client could just be this:
export class MockHttpClient implements CustomHTTPClient {
get lastCall() {
return this.send.mock.lastCall
}
get calls() {
this.send.mock.calls
}
send = jest.fn().mockResolvedValue(createSuccess())
}
Superceded by #880 |
Creating a pluggable interface to wrap fetch calls, which will allow for custom clients to be used, such as a fetch implementation that supports http proxies.