Guest User

Untitled

a guest
May 25th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. In this assignment, we are going to look at how to test applications that use
  2. Redux Thunk to handle asynchronous requests.
  3.  
  4. ### Async Actions
  5. Testing asynchronous action creators is a little more complicated than their
  6. synchronous counterparts. Take a look at the test for the fetchBoard action creator:
  7.  
  8. describe('fetchBoard', () => {
  9. it('Should dispatch fetchBoardSuccess', () => {
  10. const board = {
  11. lists: []
  12. };
  13.  
  14. global.fetch = jest.fn().mockImplementation(() =>
  15. Promise.resolve({
  16. ok: true,
  17. json() {
  18. return board;
  19. }
  20. })
  21. );
  22.  
  23. const dispatch = jest.fn();
  24. return fetchBoard()(dispatch).then(() => {
  25. expect(fetch).toHaveBeenCalledWith('/board');
  26. expect(dispatch).toHaveBeenCalledWith(fetchBoardSuccess(board));
  27. });
  28. });
  29. });
  30.  
  31. We use a technique called mocking.
  32. Mocking is simulating the behavior of part of our system so that we can
  33. remove that piece of the system from our tests.
  34.  
  35. Mocking helps to simplify your tests when it would be too complicated to
  36. work with the entire system. In this case we mock the fetch function:
  37.  
  38. global.fetch = jest.fn().mockImplementation(() =>
  39. Promise.resolve({
  40. ok: true,
  41. json() {
  42. return board;
  43. }
  44. })
  45. );
  46.  
  47. ### Dispatching async actions
  48. Testing for the dispatch of an async action is again a little more tricky
  49. than when we're dispatching a sync action.
  50.  
  51. This is because it's hard to identify an async action just by looking at
  52. the function returned.
Add Comment
Please, Sign In to add comment