crud.spec.js 5.2 KB

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