Ad

What Is The Purpose Of Redux Async Action Testing?

- 1 answer

Could someone explain why we test asynchronous actions? What are we hoping to ensure works?

So in the docs, http://rackt.org/redux/docs/recipes/WritingTests.html, we mock the server request.

nock('http://example.com/')
  .get('/todos')
  .reply(200, { todos: ['do something'] })

const expectedActions = [
  { type: types.FETCH_TODOS_REQUEST },
  { type: types.FETCH_TODOS_SUCCESS, body: { todos: ['do something']  } }
]
const store = mockStore({ todos: [] }, expectedActions, done)
store.dispatch(actions.fetchTodos())

So we aren't testing to see if we contact the server. We are testing to see if the correct sequence of actions takes place, given a response of 200 correct?

Ad

Answer

Yes, the point of this type of test isn't to see if your network requests are working correctly but to ensure that the sequence of actions that you expect to fire is actually firing. It can also be used to make sure that the data you get back from the server is the correct format your front end expects.

This may seem trivial if you write the tests after you've implemented the code, but say that down the road, you or another developer decides to make some slight tweaks to those actions or remove one entirely. Worst case scenario, your code fails silently, leaving you to wonder what went wrong. These types of tests help you avoid that kind of problem.

Ad
source: stackoverflow.com
Ad