actions.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { fromJS } from "immutable"
  2. import { execute, executeRequest, changeParamByIdentity, updateEmptyParamInclusion } from "corePlugins/spec/actions"
  3. describe("spec plugin - actions", function(){
  4. describe("execute", function(){
  5. xit("should collect a full request and call fn.executeRequest", function(){
  6. // Given
  7. const system = {
  8. fn: {
  9. fetch: 1
  10. },
  11. specActions: {
  12. executeRequest: jest.fn()
  13. },
  14. specSelectors: {
  15. spec: () => fromJS({spec: 1}),
  16. parameterValues: () => fromJS({values: 2}),
  17. contentTypeValues: () => fromJS({requestContentType: "one", responseContentType: "two"})
  18. }
  19. }
  20. // When
  21. let executeFn = execute({ path: "/one", method: "get"})
  22. executeFn(system)
  23. // Then
  24. expect(system.specActions.executeRequest.calls[0][0]).toEqual({
  25. fetch: 1,
  26. method: "get",
  27. pathName: "/one",
  28. parameters: {
  29. values: 2
  30. },
  31. requestContentType: "one",
  32. responseContentType: "two",
  33. spec: {
  34. spec: 1
  35. }
  36. })
  37. })
  38. xit("should allow passing _extra_ properties to executeRequest", function(){
  39. // Given
  40. const system = {
  41. fn: {},
  42. specActions: {
  43. executeRequest: jest.fn()
  44. },
  45. specSelectors: {
  46. spec: () => fromJS({}),
  47. parameterValues: () => fromJS({}),
  48. contentTypeValues: () => fromJS({})
  49. }
  50. }
  51. // When
  52. let executeFn = execute({ hi: "hello" })
  53. executeFn(system)
  54. // Then
  55. expect(system.specActions.executeRequest.calls[0][0]).toContain({hi: "hello"})
  56. })
  57. })
  58. describe("executeRequest", function(){
  59. xit("should call fn.execute with arg ", function(){
  60. const system = {
  61. fn: {
  62. execute: jest.fn().mockImplementation(() => Promise.resolve({}))
  63. },
  64. specActions: {
  65. setResponse: jest.fn()
  66. }
  67. }
  68. // When
  69. let executeFn = executeRequest({one: 1})
  70. let res = executeFn(system)
  71. // Then
  72. expect(res).toBeInstanceOf(Promise)
  73. expect(system.fn.execute.mock.calls.length).toEqual(1)
  74. expect(system.fn.execute.mock.calls[0][0]).toEqual({
  75. one: 1
  76. })
  77. })
  78. it("should pass requestInterceptor/responseInterceptor to fn.execute", async () => {
  79. // Given
  80. let configs = {
  81. requestInterceptor: jest.fn(),
  82. responseInterceptor: jest.fn()
  83. }
  84. const system = {
  85. fn: {
  86. buildRequest: jest.fn(),
  87. execute: jest.fn().mockImplementation(() => Promise.resolve({}))
  88. },
  89. specActions: {
  90. executeRequest: jest.fn(),
  91. setMutatedRequest: jest.fn(),
  92. setRequest: jest.fn(),
  93. setResponse: jest.fn()
  94. },
  95. specSelectors: {
  96. spec: () => fromJS({}),
  97. parameterValues: () => fromJS({}),
  98. contentTypeValues: () => fromJS({}),
  99. url: () => fromJS({}),
  100. isOAS3: () => false
  101. },
  102. getConfigs: () => configs
  103. }
  104. // When
  105. let executeFn = executeRequest({
  106. pathName: "/one",
  107. method: "GET",
  108. operation: fromJS({operationId: "getOne"})
  109. })
  110. await executeFn(system)
  111. // Then
  112. expect(system.fn.execute.mock.calls.length).toEqual(1)
  113. expect(Object.keys(system.fn.execute.mock.calls[0][0])).toContain("requestInterceptor")
  114. expect(system.fn.execute.mock.calls[0][0]).toEqual(expect.objectContaining({
  115. responseInterceptor: configs.responseInterceptor
  116. }))
  117. expect(system.specActions.setMutatedRequest.mock.calls.length).toEqual(0)
  118. expect(system.specActions.setRequest.mock.calls.length).toEqual(1)
  119. let wrappedRequestInterceptor = system.fn.execute.mock.calls[0][0].requestInterceptor
  120. await wrappedRequestInterceptor(system.fn.execute.mock.calls[0][0])
  121. expect(configs.requestInterceptor.mock.calls.length).toEqual(1)
  122. expect(system.specActions.setMutatedRequest.mock.calls.length).toEqual(1)
  123. expect(system.specActions.setRequest.mock.calls.length).toEqual(1)
  124. })
  125. })
  126. xit("should call specActions.setResponse, when fn.execute resolves", function(){
  127. const response = {serverResponse: true}
  128. const system = {
  129. fn: {
  130. execute: jest.fn().mockImplementation(() => Promise.resolve(response))
  131. },
  132. specActions: {
  133. setResponse: jest.fn()
  134. },
  135. errActions: {
  136. newSpecErr: jest.fn()
  137. }
  138. }
  139. // When
  140. let executeFn = executeRequest({
  141. pathName: "/one",
  142. method: "GET"
  143. })
  144. let executePromise = executeFn(system)
  145. // Then
  146. return executePromise.then( () => {
  147. expect(system.specActions.setResponse.calls.length).toEqual(1)
  148. expect(system.specActions.setResponse.calls[0].arguments).toEqual([
  149. "/one",
  150. "GET",
  151. response
  152. ])
  153. })
  154. })
  155. describe.skip("requestResolvedSubtree", () => {
  156. it("should return a promise ", function() {
  157. })
  158. })
  159. it.skip("should call errActions.newErr, if the fn.execute rejects", function(){
  160. })
  161. describe("changeParamByIdentity", function () {
  162. it("should map its arguments to a payload", function () {
  163. const pathMethod = ["/one", "get"]
  164. const param = fromJS({
  165. name: "body",
  166. in: "body"
  167. })
  168. const value = "my value"
  169. const isXml = false
  170. const result = changeParamByIdentity(pathMethod, param, value, isXml)
  171. expect(result).toEqual({
  172. type: "spec_update_param",
  173. payload: {
  174. path: pathMethod,
  175. param,
  176. value,
  177. isXml
  178. }
  179. })
  180. })
  181. })
  182. describe("updateEmptyParamInclusion", function () {
  183. it("should map its arguments to a payload", function () {
  184. const pathMethod = ["/one", "get"]
  185. const result = updateEmptyParamInclusion(pathMethod, "param", "query", true)
  186. expect(result).toEqual({
  187. type: "spec_update_empty_param_inclusion",
  188. payload: {
  189. pathMethod,
  190. paramName: "param",
  191. paramIn: "query",
  192. includeEmptyValue: true
  193. }
  194. })
  195. })
  196. })
  197. })