Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.2k
Hooks Testing 🎣#274
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
Uh oh!
There was an error while loading. Please reload this page.
Hooks Testing 🎣 #274
Changes from all commits
9066eff4febf7004b5ff6691b45c95992b15b2ef64c6864622adfed0b2d78bf83e2ef592b0c388ae6804File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import {testHook, cleanup} from 'react-testing-library' | ||
| import useCounter from '../react-hooks' | ||
| afterEach(cleanup) | ||
| test('accepts default initial values', () => { | ||
| let count | ||
| testHook(() => ({count} = useCounter())) | ||
| expect(count).toBe(0) | ||
| }) | ||
| test('accepts a default initial value for `count`', () => { | ||
| let count | ||
| testHook(() => ({count} = useCounter({}))) | ||
| expect(count).toBe(0) | ||
| }) | ||
| test('provides an `increment` function', () => { | ||
| let count, increment | ||
| testHook(() => ({count, increment} = useCounter({step: 2}))) | ||
| expect(count).toBe(0) | ||
| increment() | ||
| expect(count).toBe(2) | ||
| }) | ||
| test('provides an `decrement` function', () => { | ||
| let count, decrement | ||
| testHook(() => ({count, decrement} = useCounter({step: 2}))) | ||
| expect(count).toBe(0) | ||
| decrement() | ||
| expect(count).toBe(-2) | ||
| }) | ||
| test('accepts a default initial value for `step`', () => { | ||
| let count, increment | ||
| testHook(() => ({count, increment} = useCounter({}))) | ||
| expect(count).toBe(0) | ||
| increment() | ||
| expect(count).toBe(1) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import {useState} from 'react' | ||
donavon marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| function useCounter({initialCount = 0, step = 1} = {}) { | ||
| const [count, setCount] = useState(initialCount) | ||
| const increment = () => setCount(c => c + step) | ||
| const decrement = () => setCount(c => c - step) | ||
| return {count, increment, decrement} | ||
| } | ||
| export default useCounter | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import {useState} from 'react' | ||
| import 'jest-dom/extend-expect' | ||
| import {testHook, cleanup} from '../' | ||
| afterEach(cleanup) | ||
| test('testHook calls the callback', () => { | ||
| const spy = jest.fn() | ||
| testHook(spy) | ||
| expect(spy).toHaveBeenCalledTimes(1) | ||
| }) | ||
| test('confirm we can safely call a React Hook from within the callback', () => { | ||
| testHook(() => useState()) | ||
alexkrolick marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -40,6 +40,11 @@ export function render<Q extends Queries>( | ||
| options: RenderOptions<Q>, | ||
| ): RenderResult<Q> | ||
| /** | ||
| * Renders a test component that calls back to the test. | ||
| */ | ||
| export function testHook(callback: () => void): void | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to do anything special to indicate that the callback will be called with the result of the custom hook (so you can get type safety there) or is TypeScript's inference good enough to know? ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the callback isn't called with anything and isn't required to return anything. i'm not a TS expert, so I'll defer, but this should work. | ||
| /** | ||
| * Unmounts React trees that were mounted with render. | ||
| */ | ||
Uh oh!
There was an error while loading. Please reload this page.