reducer.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { fromJS } from "immutable"
  2. import reducer from "corePlugins/spec/reducers"
  3. describe("spec plugin - reducer", function(){
  4. describe("update operation meta value", function() {
  5. it("should update the operation metadata at the specified key", () => {
  6. const updateOperationValue = reducer["spec_update_operation_meta_value"]
  7. const state = fromJS({
  8. resolved: {
  9. "paths": {
  10. "/pet": {
  11. "post": {
  12. "description": "my operation"
  13. }
  14. }
  15. }
  16. }
  17. })
  18. let result = updateOperationValue(state, {
  19. payload: {
  20. path: ["/pet", "post"],
  21. value: "application/json",
  22. key: "consumes_value"
  23. }
  24. })
  25. let expectedResult = {
  26. resolved: {
  27. "paths": {
  28. "/pet": {
  29. "post": {
  30. "description": "my operation"
  31. }
  32. }
  33. }
  34. },
  35. meta: {
  36. paths: {
  37. "/pet": {
  38. post: {
  39. "consumes_value": "application/json"
  40. }
  41. }
  42. }
  43. }
  44. }
  45. expect(result.toJS()).toEqual(expectedResult)
  46. })
  47. it("shouldn't throw an error if we try to update the consumes_value of a null operation", () => {
  48. const updateOperationValue = reducer["spec_update_operation_meta_value"]
  49. const state = fromJS({
  50. resolved: {
  51. "paths": {
  52. "/pet": {
  53. "post": null
  54. }
  55. }
  56. }
  57. })
  58. let result = updateOperationValue(state, {
  59. payload: {
  60. path: ["/pet", "post"],
  61. value: "application/json",
  62. key: "consumes_value"
  63. }
  64. })
  65. expect(result.toJS()).toEqual(state.toJS())
  66. })
  67. })
  68. describe("set response value", function() {
  69. it("should combine the response and error objects", () => {
  70. const setResponse = reducer["spec_set_response"]
  71. const path = "/pet/post"
  72. const method = "POST"
  73. const state = fromJS({})
  74. const result = setResponse(state, {
  75. payload: {
  76. path: path,
  77. method: method,
  78. res: {
  79. error: true,
  80. err: {
  81. message: "Not Found",
  82. name: "Error",
  83. response: {
  84. data: "response data",
  85. headers: {
  86. key: "value"
  87. },
  88. ok: false,
  89. status: 404,
  90. statusText: "Not Found"
  91. },
  92. status: 404,
  93. statusCode: 404
  94. }
  95. }
  96. }
  97. })
  98. let expectedResult = {
  99. error: true,
  100. message: "Not Found",
  101. name: "Error",
  102. data: "response data",
  103. headers: {
  104. key: "value"
  105. },
  106. ok: false,
  107. status: 404,
  108. statusCode: 404,
  109. statusText: "Not Found"
  110. }
  111. const response = result.getIn(["responses", path, method]).toJS()
  112. expect(response).toEqual(expectedResult)
  113. })
  114. })
  115. describe("SPEC_UPDATE_PARAM", function() {
  116. it("should store parameter values by {in}.{name}", () => {
  117. const updateParam = reducer["spec_update_param"]
  118. const path = "/pet/post"
  119. const method = "POST"
  120. const state = fromJS({})
  121. const result = updateParam(state, {
  122. payload: {
  123. path: [path, method],
  124. paramName: "myBody",
  125. paramIn: "body",
  126. value: `{ "a": 123 }`,
  127. isXml: false
  128. }
  129. })
  130. const response = result.getIn(["meta", "paths", path, method, "parameters", "body.myBody", "value"])
  131. expect(response).toEqual(`{ "a": 123 }`)
  132. })
  133. it("should store parameter values by identity", () => {
  134. const updateParam = reducer["spec_update_param"]
  135. const path = "/pet/post"
  136. const method = "POST"
  137. const param = fromJS({
  138. name: "myBody",
  139. in: "body",
  140. schema: {
  141. type: "string"
  142. }
  143. })
  144. const state = fromJS({})
  145. const result = updateParam(state, {
  146. payload: {
  147. param,
  148. path: [path, method],
  149. value: `{ "a": 123 }`,
  150. isXml: false
  151. }
  152. })
  153. const value = result.getIn(["meta", "paths", path, method, "parameters", `body.myBody.hash-${param.hashCode()}`, "value"])
  154. expect(value).toEqual(`{ "a": 123 }`)
  155. })
  156. })
  157. describe("SPEC_UPDATE_EMPTY_PARAM_INCLUSION", function() {
  158. it("should store parameter values by {in}.{name}", () => {
  159. const updateParam = reducer["spec_update_empty_param_inclusion"]
  160. const path = "/pet/post"
  161. const method = "POST"
  162. const state = fromJS({})
  163. const result = updateParam(state, {
  164. payload: {
  165. pathMethod: [path, method],
  166. paramName: "param",
  167. paramIn: "query",
  168. includeEmptyValue: true
  169. }
  170. })
  171. const response = result.getIn(["meta", "paths", path, method, "parameter_inclusions", "query.param"])
  172. expect(response).toEqual(true)
  173. })
  174. })
  175. })