| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- /* 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 = '<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 = '<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 = '<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();
- });
- });
|