-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Hooks Testing 🎣 #274
Changes from all commits
9066eff
4febf70
04b5ff6
691b45c
95992b1
5b2ef64
c686462
2adfed0
b2d78bf
83e2ef5
92b0c38
8ae6804
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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.
Show resolved
Hide resolved
|
||
|
||
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.
Show resolved
Hide resolved
|
||
}) |
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 | ||
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? 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.