Skip to content

Commit 34cacac

Browse files
committed
update README.md
1 parent dbf08e6 commit 34cacac

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ A testing utility for React-Redux apps that provides hooks to run tests after ea
44
[![npm version](https://badge.fury.io/js/react-redux-stethoscope.svg)](https://badge.fury.io/js/react-redux-stethoscope) ![Node.js CI](https://github.com/y-code/react-redux-stethoscope/workflows/Node.js%20CI/badge.svg?branch=master)
55

66
## The problem to solve
7-
When you want to test a React component after an operation that dispatches a Redux action, you need to make it sure that before your test code goes, all the React components connected to the Redux store are thoroughly updated based on the new state. You can easily assure it with `act()` provided by [React Test Utility](https://reactjs.org/docs/test-utils.html#act) or [Testing Library for React](https://testing-library.com/docs/react-testing-library/api#act).
7+
When you want to test a React component after an operation that dispatches a Redux action, you need to make it sure that before your test code goes on, all the React components connected to the Redux store are thoroughly updated based on the new state. We can easily assure it with `act()` provided by [React Test Utility](https://reactjs.org/docs/test-utils.html#act) or [Testing Library for React](https://testing-library.com/docs/react-testing-library/api#act).
88

99
However, it cannot help when a test target operation dispatches Redux actions asynchronously, like the thunk action below.
1010

1111
```javascript
1212
export const thunkCreators = {
1313
requestMessages: () =>
1414
(dispatch, getState) => {
15-
// action dispatch before the fetch
15+
// Synchronous action dispatch
1616
dispatch(actionCreators.requestMessages())
1717

1818
return fetch('/api/inbox/messages', { method: 'GET' })
1919
.then(response => response.json())
2020
.then(json => {
21-
// action dispatch after the fetch
21+
// Asynchronous action dispatch
2222
return dispatch(actionCreators.receiveMessages(json))
2323
})
2424
},
@@ -55,20 +55,19 @@ it('display "Loading..." while fetching data.', async () => {
5555
{
5656
actionType: actionCreators.requestMessages().type,
5757
onUpdated: () => {
58-
const loadingMessage = wrapper.getAllByText('Loading...')
59-
expect(loadingMessage).toHaveLength(1)
58+
expect(wrapper.queryAllByText('Loading...')).toHaveLength(1)
6059
}
6160
},
6261
{
6362
actionType: actionCreators.receiveMessages({}).type,
6463
onUpdated: () => {
65-
const firstMessage = wrapper.getAllByText('Hello, World')
66-
expect(firstMessage).toHaveLength(1)
64+
expect(wrapper.queryByText('Loading...')).toBeNull()
65+
expect(wrapper.queryAllByText('Hello, World')).toHaveLength(1)
6766
}
6867
},
6968
]
7069
})
71-
}
70+
})
7271
```
7372

7473
`listenAsync()` function runs `act` property of its parameter object over the [`act()` of React Test Utility](https://reactjs.org/docs/test-utils.html#act), and waits for a Redux action to be dispatched. When the dispatched action matches one of the action types specified in the `targets` property, Stethoscope calls back the function in the corresponding `onUpdated` property. `listenAsync()` keeps waiting until all the action types in the `targets` are dispatched.

0 commit comments

Comments
 (0)