From 5568ab7a343fe7e001d403df489c6c5e3a4a669c Mon Sep 17 00:00:00 2001 From: Ritik Rawat Date: Sun, 11 Oct 2020 12:49:21 +0530 Subject: [PATCH 1/2] Create ecosystem-react-hooks-testing-library.md ecosystem page for react-hooks-testing-library --- docs/ecosystem-react-hooks-testing-library.md | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 docs/ecosystem-react-hooks-testing-library.md diff --git a/docs/ecosystem-react-hooks-testing-library.md b/docs/ecosystem-react-hooks-testing-library.md new file mode 100644 index 000000000..006be758d --- /dev/null +++ b/docs/ecosystem-react-hooks-testing-library.md @@ -0,0 +1,45 @@ +--- +id: ecosystem-react-hooks-testing-library +title: react-hooks-testing-library +--- + +The [`react-hooks-testing-library`](https://github.com/testing-library/react-hooks-testing-library) allows you to create a simple test harness for React hooks that handles running them within the body of a function component, as well as providing various useful utility functions for updating the inputs and retrieving the outputs of your amazing custom hook. This library aims to provide a testing experience as close as possible to natively using your hook from within a real component. + +Using this library, you do not have to concern yourself with how to construct, render or interact with the react component in order to test your hook. You can just use the hook directly and assert the results. + +``` +npm install --save-dev @testing-library/react-hooks +``` +Imagine we have a simple hook that we want to test: + +```js +import { useState, useCallback } from 'react' + +export default function useCounter() { + const [count, setCount] = useState(0) + const increment = useCallback(() => setCount((x) => x + 1), []) + return { count, increment } +} +``` + +To test `useCounter` we need to render it using the `renderHook` function provided by +`react-hooks-testing-library`: + +```js +import { renderHook } from '@testing-library/react-hooks' +import useCounter from './useCounter' + +test('should use counter', () => { + const { result } = renderHook(() => useCounter()) + + expect(result.current.count).toBe(0) + expect(typeof result.current.increment).toBe('function') +}) +``` + +As you can see, the result's current value matches what is returned by our hook. + + +[react-hooks-testing-library github](https://github.com/testing-library/react-hooks-testing-library) + +[react-hooks-testing-library docs](https://react-hooks-testing-library.com/) From 2c987622adbbdd4b2bde7f04bac03e5c844a0552 Mon Sep 17 00:00:00 2001 From: Ritik Rawat Date: Sun, 11 Oct 2020 12:51:22 +0530 Subject: [PATCH 2/2] add ecosystem link for react-hooks-testing-library --- website/sidebars.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/website/sidebars.json b/website/sidebars.json index 93240c586..83ac98b24 100755 --- a/website/sidebars.json +++ b/website/sidebars.json @@ -116,7 +116,8 @@ "ecosystem-riot-testing-library", "ecosystem-jasmine-dom", "ecosystem-query-extensions", - "ecosystem-rtl-simple-queries" + "ecosystem-rtl-simple-queries", + "ecosystem-react-hooks-testing-library" ], "Help": ["faq", "learning", "contributing"] },