Skip to content

Please add the all property to the result object of renderHook() #1153

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

Closed
uttk-dev opened this issue Nov 21, 2022 · 1 comment
Closed

Please add the all property to the result object of renderHook() #1153

uttk-dev opened this issue Nov 21, 2022 · 1 comment

Comments

@uttk-dev
Copy link

Describe the feature you'd like:

In @testing-library/react-hooks, the result object of renderHook() has an all property from which you can get the histories of the Hooks return value ( see docs ), which this library does not seem to have. I would like to see the all property added as it makes testing easier.

Suggested implementation:

  • The histories of returned values of Hooks can be obtained from the all property of the result object of renderHooks().

Describe alternatives you've considered:

none yet.

Teachability, Documentation, Adoption, Migration Strategy:

It is easier to verify the state in an asynchronous function.

export const useData = () => {
  const [data, setData] = useState<AnyData | null>(null);
  const [isLoading, setIsLoading] = useState(false);

  const getAnyData = async () => {
    setIsLoading(true);
    const fetchedData = await fetchAnyData();  
    setIsLoading(false);
    setData(fetchedData);
  } 

  return { data, isLoading,  getAnyData }
}

test("isLoading is changed from `true` to `false`", () => {
  const { result } = renderHook(() => useData);

  await act(() => result.current.getAnyData());

  expect(result.all[0].isLoading).toBe(false); // default value
  expect(result.all[1].isLoading).toBe(true);  // start fetching
  expect(result.all[2].isLoading).toBe(false); // end fetching
})
@uttk-dev
Copy link
Author

Closed because I found a way to deal with it.

ref: https://github.com/testing-library/react-hooks-testing-library/blob/chore/migration-guide/MIGRATION_GUIDE.md#resultall

The new renderHook APIs in React Testing Library and React Native Testing Library have not included result.all as it was deemed to promote testing implementation details. Tests that rely on result.all should be rewritten to just use result.current and/or waitFor with more emphasis on testing the value that will be observed by users and not the intermediate values in between observable results.

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

No branches or pull requests

1 participant