123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680 |
- module.exports = {
- ParameterPrimitiveTestCases,
- RequestBodyPrimitiveTestCases,
- ResponsePrimitiveTestCases,
- }
- function ParameterPrimitiveTestCases({
- operationDomId,
- parameterName,
- exampleA, // { value, key }
- exampleB, // { value, key }
- exampleC,
- customUserInput,
- customExpectedUrlSubstring,
- }) {
- it("should render examples options without Modified Value by default", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- .get(operationDomId)
- .click()
- .get(`tr[data-param-name="${parameterName}"]`)
- .find(".examples-select option")
- .should("have.length", exampleC ? 3 : 2)
- // Ensure the relevant input is disabled
- .get(
- `tr[data-param-name="${parameterName}"] input, tr[data-param-name="${parameterName}"] textarea`
- )
- .should("have.attr", "disabled")
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- .get(`.opblock-section-request-body`)
- .find(".examples-select option")
- .should("have.length", exampleC ? 3 : 2)
- })
- it("should set default static and Try-It-Out values based on the first member", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- // Expand the operation
- .get(operationDomId)
- .click()
- // Assert on the static docs value
- .get(
- `tr[data-param-name="${parameterName}"] input,tr[data-param-name="${parameterName}"] textarea`
- )
- .should("have.value", exampleA.value)
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- // Assert on the Try-It-Out value
- .get(
- `tr[data-param-name="${parameterName}"] input,tr[data-param-name="${parameterName}"] textarea`
- )
- .should("have.value", exampleA.value)
- // Execute the operation
- .get(".execute")
- .click()
- // Assert on the request URL
- .get(".request-url")
- .contains(
- exampleA.serializedValue || `?message=${escape(exampleA.value)}`
- )
- })
- it("should set static and Try-It-Out values based on the second member", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- // Expand the operation
- .get(operationDomId)
- .click()
- // Choose the second example
- .get("table.parameters .examples-select > select")
- .select(exampleB.key)
- // Assert on the static docs value
- .get(
- `tr[data-param-name="${parameterName}"] input,tr[data-param-name="${parameterName}"] textarea`
- )
- .should("have.value", exampleB.value)
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- // Assert on the Try-It-Out value
- .get(
- `tr[data-param-name="${parameterName}"] input,tr[data-param-name="${parameterName}"] textarea`
- )
- .should("have.value", exampleB.value)
- // Execute the operation
- .get(".execute")
- .click()
- // Assert on the request URL
- .get(".request-url")
- .contains(
- exampleB.serializedValue
- ? `?${exampleB.serializedValue}`
- : `?message=${escape(exampleB.value)}`
- )
- })
- it("should handle user-entered values correctly", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- // Expand the operation
- .get(operationDomId)
- .click()
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- // Modify the input value
- .get(
- `tr[data-param-name="${parameterName}"] input,tr[data-param-name="${parameterName}"] textarea`
- )
- .clear()
- .type(customUserInput)
- // Assert on the active select menu item
- .get("table.parameters .examples-select > select")
- .find(":selected")
- .should("have.text", "[Modified value]")
- // Execute the operation
- .get(".execute")
- .click()
- // Assert on the request URL
- .get(".request-url")
- .contains(
- customExpectedUrlSubstring || `?message=${escape(customUserInput)}`
- )
- })
- it("should retain user-entered values correctly", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- // Expand the operation
- .get(operationDomId)
- .click()
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- // Modify the input value
- .get(
- `tr[data-param-name="${parameterName}"] input,tr[data-param-name="${parameterName}"] textarea`
- )
- .clear()
- .type(customUserInput)
- // Select the first example
- .get("table.parameters .examples-select > select")
- .select(exampleA.key)
- // Execute the operation
- .get(".execute")
- .click()
- // Assert on the request URL
- .get(".request-url")
- .contains(
- exampleA.serializedValue
- ? `?${exampleA.serializedValue}`
- : `?message=${escape(exampleA.value)}`
- )
- // Select the modified value
- .get("table.parameters .examples-select > select")
- .select("__MODIFIED__VALUE__")
- // Execute the operation
- .get(".execute")
- .click()
- // Assert on the request URL
- .get(".request-url")
- .contains(
- customExpectedUrlSubstring || `?message=${escape(customUserInput)}`
- )
- })
- }
- function RequestBodyPrimitiveTestCases({
- operationDomId,
- exampleA, // { value, key, summary }
- exampleB, // { value, key, summary }
- exampleC,
- customUserInput,
- customUserInputExpectedCurlSubstring,
- primaryMediaType = "text/plain",
- secondaryMediaType = "text/plain+other",
- }) {
- it("should render examples options without Modified Value by default", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- .get(operationDomId)
- .click()
- .get(`.opblock-section-request-body`)
- .find(".examples-select option")
- .should("have.length", exampleC ? 3 : 2)
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- .get(`.opblock-section-request-body`)
- .find(".examples-select option")
- .should("have.length", exampleC ? 3 : 2)
- })
- it("should set default static and Try-It-Out values based on the first member", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- // Expand the operation
- .get(operationDomId)
- .click()
- // Assert on the static docs value
- .get(`.opblock-section-request-body .microlight`)
- .should("include.text", exampleA.value)
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- // Assert on the Try-It-Out value
- .get(`.opblock-section-request-body textarea`)
- .should("have.value", exampleA.value)
- // Execute the operation
- .get(".execute")
- .click()
- // Assert on the curl body
- // TODO: use an interceptor instead of curl
- .get(".curl")
- .contains(`-d "${exampleA.serializedValue || exampleA.value}"`)
- })
- it("should set default static and Try-It-Out values based on choosing the second member in static mode", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- // Expand the operation
- .get(operationDomId)
- .click()
- // Choose the second example
- .get(".opblock-section-request-body .examples-select > select")
- .select(exampleB.key)
- // Assert on the static docs value
- .get(`.opblock-section-request-body .microlight`)
- .should("include.text", exampleB.value)
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- // Assert on the Try-It-Out value
- .get(`.opblock-section-request-body textarea`)
- .should("have.value", exampleB.value)
- // Execute the operation
- .get(".execute")
- .click()
- // Assert on the request URL
- // TODO: use an interceptor instead of curl
- .get(".curl")
- .contains(`-d "${exampleB.serializedValue || exampleB.value}"`)
- })
- it("should set default static and Try-It-Out values based on choosing the second member in Try-It-Out mode", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- // Expand the operation
- .get(operationDomId)
- .click()
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- // Choose the second example
- .get(".opblock-section-request-body .examples-select > select")
- .select(exampleB.key)
- // Assert on the Try-It-Out value
- .get(`.opblock-section-request-body textarea`)
- .should("have.value", exampleB.value)
- // Execute the operation
- .get(".execute")
- .click()
- // Assert on the request URL
- // TODO: use an interceptor instead of curl
- .get(".curl")
- .contains(`-d "${exampleB.serializedValue || exampleB.value}"`)
- // Switch to static docs
- .get(".try-out__btn")
- .click()
- // Assert on the static docs value
- .get(`.opblock-section-request-body .microlight`)
- .should("include.text", exampleB.value)
- })
- it("should return the dropdown entry for an example when manually returning to its value", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- // Expand the operation
- .get(operationDomId)
- .click()
- // Assert on the static docs value
- .get(`.opblock-section-request-body .microlight`)
- .should("include.text", exampleA.value)
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- // Assert on the Try-It-Out value
- .get(`.opblock-section-request-body textarea`)
- .should("have.value", exampleA.value)
- // Clear the Try-It-Out value, replace it with custom value
- .clear()
- .type(customUserInput)
- // Assert on the dropdown value
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("have.text", "[Modified value]")
- // Modify the value again, going back to the example value
- .get(`.opblock-section-request-body textarea`)
- .clear()
- .type(exampleA.value)
- // Assert on the dropdown value returning to the example value
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("include.text", exampleA.summary)
- })
- it("should retain choosing a member in static docs when changing the media type", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- // Expand the operation
- .get(operationDomId)
- .click()
- // Choose the second example
- .get(".opblock-section-request-body .examples-select > select")
- .select(exampleB.key)
- // Change the media type
- .get(".opblock-section-request-body .content-type")
- .select(secondaryMediaType)
- // Assert on the static docs value
- .get(`.opblock-section-request-body .microlight`)
- .should("include.text", exampleB.value)
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- // Assert on the Try-It-Out value
- .get(`.opblock-section-request-body textarea`)
- .should("have.value", exampleB.value)
- // Execute the operation
- .get(".execute")
- .click()
- // Assert on the request URL
- // TODO: use an interceptor instead of curl
- .get(".curl")
- .contains(`-d "${exampleB.serializedValue || exampleB.value}"`)
- })
- it("should use the first example for the media type when changing the media type without prior interactions with the value", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- // Expand the operation
- .get(operationDomId)
- .click()
- // Change the media type
- .get(".opblock-section-request-body .content-type")
- .select(secondaryMediaType)
- // Assert on the static docs value
- .get(`.opblock-section-request-body .microlight`)
- .should("include.text", exampleA.value)
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- // Assert on the Try-It-Out value
- .get(`.opblock-section-request-body textarea`)
- .should("have.value", exampleA.value)
- // Execute the operation
- .get(".execute")
- .click()
- // Assert on the request URL
- // TODO: use an interceptor instead of curl
- .get(".curl")
- .contains(`-d "${exampleA.serializedValue || exampleA.value}"`)
- })
- it("static mode toggling: mediaType -> example -> mediaType -> example", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- // Expand the operation
- .get(operationDomId)
- .click()
- // Change the media type
- .get(".opblock-section-request-body .content-type")
- .select(secondaryMediaType)
- // Assert on the static docs value
- .get(`.opblock-section-request-body .microlight`)
- .should("include.text", exampleA.value)
- // Assert on the dropdown value
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("include.text", exampleA.summary)
- // Choose exampleB
- .get(".opblock-section-request-body .examples-select > select")
- .select(exampleB.key)
- // Assert on the static docs value
- .get(`.opblock-section-request-body .microlight`)
- .should("include.text", exampleB.value)
- // Assert on the dropdown value
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("include.text", exampleB.summary)
- // Change the media type
- .get(".opblock-section-request-body .content-type")
- .select(primaryMediaType)
- // Assert that the static docs value didn't change
- .get(`.opblock-section-request-body .microlight`)
- .should("include.text", exampleB.value)
- // Assert that the dropdown value didn't change
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("include.text", exampleB.summary)
- // Choose exampleA
- .get(".opblock-section-request-body .examples-select > select")
- .select(exampleA.key)
- // Assert on the static docs value
- .get(`.opblock-section-request-body .microlight`)
- .should("include.text", exampleA.value)
- // Assert on the dropdown value
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("include.text", exampleA.summary)
- })
- it("Try-It-Out toggling: mediaType -> example -> mediaType -> example", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- // Expand the operation
- .get(operationDomId)
- .click()
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- // Change the media type
- .get(".opblock-section-request-body .content-type")
- .select(secondaryMediaType)
- // Assert on the static docs value
- .get(`.opblock-section-request-body textarea`)
- .should("include.text", exampleA.value)
- // Assert on the dropdown value
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("include.text", exampleA.summary)
- // Choose exampleB
- .get(".opblock-section-request-body .examples-select > select")
- .select(exampleB.key)
- // Assert on the static docs value
- .get(`.opblock-section-request-body textarea`)
- .should("include.text", exampleB.value)
- // Assert on the dropdown value
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("include.text", exampleB.summary)
- // Change the media type
- .get(".opblock-section-request-body .content-type")
- .select(primaryMediaType)
- // Assert that the static docs value didn't change
- .get(`.opblock-section-request-body textarea`)
- .should("include.text", exampleB.value)
- // Assert that the dropdown value didn't change
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("include.text", exampleB.summary)
- // Choose exampleA
- .get(".opblock-section-request-body .examples-select > select")
- .select(exampleA.key)
- // Assert on the static docs value
- .get(`.opblock-section-request-body textarea`)
- .should("include.text", exampleA.value)
- // Assert on the dropdown value
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("include.text", exampleA.summary)
- })
- it("Try-It-Out toggling and execution with modified values: mediaType -> modified value -> example -> mediaType -> example", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- // Expand the operation
- .get(operationDomId)
- .click()
- // Switch to Try-It-Out
- .get(".try-out__btn")
- .click()
- // Change the media type
- .get(".opblock-section-request-body .content-type")
- .select(secondaryMediaType)
- // Assert on the static docs value
- .get(`.opblock-section-request-body textarea`)
- .should("include.text", exampleA.value)
- // Assert on the dropdown value
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("include.text", exampleA.summary)
- // Modify the value
- .get(`.opblock-section-request-body textarea`)
- .clear()
- .type(customUserInput)
- // Assert on the dropdown value
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("have.text", "[Modified value]")
- // Fire the operation
- .get(".execute")
- .click()
- // Assert on the curl body
- // TODO: use an interceptor instead of curl
- .get(".curl")
- .contains(
- `-d "${customUserInputExpectedCurlSubstring || customUserInput}"`
- )
- // Choose exampleB
- .get(".opblock-section-request-body .examples-select > select")
- .select(exampleB.key)
- // Assert on the static docs value
- .get(`.opblock-section-request-body textarea`)
- .should("include.text", exampleB.value)
- // Assert on the dropdown value
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("include.text", exampleB.summary)
- // Fire the operation
- .get(".execute")
- .click()
- // Assert on the curl body
- // TODO: use an interceptor instead of curl
- .get(".curl")
- .contains(`-d "${exampleB.serializedValue || exampleB.value}"`)
- // Ensure the modified value is still accessible
- .get(".opblock-section-request-body .examples-select > select")
- .contains("[Modified value]")
- // Change the media type to text/plain
- .get(".opblock-section-request-body .content-type")
- .select(primaryMediaType)
- // Assert that the static docs value didn't change
- .get(`.opblock-section-request-body textarea`)
- .should("include.text", exampleB.value)
- // Assert that the dropdown value didn't change
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("include.text", exampleB.summary)
- // Fire the operation
- .get(".execute")
- .click()
- // Assert on the curl body
- // TODO: use an interceptor instead of curl
- .get(".curl")
- .contains(`-d "${exampleB.serializedValue || exampleB.value}"`)
- // Ensure the modified value is still accessible
- .get(".opblock-section-request-body .examples-select > select")
- .contains("[Modified value]")
- // Choose exampleA
- .get(".opblock-section-request-body .examples-select > select")
- .select(exampleA.key)
- // Assert on the static docs value
- .get(`.opblock-section-request-body textarea`)
- .should("include.text", exampleA.value)
- // Assert on the dropdown value
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("include.text", exampleA.summary)
- // Fire the operation
- .get(".execute")
- .click()
- // Assert on the curl body
- // TODO: use an interceptor instead of curl
- .get(".curl")
- .contains(`-d "${exampleA.serializedValue || exampleA.value}"`)
- // Ensure the modified value is still the same value
- .get(".opblock-section-request-body .examples-select > select")
- .select("__MODIFIED__VALUE__")
- // Assert on the static docs value
- .get(`.opblock-section-request-body textarea`)
- .should("have.text", customUserInput.replace(/{{}/g, "{"))
- // Assert on the dropdown value
- .get(".opblock-section-request-body .examples-select > select")
- .find(":selected")
- .should("have.text", "[Modified value]")
- // Fire the operation
- .get(".execute")
- .click()
- // Assert on the curl body
- // TODO: use an interceptor instead of curl
- .get(".curl")
- .contains(
- `-d "${customUserInputExpectedCurlSubstring || customUserInput}"`
- )
- })
- // TODO: Try-It-Out + Try-It-Out media type changes
- }
- function ResponsePrimitiveTestCases({
- operationDomId,
- exampleA, // { value, key, summary }
- exampleB, // { value, key, summary }
- exampleC, // { value, key, summary }
- }) {
- it("should render the first example by default", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- .get(operationDomId)
- .click()
- .get(".responses-wrapper")
- .within(() => {
- cy.get(".examples-select > select")
- .find(":selected")
- .should("include.text", exampleA.summary)
- .get(".microlight")
- .should("include.text", exampleA.value)
- })
- })
- it("should render the second example", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- .get(operationDomId)
- .click()
- .get(".responses-wrapper")
- .within(() => {
- cy.get(".examples-select > select")
- .select(exampleB.key)
- .find(":selected")
- .should("include.text", exampleB.summary)
- .get(".microlight")
- .should("include.text", exampleB.value)
- })
- })
- it("should retain an example choice across media types if they share the same example", () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- .get(operationDomId)
- .click()
- .get(".responses-wrapper")
- .within(() => {
- cy
- // Change examples
- .get(".examples-select > select")
- .select(exampleB.key)
- // Assert against dropdown value
- .find(":selected")
- .should("include.text", exampleB.summary)
- // Assert against example value
- .get(".microlight")
- .should("include.text", exampleB.value)
- // Change media types
- .get(".content-type")
- .select("text/plain+other")
- // Assert against dropdown value
- .get(".examples-select > select")
- .find(":selected")
- .should("include.text", exampleB.summary)
- // Assert against example value
- .get(".microlight")
- .should("include.text", exampleB.value)
- })
- })
- ;(exampleC ? it : it.skip)(
- "should reset to the first example if the new media type lacks the current example",
- () => {
- cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
- .get(operationDomId)
- .click()
- .get(".responses-wrapper")
- .within(() => {
- cy
- // Change media types
- .get(".content-type")
- .select("text/plain+other")
- // Change examples
- .get(".examples-select > select")
- .select(exampleC.key)
- // Assert against dropdown value
- .find(":selected")
- .should("include.text", exampleC.summary || exampleC.key)
- // Assert against example value
- .get(".microlight")
- .should("include.text", exampleC.value)
- // Change media types
- .get(".content-type")
- .select("text/plain")
- // Assert against dropdown value
- .get(".examples-select > select")
- .find(":selected")
- .should("include.text", exampleA.summary)
- // Assert against example value
- .get(".microlight")
- .should("include.text", exampleA.value)
- })
- }
- )
- }
|