reducers.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. import { fromJS } from "immutable"
  2. import reducer from "corePlugins/oas3/reducers"
  3. describe("oas3 plugin - reducer", function () {
  4. describe("SET_REQUEST_BODY_VALIDATE_ERROR", () => {
  5. const setRequestBodyValidateError = reducer["oas3_set_request_body_validate_error"]
  6. describe("missingBodyValue exists, e.g. application/json", () => {
  7. it("should set errors", () => {
  8. const state = fromJS({
  9. requestData: {
  10. "/pet": {
  11. post: {
  12. bodyValue: "",
  13. requestContentType: "application/json"
  14. }
  15. }
  16. }
  17. })
  18. const result = setRequestBodyValidateError(state, {
  19. payload: {
  20. path: "/pet",
  21. method: "post",
  22. validationErrors: {
  23. missingBodyValue: true,
  24. missingRequiredKeys: []
  25. },
  26. }
  27. })
  28. const expectedResult = {
  29. requestData: {
  30. "/pet": {
  31. post: {
  32. bodyValue: "",
  33. requestContentType: "application/json",
  34. errors: ["Required field is not provided"]
  35. }
  36. }
  37. }
  38. }
  39. expect(result.toJS()).toEqual(expectedResult)
  40. })
  41. })
  42. describe("missingRequiredKeys exists with length, e.g. application/x-www-form-urleconded", () => {
  43. it("should set nested errors", () => {
  44. const state = fromJS({
  45. requestData: {
  46. "/pet": {
  47. post: {
  48. bodyValue: {
  49. id: {
  50. value: "10",
  51. },
  52. name: {
  53. value: "",
  54. },
  55. },
  56. requestContentType: "application/x-www-form-urlencoded"
  57. }
  58. }
  59. }
  60. })
  61. const result = setRequestBodyValidateError(state, {
  62. payload: {
  63. path: "/pet",
  64. method: "post",
  65. validationErrors: {
  66. missingBodyValue: null,
  67. missingRequiredKeys: ["name"]
  68. },
  69. }
  70. })
  71. const expectedResult = {
  72. requestData: {
  73. "/pet": {
  74. post: {
  75. bodyValue: {
  76. id: {
  77. value: "10",
  78. },
  79. name: {
  80. value: "",
  81. errors: ["Required field is not provided"]
  82. },
  83. },
  84. requestContentType: "application/x-www-form-urlencoded",
  85. }
  86. }
  87. }
  88. }
  89. expect(result.toJS()).toEqual(expectedResult)
  90. })
  91. it("should overwrite nested errors, for keys listed in missingRequiredKeys", () => {
  92. const state = fromJS({
  93. requestData: {
  94. "/pet": {
  95. post: {
  96. bodyValue: {
  97. id: {
  98. value: "10",
  99. },
  100. name: {
  101. value: "",
  102. errors: ["some fake error"]
  103. },
  104. },
  105. requestContentType: "application/x-www-form-urlencoded"
  106. }
  107. }
  108. }
  109. })
  110. const result = setRequestBodyValidateError(state, {
  111. payload: {
  112. path: "/pet",
  113. method: "post",
  114. validationErrors: {
  115. missingBodyValue: null,
  116. missingRequiredKeys: ["name"]
  117. },
  118. }
  119. })
  120. const expectedResult = {
  121. requestData: {
  122. "/pet": {
  123. post: {
  124. bodyValue: {
  125. id: {
  126. value: "10",
  127. },
  128. name: {
  129. value: "",
  130. errors: ["Required field is not provided"]
  131. },
  132. },
  133. requestContentType: "application/x-www-form-urlencoded",
  134. }
  135. }
  136. }
  137. }
  138. expect(result.toJS()).toEqual(expectedResult)
  139. })
  140. it("should not overwrite nested errors, for keys not listed in missingRequiredKeys", () => {
  141. const state = fromJS({
  142. requestData: {
  143. "/pet": {
  144. post: {
  145. bodyValue: {
  146. id: {
  147. value: "10",
  148. errors: ["random error should not be overwritten"]
  149. },
  150. name: {
  151. value: "",
  152. errors: ["some fake error"]
  153. },
  154. },
  155. requestContentType: "application/x-www-form-urlencoded"
  156. }
  157. }
  158. }
  159. })
  160. const result = setRequestBodyValidateError(state, {
  161. payload: {
  162. path: "/pet",
  163. method: "post",
  164. validationErrors: {
  165. missingBodyValue: null,
  166. missingRequiredKeys: ["name"]
  167. },
  168. }
  169. })
  170. const expectedResult = {
  171. requestData: {
  172. "/pet": {
  173. post: {
  174. bodyValue: {
  175. id: {
  176. value: "10",
  177. errors: ["random error should not be overwritten"]
  178. },
  179. name: {
  180. value: "",
  181. errors: ["Required field is not provided"]
  182. },
  183. },
  184. requestContentType: "application/x-www-form-urlencoded",
  185. }
  186. }
  187. }
  188. }
  189. expect(result.toJS()).toEqual(expectedResult)
  190. })
  191. it("should set multiple nested errors", () => {
  192. const state = fromJS({
  193. requestData: {
  194. "/pet": {
  195. post: {
  196. bodyValue: {
  197. id: {
  198. value: "",
  199. },
  200. name: {
  201. value: "",
  202. },
  203. },
  204. requestContentType: "application/x-www-form-urlencoded"
  205. }
  206. }
  207. }
  208. })
  209. const result = setRequestBodyValidateError(state, {
  210. payload: {
  211. path: "/pet",
  212. method: "post",
  213. validationErrors: {
  214. missingBodyValue: null,
  215. missingRequiredKeys: ["id", "name"]
  216. },
  217. }
  218. })
  219. const expectedResult = {
  220. requestData: {
  221. "/pet": {
  222. post: {
  223. bodyValue: {
  224. id: {
  225. value: "",
  226. errors: ["Required field is not provided"]
  227. },
  228. name: {
  229. value: "",
  230. errors: ["Required field is not provided"]
  231. },
  232. },
  233. requestContentType: "application/x-www-form-urlencoded",
  234. }
  235. }
  236. }
  237. }
  238. expect(result.toJS()).toEqual(expectedResult)
  239. })
  240. })
  241. describe("missingRequiredKeys is empty list", () => {
  242. it("should not set any errors, and return state unchanged", () => {
  243. const state = fromJS({
  244. requestData: {
  245. "/pet": {
  246. post: {
  247. bodyValue: {
  248. id: {
  249. value: "10",
  250. },
  251. name: {
  252. value: "",
  253. },
  254. },
  255. requestContentType: "application/x-www-form-urlencoded"
  256. }
  257. }
  258. }
  259. })
  260. const result = setRequestBodyValidateError(state, {
  261. payload: {
  262. path: "/pet",
  263. method: "post",
  264. validationErrors: {
  265. missingBodyValue: null,
  266. missingRequiredKeys: []
  267. },
  268. }
  269. })
  270. const expectedResult = {
  271. requestData: {
  272. "/pet": {
  273. post: {
  274. bodyValue: {
  275. id: {
  276. value: "10",
  277. },
  278. name: {
  279. value: "",
  280. },
  281. },
  282. requestContentType: "application/x-www-form-urlencoded",
  283. }
  284. }
  285. }
  286. }
  287. expect(result.toJS()).toEqual(expectedResult)
  288. })
  289. })
  290. describe("other unexpected payload, e.g. no missingBodyValue or missingRequiredKeys", () => {
  291. it("should not throw error if receiving unexpected validationError format. return state unchanged", () => {
  292. const state = fromJS({
  293. requestData: {
  294. "/pet": {
  295. post: {
  296. bodyValue: {
  297. id: {
  298. value: "10",
  299. },
  300. name: {
  301. value: "",
  302. },
  303. },
  304. requestContentType: "application/x-www-form-urlencoded"
  305. }
  306. }
  307. }
  308. })
  309. const result = setRequestBodyValidateError(state, {
  310. payload: {
  311. path: "/pet",
  312. method: "post",
  313. validationErrors: {
  314. missingBodyValue: null,
  315. // missingRequiredKeys: ["none provided"]
  316. },
  317. }
  318. })
  319. const expectedResult = {
  320. requestData: {
  321. "/pet": {
  322. post: {
  323. bodyValue: {
  324. id: {
  325. value: "10",
  326. },
  327. name: {
  328. value: "",
  329. },
  330. },
  331. requestContentType: "application/x-www-form-urlencoded",
  332. }
  333. }
  334. }
  335. }
  336. expect(result.toJS()).toEqual(expectedResult)
  337. })
  338. })
  339. })
  340. describe("CLEAR_REQUEST_BODY_VALIDATE_ERROR", function() {
  341. const clearRequestBodyValidateError = reducer["oas3_clear_request_body_validate_error"]
  342. describe("bodyValue is String, e.g. application/json", () => {
  343. it("should clear errors", () => {
  344. const state = fromJS({
  345. requestData: {
  346. "/pet": {
  347. post: {
  348. bodyValue: "{}",
  349. requestContentType: "application/json"
  350. }
  351. }
  352. }
  353. })
  354. const result = clearRequestBodyValidateError(state, {
  355. payload: {
  356. path: "/pet",
  357. method: "post",
  358. }
  359. })
  360. const expectedResult = {
  361. requestData: {
  362. "/pet": {
  363. post: {
  364. bodyValue: "{}",
  365. requestContentType: "application/json",
  366. errors: []
  367. }
  368. }
  369. }
  370. }
  371. expect(result.toJS()).toEqual(expectedResult)
  372. })
  373. })
  374. describe("bodyValue is Map with entries, e.g. application/x-www-form-urleconded", () => {
  375. it("should clear nested errors, and apply empty error list to all entries", () => {
  376. const state = fromJS({
  377. requestData: {
  378. "/pet": {
  379. post: {
  380. bodyValue: {
  381. id: {
  382. value: "10",
  383. errors: ["some random error"]
  384. },
  385. name: {
  386. value: "doggie",
  387. errors: ["Required field is not provided"]
  388. },
  389. status: {
  390. value: "available"
  391. }
  392. },
  393. requestContentType: "application/x-www-form-urlencoded"
  394. }
  395. }
  396. }
  397. })
  398. const result = clearRequestBodyValidateError(state, {
  399. payload: {
  400. path: "/pet",
  401. method: "post",
  402. }
  403. })
  404. const expectedResult = {
  405. requestData: {
  406. "/pet": {
  407. post: {
  408. bodyValue: {
  409. id: {
  410. value: "10",
  411. errors: [],
  412. },
  413. name: {
  414. value: "doggie",
  415. errors: [],
  416. },
  417. status: {
  418. value: "available",
  419. errors: [],
  420. },
  421. },
  422. requestContentType: "application/x-www-form-urlencoded",
  423. }
  424. }
  425. }
  426. }
  427. expect(result.toJS()).toEqual(expectedResult)
  428. })
  429. })
  430. describe("bodyValue is empty Map", () => {
  431. it("should return state unchanged", () => {
  432. const state = fromJS({
  433. requestData: {
  434. "/pet": {
  435. post: {
  436. bodyValue: {},
  437. requestContentType: "application/x-www-form-urlencoded"
  438. }
  439. }
  440. }
  441. })
  442. const result = clearRequestBodyValidateError(state, {
  443. payload: {
  444. path: "/pet",
  445. method: "post",
  446. }
  447. })
  448. const expectedResult = {
  449. requestData: {
  450. "/pet": {
  451. post: {
  452. bodyValue: {
  453. },
  454. requestContentType: "application/x-www-form-urlencoded",
  455. }
  456. }
  457. }
  458. }
  459. expect(result.toJS()).toEqual(expectedResult)
  460. })
  461. })
  462. })
  463. describe("CLEAR_REQUEST_BODY_VALUE", function () {
  464. const clearRequestBodyValue = reducer["oas3_clear_request_body_value"]
  465. describe("when requestBodyValue is a String", () => {
  466. it("should clear requestBodyValue with empty String", () => {
  467. const state = fromJS({
  468. requestData: {
  469. "/pet": {
  470. post: {
  471. bodyValue: "some random string",
  472. requestContentType: "application/json"
  473. }
  474. }
  475. }
  476. })
  477. const result = clearRequestBodyValue(state, {
  478. payload: {
  479. pathMethod: ["/pet", "post"],
  480. }
  481. })
  482. const expectedResult = {
  483. requestData: {
  484. "/pet": {
  485. post: {
  486. bodyValue: "",
  487. requestContentType: "application/json",
  488. }
  489. }
  490. }
  491. }
  492. expect(result.toJS()).toEqual(expectedResult)
  493. })
  494. })
  495. describe("when requestBodyValue is a Map", () => {
  496. it("should clear requestBodyValue with empty Map", () => {
  497. const state = fromJS({
  498. requestData: {
  499. "/pet": {
  500. post: {
  501. bodyValue: {
  502. id: {
  503. value: "10",
  504. },
  505. },
  506. requestContentType: "application/x-www-form-urlencoded"
  507. }
  508. }
  509. }
  510. })
  511. const result = clearRequestBodyValue(state, {
  512. payload: {
  513. pathMethod: ["/pet", "post"],
  514. }
  515. })
  516. const expectedResult = {
  517. requestData: {
  518. "/pet": {
  519. post: {
  520. bodyValue: {},
  521. requestContentType: "application/x-www-form-urlencoded",
  522. }
  523. }
  524. }
  525. }
  526. expect(result.toJS()).toEqual(expectedResult)
  527. })
  528. })
  529. })
  530. })