-
Notifications
You must be signed in to change notification settings - Fork 1.1k
No waitForNextUpdate
?
#1101
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
Comments
the next update isn't necessarily relevant to the user. We want to discourage testing these implementation in Testing Library. |
@eps1lon is right on the Here is an example of an upgrade I did (code simplified). From: import { renderHook } from '@testing-library/react-hooks';
describe(`Search users`, () => {
let store: Store;
const searchTerm = 'test';
beforeEach(() => {
store = createTestStore();
});
it(`searches for users`, async () => {
const wrapper = <ReduxProvider store={store}>{children}</ReduxProvider>
const { result, waitForNextUpdate } = renderHook(() => useSearchUsers(searchTerm), { wrapper });
expect(result.current).toMatchObject({ users: [], isFetching: true });
// OLD WAY
await waitForNextUpdate();
expect(result.current).toMatchObject({ users: [], isFetching: false });
// assert more...
});
}); To: import { renderHook, waitFor } from '@testing-library/react';
describe(`Search users`, () => {
let store: Store;
const searchTerm = 'test';
beforeEach(() => {
store = createTestStore();
});
it(`searches for users`, async () => {
const wrapper = <ReduxProvider store={store}>{children}</ReduxProvider>
const { result } = renderHook(() => useSearchUsers(searchTerm), { wrapper });
expect(result.current).toMatchObject({ users: [], isFetching: true });
// NEW WAY
await waitFor(() => expect(result.current).toMatchObject({ users: [], isFetching: false }));
// assert more...
});
}); |
This is my previous test on Apollo's useQuery using beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())
test('useQuery', async () => {
const wrapper = ({ children }) => <ApolloProvider client={createMockApolloClient()}>{children}</ApolloProvider>
const { result, waitForNextUpdate } = renderHook(
() =>
useQuery(GET_ORGANIZATION_PACKS_API, {
variables: { orgUid },
fetchPolicy: 'no-cache',
notifyOnNetworkStatusChange: true,
}),
{ wrapper },
)
let { data, loading, error, refetch, networkStatus } = result.current
expect(loading).toBeTrue()
expect(networkStatus).toBe(NetworkStatus.loading)
expect(error).toBeUndefined()
expect(data).toBeUndefined()
expect(refetch).toBeFunction()
await waitForNextUpdate({ timeout: 5000 })
;({ data, loading, error, refetch, networkStatus } = result.current)
expect(error).toBeUndefined()
expect(loading).toBeFalse()
expect(networkStatus).toBe(NetworkStatus.ready)
expect(data).toMatchSnapshot({}, 'GET_ORGANIZATION_PACKS_API')
expect(refetch).toBeFunction()
refetch().then()
await waitForNextUpdate()
const data0 = data
;({ data, loading, error, refetch, networkStatus } = result.current)
expect(error).toBeUndefined()
expect(loading).toBeTrue()
expect(networkStatus).toBe(NetworkStatus.refetch)
expect(data).toEqual(data0)
expect(refetch).toBeFunction()
await waitForNextUpdate()
;({ data, loading, error, refetch, networkStatus } = result.current)
expect(error).toBeUndefined()
expect(loading).toBeFalse()
expect(networkStatus).toBe(NetworkStatus.ready)
expect(data).toEqual(data0)
expect(refetch).toBeFunction()
})
const GET_ORGANIZATION_PACKS_API = gql`...` It was working well. Now, how do I write it w/o |
@mirismaili you can use What about trying:
That should work. |
To mock apollo queries and make it work in test
|
|
I don't see how By way of example: I've just spent several days implementing some hooks based around Tanstack Query with selectors/transformations to ensure that consumers of the hook are only reloaded when their relevant slice of the underlying data is mutated (a la useContextSelector). It's a critical performance consideration for my app. How can I test that with this library? |
Well, here's one way:
|
* Replaced `waitForNextUpdate` with `waitFor` (see testing-library/react-testing-library#1101) * Updated `courseId` in success/fail tests to prevent concurrency issues
* Replaced `waitForNextUpdate` with `waitFor` (see testing-library/react-testing-library#1101) * Updated `courseId` in success/fail tests to prevent concurrency issues
* Replaced `waitForNextUpdate` with `waitFor` (see testing-library/react-testing-library#1101) * Updated `courseId` in success/fail tests to prevent concurrency issues
* Replaced `waitForNextUpdate` with `waitFor` (see testing-library/react-testing-library#1101) * Updated `courseId` in success/fail tests to prevent concurrency issues
* Replaced `waitForNextUpdate` with `waitFor` (see testing-library/react-testing-library#1101) * Updated `courseId` in success/fail tests to prevent concurrency issues
Hi all,
I've briefly checked the docs and discord to check that and it sees that
waitForNextUpdate
is not available as return of renderHook for now in testing library.Is there a plan to implement that or testing async hooks is considered as bad practice?
If it won't be implemented maybe theres an option to inform about it in the docs, as it seems to be main feature for testing async hooks
Thank you for what you do with the lib.
The text was updated successfully, but these errors were encountered: