I have an action called addItemsToCollection that calls the API call like this:
import {CALL_API} from 'redux-api-middleware'; export function addItemsToCollection(collectionId, itemIds) { return {[CALL_API]: { endpoint: `/collections/${collectionId}/items`, method: 'POST', body: JSON.stringify(itemIds), types: [ ADD_ITEMS, { type: ADD_ITEMS_SUCCESS }, ADD_ITEMS_FAIL ] } }; }
When I try to write a test just to create this action, for example:
it('should create an action to add items to a collection', () => { const collectionId = 1; const itemIds = [1,2,3]; const expectedAction = { [CALL_API]: { endpoint: `/collections/${collectionId}/items`, method: 'POST', body: JSON.stringify(itemIds), types: [ ADD_ITEMS, { type: ADD_ITEMS_SUCCESS }, ADD_ITEMS_FAIL ] } }; expect(addItemsToCollection(collectionId, itemIds)).toEqual(expectedAction); });
It passes, but this is because it returns an empty {} object for expectAction, as well as from the function of the creator of the action. Not much use.
So my questions are:
Why are empty objects returned?
What is the correct way to conduct this test?
source share