This strategy prevents dispatched actions from executing and instead records them. Use this to verify that a thunk dispatches the correct actions with the correct payloads without triggering side effects from those actions.
To use this, set mockActions: true in the createStore configuration. You can then inspect the recorded actions using store.getMockedActions().
import { createStore } from 'easy-peasy';
// ... model definition ...
test('fetchById', async () => {
// arrange
const todo = { id: 1, text: 'Test my store' };
const mockTodosService = {
fetchById: jest.fn(() => Promise.resolve(todo)),
};
const store = createStore(todosModel, {
injections: { todosService: mockTodosService },
mockActions: true,
});
// act
await store.getActions().fetchById(todo.id);
// assert
expect(mockTodosService.fetchById).toHaveBeenCalledWith(todo.id);
expect(store.getMockedActions()).toEqual([
{ type: '@thunk.fetchById(start)', payload: todo.id },
{ type: '@action.fetchedTodo', payload: todo },
{ type: '@thunk.fetchById(success)', payload: todo.id },
{ type: '@thunk.fetchById', payload: todo.id },
]);
});