I have a redux thunk action that invokes an asynchronous callback service then dispatches additional actions based on the result:
import { UPDATE_TODOS } from '../../todos/actions/update-todos';
import { UPDATE_SUMMARY } from '../../todos/actions/update-summary';
import { newTodo } from '../../todos/services/fake-backend';
export function addTodo() {
return (dispatch, getState) => {
newTodo((err, todo) => {
dispatch({ type: UPDATE_TODOS, todo });
dispatch({ type: UPDATE_SUMMARY });
});
};
}
Emulating the latest docs I can write a test that is promise-based, but I am unsure how to make it callback-based:
import { expect } from 'jest';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { UPDATE_TODOS } from '../../../todos/actions/update-todos';
import { addTodo } from '../../../todos/actions/add-todo';
const mockStore = configureMockStore([thunk]);
describe('addTodo', () => {
it('creates new todo and dispatches UPDATE_TODOS', (done) => {
const expectedActions = [
{ type: UPDATE_TODOS, todo: {} }
];
const store = mockStore({ todos: [] });
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * How do you re-write this to be callback-oriented * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
return store.dispatch(addTodo()).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
})
```
Aucun commentaire:
Enregistrer un commentaire