test: align tests with sample plugin changes

Refs #8577
This commit is contained in:
Vladimir Gorej
2023-05-30 15:29:33 +02:00
committed by Vladimír Gorej
parent 1ce9ce0cda
commit 2ce9d08af7
5 changed files with 306 additions and 138 deletions

View File

@@ -175,7 +175,7 @@ const RequestBody = ({
} }
if (type === "object" || useInitialValue) { if (type === "object" || useInitialValue) {
// TODO: what about example or examples from requestBody could be passed as exampleOverride // TODO: what about example or examples from requestBody could be passed as exampleOverride
initialValue = getSampleSchema(prop, false, { initialValue = fn.getSampleSchema(prop, false, {
includeWriteOnly: true includeWriteOnly: true
}) })
} }
@@ -242,6 +242,7 @@ const RequestBody = ({
requestBody, requestBody,
contentType, contentType,
activeExamplesKey, activeExamplesKey,
fn,
) )
let language = null let language = null
let testValueForJson = getKnownSyntaxHighlighterLanguage(sampleRequestBody) let testValueForJson = getKnownSyntaxHighlighterLanguage(sampleRequestBody)

View File

@@ -103,7 +103,7 @@ export const selectDefaultRequestBodyValue =
} }
export const hasUserEditedBody = onlyOAS3((state, path, method) => (system) => { export const hasUserEditedBody = onlyOAS3((state, path, method) => (system) => {
const { oas3Selectors, specSelectors } = system const { oas3Selectors, specSelectors, fn } = system
let userHasEditedBody = false let userHasEditedBody = false
const currentMediaType = oas3Selectors.requestContentType(path, method) const currentMediaType = oas3Selectors.requestContentType(path, method)
@@ -147,7 +147,8 @@ export const hasUserEditedBody = onlyOAS3((state, path, method) => (system) => {
method, method,
"requestBody", "requestBody",
"requestBody" "requestBody"
) ),
fn
) )
userHasEditedBody = userHasEditedBody =
!!userEditedRequestBody && !!userEditedRequestBody &&

View File

@@ -1,78 +1,128 @@
/**
* @prettier
*/
import React from "react" import React from "react"
import { List, fromJS } from "immutable" import { List, fromJS } from "immutable"
import { render } from "enzyme" import { render } from "enzyme"
import ParameterRow from "components/parameter-row" import ParameterRow from "components/parameter-row"
import {
memoizedSampleFromSchema,
memoizedCreateXMLExample,
} from "core/plugins/samples/fn/index"
import makeGetSampleSchema from "core/plugins/samples/fn/get-sample-schema"
import makeGetJsonSampleSchema from "core/plugins/samples/fn/get-json-sample-schema"
import makeGetYamlSampleSchema from "core/plugins/samples/fn/get-yaml-sample-schema"
import makeGetXmlSampleSchema from "core/plugins/samples/fn/get-xml-sample-schema"
describe("bug #4557: default parameter values", function(){ describe("bug #4557: default parameter values", function () {
it("should apply a Swagger 2.0 default value", function(){ it("should apply a Swagger 2.0 default value", function () {
const paramValue = fromJS({ const paramValue = fromJS({
description: "a pet", description: "a pet",
type: "string", type: "string",
default: "MyDefaultValue" default: "MyDefaultValue",
}) })
const getSystem = () => ({
let props = { getComponent: () => "div",
getComponent: ()=> "div",
specSelectors: { specSelectors: {
security(){}, security() {},
parameterWithMetaByIdentity(){ return paramValue }, parameterWithMetaByIdentity() {
isOAS3(){ return false }, return paramValue
isSwagger2(){ return true } },
isOAS3() {
return false
},
isSwagger2() {
return true
},
}, },
fn: {}, getConfigs: () => {
operation: {get: ()=>{}}, return {}
},
fn: {
memoizedSampleFromSchema,
memoizedCreateXMLExample,
getJsonSampleSchema: makeGetJsonSampleSchema(getSystem),
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
},
})
const props = {
...getSystem(),
operation: { get: () => {} },
onChange: jest.fn(), onChange: jest.fn(),
param: paramValue, param: paramValue,
rawParam: paramValue, rawParam: paramValue,
onChangeConsumes: () => {}, onChangeConsumes: () => {},
pathMethod: [], pathMethod: [],
getConfigs: () => { return {} }, specPath: List([]),
specPath: List([])
} }
render(<ParameterRow {...props}/>) render(<ParameterRow {...props} />)
expect(props.onChange).toHaveBeenCalled() expect(props.onChange).toHaveBeenCalled()
expect(props.onChange).toHaveBeenCalledWith(paramValue, "MyDefaultValue", false) expect(props.onChange).toHaveBeenCalledWith(
paramValue,
"MyDefaultValue",
false
)
}) })
it("should apply an OpenAPI 3.0 default value", function(){ it("should apply an OpenAPI 3.0 default value", function () {
const paramValue = fromJS({ const paramValue = fromJS({
description: "a pet", description: "a pet",
schema: { schema: {
type: "string", type: "string",
default: "MyDefaultValue" default: "MyDefaultValue",
} },
}) })
const getSystem = () => ({
let props = { getComponent: () => "div",
getComponent: ()=> "div",
specSelectors: { specSelectors: {
security(){}, security() {},
parameterWithMetaByIdentity(){ return paramValue }, parameterWithMetaByIdentity() {
isOAS3(){ return true }, return paramValue
isSwagger2() { return false } },
isOAS3() {
return true
},
isSwagger2() {
return false
},
}, },
oas3Selectors: { oas3Selectors: {
activeExamplesMember: () => null activeExamplesMember: () => null,
}, },
fn: {}, getConfigs: () => {
operation: {get: ()=>{}}, return {}
},
fn: {
memoizedSampleFromSchema,
memoizedCreateXMLExample,
getJsonSampleSchema: makeGetJsonSampleSchema(getSystem),
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
},
})
const props = {
...getSystem(),
operation: { get: () => {} },
onChange: jest.fn(), onChange: jest.fn(),
param: paramValue, param: paramValue,
rawParam: paramValue, rawParam: paramValue,
onChangeConsumes: () => {}, onChangeConsumes: () => {},
pathMethod: [], pathMethod: [],
getConfigs: () => { return {} }, specPath: List([]),
specPath: List([])
} }
render(<ParameterRow {...props}/>) render(<ParameterRow {...props} />)
expect(props.onChange).toHaveBeenCalled() expect(props.onChange).toHaveBeenCalled()
expect(props.onChange).toHaveBeenCalledWith(paramValue, "MyDefaultValue", false) expect(props.onChange).toHaveBeenCalledWith(
paramValue,
"MyDefaultValue",
false
)
}) })
}) })

View File

@@ -1,22 +1,48 @@
/**
* @prettier
*/
import React from "react" import React from "react"
import { List, fromJS } from "immutable" import { List, fromJS } from "immutable"
import { render } from "enzyme" import { render } from "enzyme"
import ParameterRow from "components/parameter-row" import ParameterRow from "components/parameter-row"
import {
memoizedSampleFromSchema,
memoizedCreateXMLExample,
} from "core/plugins/samples/fn/index"
import makeGetSampleSchema from "core/plugins/samples/fn/get-sample-schema"
import makeGetJsonSampleSchema from "core/plugins/samples/fn/get-json-sample-schema"
import makeGetYamlSampleSchema from "core/plugins/samples/fn/get-yaml-sample-schema"
import makeGetXmlSampleSchema from "core/plugins/samples/fn/get-xml-sample-schema"
describe("<ParameterRow/>", () => { describe("<ParameterRow/>", () => {
const createProps = ({ param, isOAS3 }) => ({ const createProps = ({ param, isOAS3 }) => {
getComponent: () => "div", const getSystem = () => ({
specSelectors: { getComponent: () => "div",
parameterWithMetaByIdentity: () => param, specSelectors: {
isOAS3: () => isOAS3, parameterWithMetaByIdentity: () => param,
isSwagger2: () => !isOAS3 isOAS3: () => isOAS3,
}, isSwagger2: () => !isOAS3,
oas3Selectors: { activeExamplesMember: () => {} }, },
param, fn: {
rawParam: param, memoizedSampleFromSchema,
pathMethod: [], memoizedCreateXMLExample,
getConfigs: () => ({}) getJsonSampleSchema: makeGetJsonSampleSchema(getSystem),
}) getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
},
oas3Selectors: { activeExamplesMember: () => {} },
getConfigs: () => ({}),
})
return {
...getSystem(),
param,
rawParam: param,
pathMethod: [],
}
}
it("Can render Swagger 2 parameter type with format", () => { it("Can render Swagger 2 parameter type with format", () => {
const param = fromJS({ const param = fromJS({
@@ -24,11 +50,11 @@ describe("<ParameterRow/>", () => {
in: "path", in: "path",
description: "UUID that identifies a pet", description: "UUID that identifies a pet",
type: "string", type: "string",
format: "uuid" format: "uuid",
}) })
const props = createProps({ param, isOAS3: false }) const props = createProps({ param, isOAS3: false })
const wrapper = render(<ParameterRow {...props}/>) const wrapper = render(<ParameterRow {...props} />)
expect(wrapper.find(".parameter__type").length).toEqual(1) expect(wrapper.find(".parameter__type").length).toEqual(1)
expect(wrapper.find(".parameter__type").text()).toEqual("string($uuid)") expect(wrapper.find(".parameter__type").text()).toEqual("string($uuid)")
@@ -39,11 +65,11 @@ describe("<ParameterRow/>", () => {
name: "petId", name: "petId",
in: "path", in: "path",
description: "ID that identifies a pet", description: "ID that identifies a pet",
type: "string" type: "string",
}) })
const props = createProps({ param, isOAS3: false }) const props = createProps({ param, isOAS3: false })
const wrapper = render(<ParameterRow {...props}/>) const wrapper = render(<ParameterRow {...props} />)
expect(wrapper.find(".parameter__type").length).toEqual(1) expect(wrapper.find(".parameter__type").length).toEqual(1)
expect(wrapper.find(".parameter__type").text()).toEqual("string") expect(wrapper.find(".parameter__type").text()).toEqual("string")
@@ -54,11 +80,11 @@ describe("<ParameterRow/>", () => {
name: "hasId", name: "hasId",
in: "path", in: "path",
description: "boolean value to indicate if the pet has an id", description: "boolean value to indicate if the pet has an id",
type: "boolean" type: "boolean",
}) })
const props = createProps({ param, isOAS3: false }) const props = createProps({ param, isOAS3: false })
const wrapper = render(<ParameterRow {...props}/>) const wrapper = render(<ParameterRow {...props} />)
expect(wrapper.find(".parameter__type").length).toEqual(1) expect(wrapper.find(".parameter__type").length).toEqual(1)
expect(wrapper.find(".parameter__type").text()).toEqual("boolean") expect(wrapper.find(".parameter__type").text()).toEqual("boolean")
@@ -71,12 +97,12 @@ describe("<ParameterRow/>", () => {
description: "UUID that identifies a pet", description: "UUID that identifies a pet",
schema: { schema: {
type: "string", type: "string",
format: "uuid" format: "uuid",
} },
}) })
const props = createProps({ param, isOAS3: true }) const props = createProps({ param, isOAS3: true })
const wrapper = render(<ParameterRow {...props}/>) const wrapper = render(<ParameterRow {...props} />)
expect(wrapper.find(".parameter__type").length).toEqual(1) expect(wrapper.find(".parameter__type").length).toEqual(1)
expect(wrapper.find(".parameter__type").text()).toEqual("string($uuid)") expect(wrapper.find(".parameter__type").text()).toEqual("string($uuid)")
@@ -88,12 +114,12 @@ describe("<ParameterRow/>", () => {
in: "path", in: "path",
description: "ID that identifies a pet", description: "ID that identifies a pet",
schema: { schema: {
type: "string" type: "string",
} },
}) })
const props = createProps({ param, isOAS3: true }) const props = createProps({ param, isOAS3: true })
const wrapper = render(<ParameterRow {...props}/>) const wrapper = render(<ParameterRow {...props} />)
expect(wrapper.find(".parameter__type").length).toEqual(1) expect(wrapper.find(".parameter__type").length).toEqual(1)
expect(wrapper.find(".parameter__type").text()).toEqual("string") expect(wrapper.find(".parameter__type").text()).toEqual("string")
@@ -105,12 +131,12 @@ describe("<ParameterRow/>", () => {
in: "path", in: "path",
description: "boolean value to indicate if the pet has an id", description: "boolean value to indicate if the pet has an id",
schema: { schema: {
type: "boolean" type: "boolean",
} },
}) })
const props = createProps({ param, isOAS3: true }) const props = createProps({ param, isOAS3: true })
const wrapper = render(<ParameterRow {...props}/>) const wrapper = render(<ParameterRow {...props} />)
expect(wrapper.find(".parameter__type").length).toEqual(1) expect(wrapper.find(".parameter__type").length).toEqual(1)
expect(wrapper.find(".parameter__type").text()).toEqual("boolean") expect(wrapper.find(".parameter__type").text()).toEqual("boolean")
@@ -122,26 +148,42 @@ describe("bug #5573: zero default and example values", function () {
const paramValue = fromJS({ const paramValue = fromJS({
description: "a pet", description: "a pet",
type: "integer", type: "integer",
default: 0 default: 0,
}) })
const getSystem = () => ({
let props = {
getComponent: () => "div", getComponent: () => "div",
specSelectors: { specSelectors: {
security() { }, security() {},
parameterWithMetaByIdentity() { return paramValue }, parameterWithMetaByIdentity() {
isOAS3() { return false }, return paramValue
isSwagger2() { return true } },
isOAS3() {
return false
},
isSwagger2() {
return true
},
}, },
fn: {}, fn: {
operation: { get: () => { } }, memoizedSampleFromSchema,
memoizedCreateXMLExample,
getJsonSampleSchema: makeGetJsonSampleSchema(getSystem),
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
},
getConfigs: () => {
return {}
},
})
const props = {
...getSystem(),
onChange: jest.fn(), onChange: jest.fn(),
param: paramValue, param: paramValue,
rawParam: paramValue, rawParam: paramValue,
onChangeConsumes: () => { }, onChangeConsumes: () => {},
pathMethod: [], pathMethod: [],
getConfigs: () => { return {} }, specPath: List([]),
specPath: List([])
} }
render(<ParameterRow {...props} />) render(<ParameterRow {...props} />)
@@ -154,27 +196,44 @@ describe("bug #5573: zero default and example values", function () {
description: "a pet", description: "a pet",
type: "integer", type: "integer",
schema: { schema: {
example: 0 example: 0,
} },
}) })
const getSystem = () => ({
let props = {
getComponent: () => "div", getComponent: () => "div",
specSelectors: { specSelectors: {
security() { }, security() {},
parameterWithMetaByIdentity() { return paramValue }, parameterWithMetaByIdentity() {
isOAS3() { return false }, return paramValue
isSwagger2() { return true } },
isOAS3() {
return false
},
isSwagger2() {
return true
},
}, },
fn: {}, getConfigs: () => {
operation: { get: () => { } }, return {}
},
fn: {
memoizedSampleFromSchema,
memoizedCreateXMLExample,
getJsonSampleSchema: makeGetJsonSampleSchema(getSystem),
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
},
})
const props = {
...getSystem(),
operation: { get: () => {} },
onChange: jest.fn(), onChange: jest.fn(),
param: paramValue, param: paramValue,
rawParam: paramValue, rawParam: paramValue,
onChangeConsumes: () => { }, onChangeConsumes: () => {},
pathMethod: [], pathMethod: [],
getConfigs: () => { return {} }, specPath: List([]),
specPath: List([])
} }
render(<ParameterRow {...props} />) render(<ParameterRow {...props} />)
@@ -187,30 +246,47 @@ describe("bug #5573: zero default and example values", function () {
description: "a pet", description: "a pet",
schema: { schema: {
type: "integer", type: "integer",
default: 0 default: 0,
} },
}) })
const getSystem = () => ({
let props = {
getComponent: () => "div", getComponent: () => "div",
specSelectors: { specSelectors: {
security() { }, security() {},
parameterWithMetaByIdentity() { return paramValue }, parameterWithMetaByIdentity() {
isOAS3() { return true }, return paramValue
isSwagger2() { return false } },
isOAS3() {
return true
},
isSwagger2() {
return false
},
}, },
oas3Selectors: { oas3Selectors: {
activeExamplesMember: () => null activeExamplesMember: () => null,
}, },
fn: {}, getConfigs: () => {
operation: { get: () => { } }, return {}
},
fn: {
memoizedSampleFromSchema,
memoizedCreateXMLExample,
getJsonSampleSchema: makeGetJsonSampleSchema(getSystem),
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
},
})
const props = {
...getSystem(),
operation: { get: () => {} },
onChange: jest.fn(), onChange: jest.fn(),
param: paramValue, param: paramValue,
rawParam: paramValue, rawParam: paramValue,
onChangeConsumes: () => { }, onChangeConsumes: () => {},
pathMethod: [], pathMethod: [],
getConfigs: () => { return {} }, specPath: List([]),
specPath: List([])
} }
render(<ParameterRow {...props} />) render(<ParameterRow {...props} />)
@@ -223,30 +299,47 @@ describe("bug #5573: zero default and example values", function () {
description: "a pet", description: "a pet",
schema: { schema: {
type: "integer", type: "integer",
example: 0 example: 0,
} },
}) })
const getSystem = () => ({
let props = {
getComponent: () => "div", getComponent: () => "div",
specSelectors: { specSelectors: {
security() { }, security() {},
parameterWithMetaByIdentity() { return paramValue }, parameterWithMetaByIdentity() {
isOAS3() { return true }, return paramValue
isSwagger2() { return false } },
isOAS3() {
return true
},
isSwagger2() {
return false
},
}, },
oas3Selectors: { oas3Selectors: {
activeExamplesMember: () => null activeExamplesMember: () => null,
}, },
fn: {}, getConfigs: () => {
operation: { get: () => { } }, return {}
},
fn: {
memoizedSampleFromSchema,
memoizedCreateXMLExample,
getJsonSampleSchema: makeGetJsonSampleSchema(getSystem),
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
},
})
const props = {
...getSystem(),
operation: { get: () => {} },
onChange: jest.fn(), onChange: jest.fn(),
param: paramValue, param: paramValue,
rawParam: paramValue, rawParam: paramValue,
onChangeConsumes: () => { }, onChangeConsumes: () => {},
pathMethod: [], pathMethod: [],
getConfigs: () => { return {} }, specPath: List([]),
specPath: List([])
} }
render(<ParameterRow {...props} />) render(<ParameterRow {...props} />)

View File

@@ -1,9 +1,21 @@
/**
* @prettier
*/
import React from "react" import React from "react"
import { shallow } from "enzyme" import { shallow } from "enzyme"
import { fromJS, List } from "immutable" import { fromJS, List } from "immutable"
import Response from "components/response" import Response from "components/response"
import ModelExample from "components/model-example" import ModelExample from "components/model-example"
import { inferSchema } from "corePlugins/samples/fn" import {
inferSchema,
memoizedSampleFromSchema,
memoizedCreateXMLExample,
} from "core/plugins/samples/fn/index"
import makeGetSampleSchema from "core/plugins/samples/fn/get-sample-schema"
import makeGetJsonSampleSchema from "core/plugins/samples/fn/get-json-sample-schema"
import makeGetYamlSampleSchema from "core/plugins/samples/fn/get-yaml-sample-schema"
import makeGetXmlSampleSchema from "core/plugins/samples/fn/get-xml-sample-schema"
describe("<Response />", function () { describe("<Response />", function () {
const dummyComponent = () => null const dummyComponent = () => null
@@ -13,21 +25,30 @@ describe("<Response />", function () {
modelExample: ModelExample, modelExample: ModelExample,
Markdown: dummyComponent, Markdown: dummyComponent,
operationLink: dummyComponent, operationLink: dummyComponent,
contentType: dummyComponent contentType: dummyComponent,
} }
const props = { const getSystem = () => ({
getComponent: c => components[c], getComponent: (c) => components[c],
getConfigs: () => { getConfigs: () => {
return {} return {}
}, },
specSelectors: { specSelectors: {
isOAS3() { isOAS3() {
return false return false
} },
}, },
fn: { fn: {
inferSchema inferSchema,
memoizedSampleFromSchema,
memoizedCreateXMLExample,
getJsonSampleSchema: makeGetJsonSampleSchema(getSystem),
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
}, },
})
const props = {
...getSystem(),
contentType: "application/json", contentType: "application/json",
className: "for-test", className: "for-test",
specPath: List(), specPath: List(),
@@ -36,19 +57,19 @@ describe("<Response />", function () {
type: "object", type: "object",
properties: { properties: {
// Note reverse order: c, b, a // Note reverse order: c, b, a
"c": { c: {
type: "integer" type: "integer",
}, },
"b": { b: {
type: "boolean" type: "boolean",
}, },
"a": { a: {
type: "string" type: "string",
} },
} },
} },
}), }),
code: "200" code: "200",
} }
it("renders the model-example schema properties in order", function () { it("renders the model-example schema properties in order", function () {
@@ -57,7 +78,9 @@ describe("<Response />", function () {
expect(renderedModelExample.length).toEqual(1) expect(renderedModelExample.length).toEqual(1)
// Assert the schema's properties have maintained their order // Assert the schema's properties have maintained their order
const modelExampleSchemaProperties = renderedModelExample.props().schema.toJS().properties const modelExampleSchemaProperties = renderedModelExample
.props()
.schema.toJS().properties
expect(Object.keys(modelExampleSchemaProperties)).toEqual(["c", "b", "a"]) expect(Object.keys(modelExampleSchemaProperties)).toEqual(["c", "b", "a"])
}) })
}) })