Skip to content
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

Closed
wants to merge 6 commits into from

Conversation

MichaelGHSeg
Copy link
Contributor

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.

@changeset-bot
Copy link

changeset-bot bot commented Jun 29, 2023

⚠️ No Changeset found

Latest commit: 6cf70ee

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@silesky silesky requested a review from oscb June 30, 2023 07:24
import { createTestAnalytics } from './test-helpers/create-test-analytics'
import { CustomHTTPClient } from '../lib/customhttpclient'

export class CheckFetchClient implements CustomHTTPClient {
Copy link
Contributor

@silesky silesky Jun 30, 2023

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 {
Copy link
Contributor

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

  1. This is not a factory, so I would put it in another file maybe just called mock-http-client.
  2. 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())
}

@silesky
Copy link
Contributor

silesky commented Jul 5, 2023

Superceded by #880

@silesky silesky closed this Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants