You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

643 lines
22 KiB

  1. /**
  2. * @prettier
  3. */
  4. const {
  5. ParameterPrimitiveTestCases,
  6. RequestBodyPrimitiveTestCases,
  7. ResponsePrimitiveTestCases,
  8. } = require("../../helpers/multiple-examples")
  9. describe("OpenAPI 3.0 Multiple Examples - core features", () => {
  10. describe("/String", () => {
  11. describe("in a parameter", () => {
  12. ParameterPrimitiveTestCases({
  13. operationDomId: "#operations-default-post_String",
  14. parameterName: "message",
  15. exampleA: {
  16. key: "StringExampleA",
  17. value: "hello world",
  18. },
  19. exampleB: {
  20. key: "StringExampleB",
  21. value: "The quick brown fox jumps over the lazy dog",
  22. },
  23. customUserInput: "OpenAPIs.org <3",
  24. })
  25. })
  26. describe("in a Request Body", () => {
  27. RequestBodyPrimitiveTestCases({
  28. operationDomId: "#operations-default-post_String",
  29. exampleA: {
  30. key: "StringExampleA",
  31. value: "hello world",
  32. serializedValue: "hello world",
  33. summary: "Don't just string me along...",
  34. },
  35. exampleB: {
  36. key: "StringExampleB",
  37. value: "The quick brown fox jumps over the lazy dog",
  38. serializedValue: "The quick brown fox jumps over the lazy dog",
  39. summary: "I'm a pangram!",
  40. },
  41. customUserInput: "OpenAPIs.org <3",
  42. })
  43. })
  44. describe("in a Response", () => {
  45. ResponsePrimitiveTestCases({
  46. operationDomId: "#operations-default-post_String",
  47. exampleA: {
  48. key: "StringExampleA",
  49. value: "hello world",
  50. summary: "Don't just string me along...",
  51. },
  52. exampleB: {
  53. key: "StringExampleB",
  54. value: "The quick brown fox jumps over the lazy dog",
  55. summary: "I'm a pangram!",
  56. },
  57. exampleC: {
  58. key: "StringExampleC",
  59. value: "JavaScript rules",
  60. summary: "A third example, for use in special places...",
  61. },
  62. })
  63. })
  64. })
  65. describe("/Number", () => {
  66. describe("in a parameter", () => {
  67. ParameterPrimitiveTestCases({
  68. operationDomId: "#operations-default-post_Number",
  69. parameterName: "message",
  70. exampleA: {
  71. key: "NumberExampleA",
  72. value: "7710263025",
  73. },
  74. exampleB: {
  75. key: "NumberExampleB",
  76. value: "9007199254740991",
  77. },
  78. exampleC: {
  79. key: "NumberExampleC",
  80. value: "0",
  81. },
  82. customUserInput: "9001",
  83. })
  84. })
  85. describe("in a Request Body", () => {
  86. RequestBodyPrimitiveTestCases({
  87. operationDomId: "#operations-default-post_Number",
  88. exampleA: {
  89. key: "NumberExampleA",
  90. value: "7710263025",
  91. summary: "World population",
  92. },
  93. exampleB: {
  94. key: "NumberExampleB",
  95. value: "9007199254740991",
  96. summary: "Number.MAX_SAFE_INTEGER",
  97. },
  98. exampleC: {
  99. key: "NumberExampleC",
  100. value: "0",
  101. },
  102. customUserInput: "1337",
  103. })
  104. })
  105. describe("in a Response", () => {
  106. ResponsePrimitiveTestCases({
  107. operationDomId: "#operations-default-post_Number",
  108. exampleA: {
  109. key: "NumberExampleA",
  110. value: "7710263025",
  111. summary: "World population",
  112. },
  113. exampleB: {
  114. key: "NumberExampleB",
  115. value: "9007199254740991",
  116. summary: "Number.MAX_SAFE_INTEGER",
  117. },
  118. exampleC: {
  119. key: "NumberExampleC",
  120. value: "0",
  121. },
  122. })
  123. })
  124. })
  125. describe("/Boolean", () => {
  126. describe("in a parameter", () => {
  127. it("should render and apply the first example and value by default", () => {
  128. cy.visit(
  129. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  130. )
  131. .get("#operations-default-post_Boolean")
  132. .click()
  133. // Assert on the initial dropdown value
  134. .get("table.parameters .examples-select > select")
  135. .find(":selected")
  136. .should("have.text", "The truth will set you free")
  137. // Assert on the initial JsonSchemaForm value
  138. .get(".parameters-col_description > select")
  139. .should("have.attr", "disabled")
  140. .get(".parameters-col_description > select")
  141. .find(":selected")
  142. .should("have.text", "true")
  143. // Execute
  144. .get(".try-out__btn")
  145. .click()
  146. .get(".execute")
  147. .click()
  148. // Assert on the request URL
  149. .get(".request-url")
  150. .contains(`?message=true`)
  151. })
  152. it("should render and apply the second value when chosen", () => {
  153. cy.visit(
  154. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  155. )
  156. .get("#operations-default-post_Boolean")
  157. .click()
  158. // Set the dropdown value, then assert on it
  159. .get("table.parameters .examples-select > select")
  160. .select("BooleanExampleB")
  161. .find(":selected")
  162. .should("have.text", "Friends don't lie to friends")
  163. // Set the JsonSchemaForm value, then assert on it
  164. .get(".parameters-col_description > select")
  165. .find(":selected")
  166. .should("have.text", "false")
  167. // Execute
  168. .get(".try-out__btn")
  169. .click()
  170. .get(".execute")
  171. .click()
  172. // Assert on the request URL
  173. .get(".request-url")
  174. .contains(`?message=false`)
  175. })
  176. it("should track value changes against valid examples", () => {
  177. cy.visit(
  178. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  179. )
  180. .get("#operations-default-post_Boolean")
  181. .click()
  182. .get(".try-out__btn")
  183. .click()
  184. // Set the JsonSchemaForm value, then assert on it
  185. .get(".parameters-col_description > select")
  186. .select("false")
  187. .find(":selected")
  188. .should("have.text", "false")
  189. // Assert on the dropdown value
  190. .get("table.parameters .examples-select > select")
  191. .find(":selected")
  192. .should("have.text", "Friends don't lie to friends")
  193. // Execute
  194. .get(".execute")
  195. .click()
  196. // Assert on the request URL
  197. .get(".request-url")
  198. .contains(`?message=false`)
  199. })
  200. })
  201. describe("in a Request Body", () => {
  202. RequestBodyPrimitiveTestCases({
  203. operationDomId: "#operations-default-post_Boolean",
  204. exampleA: {
  205. key: "BooleanExampleA",
  206. value: "true",
  207. summary: "The truth will set you free",
  208. },
  209. exampleB: {
  210. key: "BooleanExampleB",
  211. value: "false",
  212. summary: "Friends don't lie to friends",
  213. },
  214. customUserInput: "tralse",
  215. })
  216. })
  217. describe("in a Response", () => {
  218. it("should render and apply the first example and value by default", () => {
  219. cy.visit(
  220. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  221. )
  222. .get("#operations-default-post_Boolean")
  223. .click()
  224. // Assert on the initial dropdown value
  225. .get(".responses-wrapper .examples-select > select")
  226. .find(":selected")
  227. .should("have.text", "The truth will set you free")
  228. // Assert on the example value
  229. .get(".example.microlight")
  230. .should("have.text", "true")
  231. })
  232. it("should render and apply the second value when chosen", () => {
  233. cy.visit(
  234. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  235. )
  236. .get("#operations-default-post_Boolean")
  237. .click()
  238. // Set the dropdown value, then assert on it
  239. .get(".responses-wrapper .examples-select > select")
  240. .select("BooleanExampleB")
  241. .find(":selected")
  242. .should("have.text", "Friends don't lie to friends")
  243. // Assert on the example value
  244. .get(".example.microlight")
  245. .should("have.text", "false")
  246. })
  247. })
  248. })
  249. describe("/Array", () => {
  250. describe("in a Parameter", () => {
  251. it("should have the first example's array entries by default", () => {
  252. cy.visit(
  253. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  254. )
  255. .get("#operations-default-post_Array")
  256. .click()
  257. .get(".json-schema-form-item > input")
  258. .then(inputs => {
  259. expect(inputs.map((i, el) => el.value).toArray()).to.deep.equal([
  260. "a",
  261. "b",
  262. "c",
  263. ])
  264. })
  265. .get(".parameters-col_description .examples-select > select")
  266. .find(":selected")
  267. .should("have.text", "A lowly array of strings")
  268. })
  269. it("should switch to the second array's entries via dropdown", () => {
  270. cy.visit(
  271. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  272. )
  273. .get("#operations-default-post_Array")
  274. .click()
  275. .get(".parameters-col_description .examples-select > select")
  276. .select("ArrayExampleB")
  277. .get(".json-schema-form-item > input")
  278. .then(inputs => {
  279. expect(inputs.map((i, el) => el.value).toArray()).to.deep.equal([
  280. "1",
  281. "2",
  282. "3",
  283. "4",
  284. ])
  285. })
  286. .get(".parameters-col_description .examples-select > select")
  287. .find(":selected")
  288. .should("have.text", "A lowly array of numbers")
  289. })
  290. it("should not allow modification of values in static mode", () => {
  291. cy.visit(
  292. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  293. )
  294. .get("#operations-default-post_Array")
  295. .click()
  296. .get(".parameters-col_description .examples-select > select")
  297. .select("ArrayExampleB")
  298. // Add a new item
  299. .get(".json-schema-form-item > input")
  300. .should("have.attr", "disabled")
  301. })
  302. it("should allow modification of values in Try-It-Out", () => {
  303. cy.visit(
  304. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  305. )
  306. .get("#operations-default-post_Array")
  307. .click()
  308. .get(".try-out__btn")
  309. .click()
  310. .get(".parameters-col_description .examples-select > select")
  311. .select("ArrayExampleB")
  312. // Add a new item
  313. .get(".json-schema-form-item-add")
  314. .click()
  315. .get(".json-schema-form-item:last-of-type > input")
  316. .type("5")
  317. // Assert against the input fields
  318. .get(".json-schema-form-item > input")
  319. .then(inputs => {
  320. expect(inputs.map((i, el) => el.value).toArray()).to.deep.equal([
  321. "1",
  322. "2",
  323. "3",
  324. "4",
  325. "5",
  326. ])
  327. })
  328. .get(".parameters-col_description .examples-select > select")
  329. .find(":selected")
  330. .should("have.text", "[Modified value]")
  331. })
  332. it("should retain a modified value, and support returning to it", () => {
  333. cy.visit(
  334. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  335. )
  336. .get("#operations-default-post_Array")
  337. .click()
  338. .get(".try-out__btn")
  339. .click()
  340. .get(".parameters-col_description .examples-select > select")
  341. .select("ArrayExampleB")
  342. // Add a new item
  343. .get(".json-schema-form-item-add")
  344. .click()
  345. .get(".json-schema-form-item:last-of-type > input")
  346. .type("5")
  347. // Reset to an example
  348. .get(".parameters-col_description .examples-select > select")
  349. .select("ArrayExampleB")
  350. // Assert against the input fields
  351. .get(".json-schema-form-item > input")
  352. .then(inputs => {
  353. expect(inputs.map((i, el) => el.value).toArray()).to.deep.equal([
  354. "1",
  355. "2",
  356. "3",
  357. "4",
  358. ])
  359. })
  360. .get(".parameters-col_description .examples-select > select")
  361. .find(":selected")
  362. .should("have.text", "A lowly array of numbers")
  363. // Return to the modified value
  364. .get(".parameters-col_description .examples-select > select")
  365. .select("__MODIFIED__VALUE__")
  366. // Assert that our modified value is back
  367. .get(".json-schema-form-item > input")
  368. .then(inputs => {
  369. expect(inputs.map((i, el) => el.value).toArray()).to.deep.equal([
  370. "1",
  371. "2",
  372. "3",
  373. "4",
  374. "5",
  375. ])
  376. })
  377. .get(".parameters-col_description .examples-select > select")
  378. .find(":selected")
  379. .should("have.text", "[Modified value]")
  380. })
  381. })
  382. describe("in a Request Body", () => {
  383. it("should have the first example's array entries by default", () => {
  384. cy.visit(
  385. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  386. )
  387. .get("#operations-default-post_Array")
  388. .click()
  389. // Check HighlightCode value
  390. .get(".opblock-section-request-body .highlight-code")
  391. .should("include.text", JSON.stringify(["a", "b", "c"], null, 2))
  392. // Check dropdown value
  393. .get(".opblock-section-request-body .examples-select > select")
  394. .find(":selected")
  395. .should("have.text", "A lowly array of strings")
  396. // Switch to Try-It-Out
  397. .get(".try-out__btn")
  398. .click()
  399. // Check textarea value
  400. .get(".opblock-section-request-body textarea")
  401. .should("have.value", JSON.stringify(["a", "b", "c"], null, 2))
  402. // Check dropdown value
  403. .get(".opblock-section-request-body .examples-select > select")
  404. .find(":selected")
  405. .should("have.text", "A lowly array of strings")
  406. })
  407. it("should switch to the second array's entries via dropdown", () => {
  408. cy.visit(
  409. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  410. )
  411. .get("#operations-default-post_Array")
  412. .click()
  413. .get(".opblock-section-request-body .examples-select > select")
  414. .select("ArrayExampleB")
  415. .get(".opblock-section-request-body .highlight-code")
  416. .should("include.text", JSON.stringify([1, 2, 3, 4], null, 2))
  417. .get(".opblock-section-request-body .examples-select > select")
  418. .find(":selected")
  419. .should("have.text", "A lowly array of numbers")
  420. // Switch to Try-It-Out
  421. .get(".try-out__btn")
  422. .click()
  423. // Check textarea value
  424. .get(".opblock-section-request-body textarea")
  425. .should("include.text", JSON.stringify([1, 2, 3, 4], null, 2))
  426. // Check dropdown value
  427. .get(".opblock-section-request-body .examples-select > select")
  428. .find(":selected")
  429. .should("have.text", "A lowly array of numbers")
  430. })
  431. it("should allow modification of values", () => {
  432. cy.visit(
  433. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  434. )
  435. .get("#operations-default-post_Array")
  436. .click()
  437. // Switch to Try-It-Out
  438. .get(".try-out__btn")
  439. .click()
  440. // Choose the second example
  441. .get(".opblock-section-request-body .examples-select > select")
  442. .select("ArrayExampleB")
  443. // Change the value
  444. .get(".opblock-section-request-body textarea")
  445. .type(`{leftarrow}{leftarrow},{enter} 5`)
  446. // Check that [Modified value] is displayed in dropdown
  447. .get(".opblock-section-request-body .examples-select > select")
  448. .find(":selected")
  449. .should("have.text", "[Modified value]")
  450. // Check textarea value
  451. .get(".opblock-section-request-body textarea")
  452. .should("include.text", JSON.stringify([1, 2, 3, 4, 5], null, 2))
  453. })
  454. it("should retain a modified value, and support returning to it", () => {
  455. cy.visit(
  456. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  457. )
  458. .get("#operations-default-post_Array")
  459. .click()
  460. // Switch to Try-It-Out
  461. .get(".try-out__btn")
  462. .click()
  463. // Choose the second example as the example to start with
  464. .get(".opblock-section-request-body .examples-select > select")
  465. .select("ArrayExampleB")
  466. // Change the value
  467. .get(".opblock-section-request-body textarea")
  468. .type(`{leftarrow}{leftarrow},{enter} 5`)
  469. // Check that [Modified value] is displayed in dropdown
  470. .get(".opblock-section-request-body .examples-select > select")
  471. .find(":selected")
  472. .should("have.text", "[Modified value]")
  473. // Check textarea value
  474. .get(".opblock-section-request-body textarea")
  475. .should("include.text", JSON.stringify([1, 2, 3, 4, 5], null, 2))
  476. // Choose the second example
  477. .get(".opblock-section-request-body .examples-select > select")
  478. .select("ArrayExampleB")
  479. // Check that the example is displayed in dropdown
  480. .get(".opblock-section-request-body .examples-select > select")
  481. .find(":selected")
  482. .should("have.text", "A lowly array of numbers")
  483. // Check textarea value
  484. .get(".opblock-section-request-body textarea")
  485. .should("include.text", JSON.stringify([1, 2, 3, 4], null, 2))
  486. // Switch back to the modified value
  487. .get(".opblock-section-request-body .examples-select > select")
  488. .select("__MODIFIED__VALUE__")
  489. // Check textarea value
  490. .get(".opblock-section-request-body textarea")
  491. .should("include.text", JSON.stringify([1, 2, 3, 4, 5], null, 2))
  492. })
  493. })
  494. describe("in a Response", () => {
  495. it("should render and apply the first example and value by default", () => {
  496. cy.visit(
  497. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  498. )
  499. .get("#operations-default-post_Array")
  500. .click()
  501. // Assert on the initial dropdown value
  502. .get(".responses-wrapper .examples-select > select")
  503. .find(":selected")
  504. .should("have.text", "A lowly array of strings")
  505. // Assert on the example value
  506. .get(".example.microlight")
  507. .should("include.text", JSON.stringify(["a", "b", "c"], null, 2))
  508. })
  509. it("should render and apply the second value when chosen", () => {
  510. cy.visit(
  511. "/?url=/documents/features/multiple-examples-core.openapi.yaml"
  512. )
  513. .get("#operations-default-post_Array")
  514. .click()
  515. // Set the dropdown value, then assert on it
  516. .get(".responses-wrapper .examples-select > select")
  517. .select("ArrayExampleB")
  518. .find(":selected")
  519. .should("have.text", "A lowly array of numbers")
  520. // Assert on the example value
  521. .get(".example.microlight")
  522. .should("include.text", JSON.stringify([1, 2, 3, 4], null, 2))
  523. })
  524. })
  525. })
  526. describe("/Object", () => {
  527. describe("in a Parameter", () => {
  528. ParameterPrimitiveTestCases({
  529. operationDomId: "#operations-default-post_Object",
  530. parameterName: "data",
  531. customUserInput: `{{} "openapiIsCool": true }`,
  532. customExpectedUrlSubstring: "?openapiIsCool=true",
  533. exampleA: {
  534. key: "ObjectExampleA",
  535. serializedValue:
  536. "firstName=Kyle&lastName=Shockey&email=kyle.shockey%40smartbear.com",
  537. value: JSON.stringify(
  538. {
  539. firstName: "Kyle",
  540. lastName: "Shockey",
  541. email: "kyle.shockey@smartbear.com",
  542. },
  543. null,
  544. 2
  545. ),
  546. },
  547. exampleB: {
  548. key: "ObjectExampleB",
  549. serializedValue:
  550. "name=Abbey&type=kitten&color=calico&gender=female&age=11%20weeks",
  551. value: JSON.stringify(
  552. {
  553. name: "Abbey",
  554. type: "kitten",
  555. color: "calico",
  556. gender: "female",
  557. age: "11 weeks",
  558. },
  559. null,
  560. 2
  561. ),
  562. },
  563. })
  564. })
  565. describe("in a Request Body", () => {
  566. RequestBodyPrimitiveTestCases({
  567. operationDomId: "#operations-default-post_Object",
  568. primaryMediaType: "application/json",
  569. // ↓ not a typo, Cypress requires escaping { when using `cy.type`
  570. customUserInput: `{{} "openapiIsCool": true }`,
  571. customExpectedUrlSubstring: "?openapiIsCool=true",
  572. customUserInputExpectedCurlSubstring: `{\\"openapiIsCool\\":true}`,
  573. exampleA: {
  574. key: "ObjectExampleA",
  575. serializedValue: `{\\"firstName\\":\\"Kyle\\",\\"lastName\\":\\"Shockey\\",\\"email\\":\\"kyle.shockey@smartbear.com\\"}`,
  576. value: JSON.stringify(
  577. {
  578. firstName: "Kyle",
  579. lastName: "Shockey",
  580. email: "kyle.shockey@smartbear.com",
  581. },
  582. null,
  583. 2
  584. ),
  585. summary: "A user's contact info",
  586. },
  587. exampleB: {
  588. key: "ObjectExampleB",
  589. serializedValue: `{\\"name\\":\\"Abbey\\",\\"type\\":\\"kitten\\",\\"color\\":\\"calico\\",\\"gender\\":\\"female\\",\\"age\\":\\"11 weeks\\"}`,
  590. value: JSON.stringify(
  591. {
  592. name: "Abbey",
  593. type: "kitten",
  594. color: "calico",
  595. gender: "female",
  596. age: "11 weeks",
  597. },
  598. null,
  599. 2
  600. ),
  601. summary: "A wonderful kitten's info",
  602. },
  603. })
  604. })
  605. describe("in a Response", () => {
  606. ResponsePrimitiveTestCases({
  607. operationDomId: "#operations-default-post_Object",
  608. exampleA: {
  609. key: "ObjectExampleA",
  610. value: JSON.stringify(
  611. {
  612. firstName: "Kyle",
  613. lastName: "Shockey",
  614. email: "kyle.shockey@smartbear.com",
  615. },
  616. null,
  617. 2
  618. ),
  619. summary: "A user's contact info",
  620. },
  621. exampleB: {
  622. key: "ObjectExampleB",
  623. value: JSON.stringify(
  624. {
  625. name: "Abbey",
  626. type: "kitten",
  627. color: "calico",
  628. gender: "female",
  629. age: "11 weeks",
  630. },
  631. null,
  632. 2
  633. ),
  634. summary: "A wonderful kitten's info",
  635. },
  636. })
  637. })
  638. })
  639. })