Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In this assignment, we are going to look at how to test applications that use
- Redux Thunk to handle asynchronous requests.
- ### Async Actions
- Testing asynchronous action creators is a little more complicated than their
- synchronous counterparts. Take a look at the test for the fetchBoard action creator:
- describe('fetchBoard', () => {
- it('Should dispatch fetchBoardSuccess', () => {
- const board = {
- lists: []
- };
- global.fetch = jest.fn().mockImplementation(() =>
- Promise.resolve({
- ok: true,
- json() {
- return board;
- }
- })
- );
- const dispatch = jest.fn();
- return fetchBoard()(dispatch).then(() => {
- expect(fetch).toHaveBeenCalledWith('/board');
- expect(dispatch).toHaveBeenCalledWith(fetchBoardSuccess(board));
- });
- });
- });
- We use a technique called mocking.
- Mocking is simulating the behavior of part of our system so that we can
- remove that piece of the system from our tests.
- Mocking helps to simplify your tests when it would be too complicated to
- work with the entire system. In this case we mock the fetch function:
- global.fetch = jest.fn().mockImplementation(() =>
- Promise.resolve({
- ok: true,
- json() {
- return board;
- }
- })
- );
- ### Dispatching async actions
- Testing for the dispatch of an async action is again a little more tricky
- than when we're dispatching a sync action.
- This is because it's hard to identify an async action just by looking at
- the function returned.
Add Comment
Please, Sign In to add comment