/* eslint-disable prefer-reflect */ import { testSaga } from 'redux-saga-test-plan'; import axios from 'axios'; import { apiRequest, createListener, readListener, updateListener, deleteListener, crudSaga } from 'sagas/crud'; 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('apiRequest', () => { const route = 'employees'; const fields = { name: 'John Doe', email: 'john.doe@mubaloo.com' }; it('should make an API POST request', () => { const response = { isResponse: true }; testSaga(apiRequest, 'post', route, [], fields) .next() .call([axios, 'post'], '/api1/employees', fields) .next(response) .returns({ response, err: null }); }); it('should handle errors', () => { const err = new Error('something bad happened'); testSaga(apiRequest, 'post', route, [], fields) .next() .call([axios, 'post'], '/api1/employees', fields) .throw(err) .returns({ response: null, err }); }); it('should make an API GET request', () => { const response = { isResponse: true }; testSaga(apiRequest, 'get', route, ['a0b']) .next() .call([axios, 'get'], '/api1/employees/a0b') .next(response) .returns({ response, err: null }); }); }); describe('createListener', () => { const route = 'employees'; const pendingId = ''; const fields = { name: 'John Doe', email: 'john.doe@mubaloo.com' }; it('should listen to DOC_CREATED and make API calls', () => { const response = { isResponse: true }; testSaga(createListener) .next() .take(DOC_CREATED) .next(docCreated(route, pendingId, fields)) .call(apiRequest, 'post', route, [], fields) .next({ response, err: null }) .put(docCreateResponded(route, pendingId, null, response)) .next() .take(DOC_CREATED); }); it('should handle errors', () => { const err = new Error('something bad happened'); testSaga(createListener) .next() .take(DOC_CREATED) .next(docCreated(route, pendingId, fields)) .call(apiRequest, 'post', route, [], fields) .next({ response: null, err }) .put(docCreateResponded(route, pendingId, err, null)) .next() .take(DOC_CREATED); }); }); describe('readListener', () => { it('should listen to DOC_READ and make API calls', () => { const route = 'employees'; const response = { isResponse: true }; testSaga(readListener) .next() .take(DOC_READ) .next(docRead(route)) .call(apiRequest, 'get', route) .next({ response, err: null }) .put(docReadResponded(route, null, response)) .next() .take(DOC_READ); }); }); describe('updateListener', () => { const route = 'employees'; const actualId = ''; const fields = { name: 'Jack Doe' }; it('should listen to DOC_UPDATED and make API calls', () => { const response = { isResponse: true }; testSaga(updateListener) .next() .take(DOC_UPDATED) .next(docUpdated(route, actualId, fields)) .call(apiRequest, 'put', route, [actualId], fields) .next({ response, err: null }) .put(docUpdateResponded(route, actualId, null, response)) .next() .take(DOC_UPDATED); }); }); describe('deleteListener', () => { const route = 'employees'; const actualId = ''; it('should listen to DOC_DELETED and make API calls', () => { const response = { isResponse: true }; testSaga(deleteListener) .next() .take(DOC_DELETED) .next(docDeleted(route, actualId)) .call(apiRequest, 'delete', route, [actualId]) .next({ response, err: null }) .put(docDeleteResponded(route, actualId, null)) .next() .take(DOC_DELETED); }); }); it('should fork listeners for CRUD actions', () => { testSaga(crudSaga) .next() .fork(createListener) .next() .fork(readListener) .next() .fork(updateListener) .next() .fork(deleteListener) .next() .isDone(); }); });