-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Release testHook
and act
support
#280
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* This is the recommended way to test reusable custom react hooks. | ||
* It is not however recommended to use the testHook utility to test | ||
* single-use custom hooks. Typically those are better tested by testing | ||
* the component that is using it. | ||
*/ | ||
import {testHook, act, 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) | ||
act(() => { | ||
increment() | ||
}) | ||
expect(count).toBe(2) | ||
}) | ||
|
||
test('provides an `decrement` function', () => { | ||
let count, decrement | ||
testHook(() => ({count, decrement} = useCounter({step: 2}))) | ||
|
||
expect(count).toBe(0) | ||
act(() => { | ||
decrement() | ||
}) | ||
expect(count).toBe(-2) | ||
}) | ||
|
||
test('accepts a default initial value for `step`', () => { | ||
let count, increment | ||
testHook(() => ({count, increment} = useCounter({}))) | ||
|
||
expect(count).toBe(0) | ||
act(() => { | ||
increment() | ||
}) | ||
expect(count).toBe(1) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {useState} from 'react' | ||
|
||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'jest-dom/extend-expect' | ||
import React from 'react' | ||
import {render, cleanup, fireEvent} from '../' | ||
|
||
afterEach(cleanup) | ||
|
||
test('render calls useEffect immediately', () => { | ||
const effectCb = jest.fn() | ||
function MyUselessComponent() { | ||
React.useEffect(effectCb) | ||
return null | ||
} | ||
render(<MyUselessComponent />) | ||
expect(effectCb).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('fireEvent triggers useEffect calls', () => { | ||
const effectCb = jest.fn() | ||
function Counter() { | ||
React.useEffect(effectCb) | ||
const [count, setCount] = React.useState(0) | ||
return <button onClick={() => setCount(count + 1)}>{count}</button> | ||
} | ||
const { | ||
container: {firstChild: buttonNode}, | ||
} = render(<Counter />) | ||
|
||
effectCb.mockClear() | ||
fireEvent.click(buttonNode) | ||
expect(buttonNode).toHaveTextContent('1') | ||
expect(effectCb).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('calls to hydrate will run useEffects', () => { | ||
const effectCb = jest.fn() | ||
function MyUselessComponent() { | ||
React.useEffect(effectCb) | ||
return null | ||
} | ||
render(<MyUselessComponent />, {hydrate: true}) | ||
expect(effectCb).toHaveBeenCalledTimes(1) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {act} from '..' | ||
|
||
jest.mock('react-dom/test-utils', () => ({})) | ||
|
||
test('act works even when there is no act from test utils', () => { | ||
const callback = jest.fn() | ||
act(callback) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(callback).toHaveBeenCalledWith(/* nothing */) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {act as reactAct} from 'react-dom/test-utils' | ||
|
||
// act is supported [email protected] | ||
// and is only needed for versions higher than that | ||
// so we do nothing for versions that don't support act. | ||
const act = reactAct || (cb => cb()) | ||
|
||
function rtlAct(...args) { | ||
return act(...args) | ||
} | ||
|
||
export default rtlAct |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can actually polyfill it for older versions of react
you won't get the warnings, but something's better than nothing I suppose.