-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(act): Support ReactDOM.TestUtils.act (#278)
This also removes `flushEffects` which is no longer necessary!
- Loading branch information
Kent C. Dodds
committed
Feb 6, 2019
1 parent
d01fbc1
commit 8e08ccf
Showing
9 changed files
with
123 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
File renamed without changes.
This file contains 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 react-dom@16.8.0 | ||
// 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 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 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