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

Unit tests for GraphQLClient #15

Merged
merged 4 commits into from
Feb 18, 2019
Merged

Unit tests for GraphQLClient #15

merged 4 commits into from
Feb 18, 2019

Conversation

jackdclark
Copy link
Member

@jackdclark jackdclark commented Feb 18, 2019

Part of #9

Add test coverage for GraphQLClient

  GraphQLClient
    when instantiated
      ✓ throws if no url provided (10ms)
      ✓ throws if fetch is not a function (1ms)
      ✓ throws if fetch is not present or polyfilled (1ms)
      ✓ assigns config.cache to an instance property
      ✓ assigns config.headers to an instance property
      ✓ assigns config.ssrMode to an instance property
      ✓ assigns config.url to an instance property (1ms)
      ✓ assigns config.fetch to an instance property
      ✓ assigns config.fetchOptions to an instance property
      ✓ assigns config.logErrors to an instance property (1ms)
      ✓ assigns config.onError to an instance property
    setHeader
      ✓ sets the key to the value (1ms)
    setHeaders
      ✓ replaces all headers
    logErrorResult
      ✓ calls onError if present (3ms)
      ✓ logs a fetchError (2ms)
      ✓ logs an httpError
      ✓ logs all graphQLErrors (1ms)
    generateResult
      ✓ shows as errored if there are graphQL errors (1ms)
      ✓ shows as errored if there is a fetch error
      ✓ shows as errored if there is an http error
      ✓ returns the errors & data (1ms)
    getCacheKey
      ✓ returns a cache key
    request
      ✓ sends the request to the configured url (3ms)
      ✓ applies the configured headers
      ✓ sends the provided operation query, variables and name (1ms)
      ✓ handles & returns fetch errors (1ms)
      ✓ handles & returns http errors (1ms)
      ✓ returns valid responses
      ✓ returns graphql errors (1ms)
      ✓ will use a configured fetch implementation

bmullan91
bmullan91 previously approved these changes Feb 18, 2019
Copy link
Contributor

@bmullan91 bmullan91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - Fantastic start @jackdclark 👍

});

it('assigns config.logErrors to an instance property', () => {
const logErrors = jest.fn();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: logErrors should be a Boolean

const client = new GraphQLClient({ ...validConfig });
fetch.mockResponseOnce(JSON.stringify({ data: 'data' }));
await client.request({ query: TEST_QUERY });
expect(fetch.mock.calls[0][0]).toBe(validConfig.url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Sometimes it's useful to set actual and expected explicitly, making it a bit clearer to understand whats being tested:

const actual = fetch.mock.calls[0][0]
const expected = validConfig.url
expect(actual).toBe(expected)

Copy link
Contributor

@bmullan91 bmullan91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@jackdclark jackdclark merged commit 33b714d into master Feb 18, 2019
@jackdclark jackdclark deleted the test-graphql-client branch February 18, 2019 14:02
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.

None yet

2 participants