crud.spec.js 5.0 KB

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