Skip to content

Add act to update call to support hooks #137

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 3 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,4 @@ expect(submitButtons).toHaveLength(3); // expect 3 elements

## `act`

Useful function to help testing components that use hooks API. By default any `render` and `fireEvent` calls are wrapped by this function, so there is no need to wrap it manually. This method is re-exported from [`react-test-renderer`](https://github.com/facebook/react/blob/master/packages/react-test-renderer/src/ReactTestRenderer.js#L567]).
Useful function to help testing components that use hooks API. By default any `render`, `update`, and `fireEvent` calls are wrapped by this function, so there is no need to wrap it manually. This method is re-exported from [`react-test-renderer`](https://github.com/facebook/react/blob/master/packages/react-test-renderer/src/ReactTestRenderer.js#L567]).
8 changes: 8 additions & 0 deletions src/__tests__/act.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ test('render should trigger useEffect', () => {
expect(effectCallback).toHaveBeenCalledTimes(1);
});

test('update should trigger useEffect', () => {
const effectCallback = jest.fn();
const { update } = render(<UseEffect callback={effectCallback} />);
update(<UseEffect callback={effectCallback} />);

expect(effectCallback).toHaveBeenCalledTimes(2);
});

test('fireEvent should trigger useState', () => {
const { getByTestId } = render(<Counter />);
const counter = getByTestId('counter');
Expand Down
10 changes: 9 additions & 1 deletion src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function render(
return {
...getByAPI(instance),
...queryByAPI(instance),
update: renderer.update,
update: updateWithAct(renderer),
unmount: renderer.unmount,
toJSON: renderer.toJSON,
debug: debug(instance, renderer),
Expand All @@ -46,6 +46,14 @@ function renderWithAct(
return ((renderer: any): ReactTestRenderer);
}

function updateWithAct(renderer: ReactTestRenderer) {
return function(component: React.Element<any>) {
act(() => {
renderer.update(component);
});
};
}

function debug(instance: ReactTestInstance, renderer) {
function debugImpl(message?: string) {
return debugDeep(renderer.toJSON(), message);
Expand Down