-
Notifications
You must be signed in to change notification settings - Fork 91
Integration tests & bug fixes #61
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8af656a
failing test for options change no resending query
bmullan91 a8035e6
fixes #60
bmullan91 aa66aa5
integration tests
bmullan91 ac74879
fixes #37
bmullan91 be0ddc0
re-add react-testing-library as a dev-dep
bmullan91 b6fcd3d
testHook -> renderHook
bmullan91 8e9beb5
re-add cleanup afterEach setup
bmullan91 7d42227
use beforeEach + jest.fn in integration tests
bmullan91 d8d1817
add test for SSR cacheHit render count
bmullan91 2519897
use allOpts in useEffect check
bmullan91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
integration tests
- Loading branch information
commit aa66aa55c45fa44a691be64733e8710ca122506d
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import React from 'react'; | ||
| import { render, waitForElement } from 'react-testing-library'; | ||
| import { GraphQLClient, ClientContext, useQuery } from '../../src'; | ||
|
|
||
| const getWrapper = client => props => ( | ||
| <ClientContext.Provider value={client}> | ||
| {props.children} | ||
| </ClientContext.Provider> | ||
| ); | ||
|
|
||
| const TestComponent = ({ query = '{ hello }', options }) => { | ||
| const { loading, error, data } = useQuery(query, options); | ||
|
|
||
| return ( | ||
| <div> | ||
| {loading && <div data-testid="loading">Loading...</div>} | ||
| {error && <div data-testid="error">Error</div>} | ||
| {data && <div data-testid="data">{data}</div>} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| describe('useQuery Integrations', () => { | ||
| it('should reset data if the query changes', async () => { | ||
| const client = new GraphQLClient({ | ||
| url: '/graphql' | ||
| }); | ||
|
|
||
| // stub the request | ||
| client.request = () => Promise.resolve({ data: 'data v1' }); | ||
|
|
||
| const wrapper = getWrapper(client); | ||
|
|
||
| let dataNode; | ||
| const { rerender, getByTestId } = render( | ||
| <TestComponent query={'{ hello }'} />, | ||
| { | ||
| wrapper | ||
| } | ||
| ); | ||
|
|
||
| // first render | ||
| // loading -> data | ||
| expect(getByTestId('loading')).toBeTruthy(); | ||
| dataNode = await waitForElement(() => getByTestId('data')); | ||
| expect(dataNode.textContent).toBe('data v1'); | ||
| expect(() => getByTestId('loading')).toThrow(); | ||
|
|
||
| // second render | ||
| // new query, the data should be null from previous query | ||
| client.request = () => Promise.resolve({ data: 'data v2' }); | ||
| rerender(<TestComponent query={'{ goodbye }'} />, { wrapper }); | ||
|
|
||
| expect(getByTestId('loading')).toBeTruthy(); | ||
| expect(() => getByTestId('data')).toThrow(); | ||
| dataNode = await waitForElement(() => getByTestId('data')); | ||
| expect(dataNode.textContent).toBe('data v2'); | ||
| expect(() => getByTestId('loading')).toThrow(); | ||
| }); | ||
|
|
||
| it('should reset state when options.variables change', async () => { | ||
| const client = new GraphQLClient({ | ||
| url: '/graphql' | ||
| }); | ||
|
|
||
| // stub the request | ||
| client.request = () => Promise.resolve({ data: 'data v1' }); | ||
bmullan91 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const wrapper = getWrapper(client); | ||
bmullan91 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let dataNode; | ||
| let options = { variables: { a: 'a' } }; | ||
| const { rerender, getByTestId } = render( | ||
| <TestComponent options={options} />, | ||
| { | ||
| wrapper | ||
| } | ||
| ); | ||
|
|
||
| // first render | ||
| // loading -> data | ||
| expect(getByTestId('loading')).toBeTruthy(); | ||
| dataNode = await waitForElement(() => getByTestId('data')); | ||
| expect(dataNode.textContent).toBe('data v1'); | ||
| expect(() => getByTestId('loading')).toThrow(); | ||
|
|
||
| // second render | ||
| // new variables, the data should be null from previous query | ||
| client.request = () => Promise.resolve({ data: 'data v2' }); | ||
| options = { variables: { a: 'b' } }; | ||
|
|
||
| rerender(<TestComponent options={options} />, { wrapper }); | ||
|
|
||
| expect(getByTestId('loading')).toBeTruthy(); | ||
| expect(() => getByTestId('data')).toThrow(); | ||
| dataNode = await waitForElement(() => getByTestId('data')); | ||
| expect(dataNode.textContent).toBe('data v2'); | ||
| expect(() => getByTestId('loading')).toThrow(); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.