crud.spec.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* eslint-disable prefer-reflect */
  2. import { testSaga } from 'redux-saga-test-plan';
  3. import {
  4. createDoc,
  5. readDoc,
  6. updateDoc,
  7. deleteDoc,
  8. crudSaga
  9. } from 'sagas/crud';
  10. import {
  11. apiRequest
  12. } from 'sagas/api';
  13. import {
  14. DOC_CREATED,
  15. DOC_READ,
  16. DOC_UPDATED,
  17. DOC_DELETED
  18. } from 'constants/actions';
  19. import {
  20. docCreated,
  21. docCreateResponded,
  22. docRead,
  23. docReadResponded,
  24. docUpdated,
  25. docUpdateResponded,
  26. docDeleted,
  27. docDeleteResponded
  28. } from 'actions/crud';
  29. describe('crudSaga', () => {
  30. describe('createDoc', () => {
  31. const route = 'employees';
  32. const pendingId = '<pendingId>';
  33. const fields = {
  34. name: 'John Doe',
  35. email: 'john.doe@mubaloo.com'
  36. };
  37. const action = docCreated(route, pendingId, fields);
  38. it('should call the API with a POST request', () => {
  39. const response = { isResponse: true };
  40. testSaga(createDoc, action)
  41. .next()
  42. .call(apiRequest, 'post', route, [], fields)
  43. .next({ response, err: null })
  44. .put(docCreateResponded(route, pendingId, null, response))
  45. .next()
  46. .isDone();
  47. });
  48. it('should handle errors', () => {
  49. const err = new Error('something bad happened');
  50. testSaga(createDoc, action)
  51. .next()
  52. .call(apiRequest, 'post', route, [], fields)
  53. .next({ response: null, err })
  54. .put(docCreateResponded(route, pendingId, err, null))
  55. .next()
  56. .isDone();
  57. });
  58. });
  59. describe('readDoc', () => {
  60. it('should call the API with a GET request', () => {
  61. const route = 'employees';
  62. const response = { isResponse: true };
  63. testSaga(readDoc, docRead(route))
  64. .next()
  65. .call(apiRequest, 'get', route)
  66. .next({ response, err: null })
  67. .put(docReadResponded(route, null, response))
  68. .next()
  69. .isDone();
  70. });
  71. });
  72. describe('updateDoc', () => {
  73. it('should call the API with a PUT request', () => {
  74. const route = 'employees';
  75. const actualId = '<actualId>';
  76. const fields = {
  77. name: 'Jack Doe'
  78. };
  79. const response = { isResponse: true };
  80. testSaga(updateDoc, docUpdated(route, actualId, fields))
  81. .next()
  82. .call(apiRequest, 'put', route, [actualId], fields)
  83. .next({ response, err: null })
  84. .put(docUpdateResponded(route, actualId, null, response))
  85. .next()
  86. .isDone();
  87. });
  88. });
  89. describe('deleteDoc', () => {
  90. it('should call the API with a DELETE request', () => {
  91. const route = 'employees';
  92. const actualId = '<actualId>';
  93. const response = { isResponse: true };
  94. testSaga(deleteDoc, docDeleted(route, actualId))
  95. .next()
  96. .call(apiRequest, 'delete', route, [actualId])
  97. .next({ response, err: null })
  98. .put(docDeleteResponded(route, actualId, null))
  99. .next()
  100. .isDone();
  101. });
  102. });
  103. it('should fork listeners for CRUD actions', () => {
  104. testSaga(crudSaga)
  105. .next()
  106. .takeEveryEffect(DOC_CREATED, createDoc)
  107. .next()
  108. .takeEveryEffect(DOC_READ, readDoc)
  109. .next()
  110. .takeEveryEffect(DOC_UPDATED, updateDoc)
  111. .next()
  112. .takeEveryEffect(DOC_DELETED, deleteDoc)
  113. .next()
  114. .isDone();
  115. });
  116. });