/* eslint-disable prefer-reflect */ import { testSaga } from 'redux-saga-test-plan'; import { createDoc, readDoc, updateDoc, deleteDoc, crudSaga } from 'sagas/crud'; import { apiRequest } from 'sagas/api'; import { DOC_CREATED, DOC_READ, DOC_UPDATED, DOC_DELETED } from 'constants/actions'; import { docCreated, docCreateResponded, docRead, docReadResponded, docUpdated, docUpdateResponded, docDeleted, docDeleteResponded } from 'actions/crud'; describe('crudSaga', () => { describe('createDoc', () => { const route = 'employees'; const pendingId = ''; const fields = { name: 'John Doe', email: 'john.doe@mubaloo.com' }; const action = docCreated(route, pendingId, fields); it('should call the API with a POST request', () => { const response = { isResponse: true }; testSaga(createDoc, action) .next() .call(apiRequest, 'post', route, [], fields) .next({ response, err: null }) .put(docCreateResponded(route, pendingId, null, response)) .next() .isDone(); }); it('should handle errors', () => { const err = new Error('something bad happened'); testSaga(createDoc, action) .next() .call(apiRequest, 'post', route, [], fields) .next({ response: null, err }) .put(docCreateResponded(route, pendingId, err, null)) .next() .isDone(); }); }); describe('readDoc', () => { it('should call the API with a GET request', () => { const route = 'employees'; const response = { isResponse: true }; testSaga(readDoc, docRead(route)) .next() .call(apiRequest, 'get', route) .next({ response, err: null }) .put(docReadResponded(route, null, response)) .next() .isDone(); }); }); describe('updateDoc', () => { it('should call the API with a PUT request', () => { const route = 'employees'; const actualId = ''; const fields = { name: 'Jack Doe' }; const response = { isResponse: true }; testSaga(updateDoc, docUpdated(route, actualId, fields)) .next() .call(apiRequest, 'put', route, [actualId], fields) .next({ response, err: null }) .put(docUpdateResponded(route, actualId, null, response)) .next() .isDone(); }); }); describe('deleteDoc', () => { it('should call the API with a DELETE request', () => { const route = 'employees'; const actualId = ''; const response = { isResponse: true }; testSaga(deleteDoc, docDeleted(route, actualId)) .next() .call(apiRequest, 'delete', route, [actualId]) .next({ response, err: null }) .put(docDeleteResponded(route, actualId, null)) .next() .isDone(); }); }); it('should fork listeners for CRUD actions', () => { testSaga(crudSaga) .next() .takeEveryEffect(DOC_CREATED, createDoc) .next() .takeEveryEffect(DOC_READ, readDoc) .next() .takeEveryEffect(DOC_UPDATED, updateDoc) .next() .takeEveryEffect(DOC_DELETED, deleteDoc) .next() .isDone(); }); });