Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/core/components/parameters/parameters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,15 @@ export default class Parameters extends Component {

onChangeMediaType = ({ value, pathMethod }) => {
let { specActions, oas3Selectors, oas3Actions } = this.props
const userHasEditedBody = oas3Selectors.hasUserEditedBody(...pathMethod)
const shouldRetainRequestBodyValue = oas3Selectors.shouldRetainRequestBodyValue(...pathMethod)
oas3Actions.setRequestContentType({ value, pathMethod })
oas3Actions.initRequestBodyValidateError({ pathMethod })
if (!userHasEditedBody) {
if(!shouldRetainRequestBodyValue) {
oas3Actions.setRequestBodyValue({ value: undefined, pathMethod })
}
specActions.clearResponse(...pathMethod)
specActions.clearRequest(...pathMethod)
specActions.clearValidateParams(pathMethod)
if(!shouldRetainRequestBodyValue) {
oas3Actions.setRequestBodyValue({ value: undefined, pathMethod })
}
specActions.clearResponse(...pathMethod)
specActions.clearRequest(...pathMethod)
specActions.clearValidateParams(pathMethod)
}

render() {
Expand Down
130 changes: 130 additions & 0 deletions test/unit/components/parameters.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* @prettier
*/
import React from "react"
import { shallow } from "enzyme"
import { List, fromJS } from "immutable"
import Parameters from "core/components/parameters/parameters"

describe("<Parameters/>", function () {
describe("onChangeMediaType", function () {
it("should reset request body value when content type changes even if user has edited body", function () {
const setRequestBodyValue = jest.fn()
const setRequestContentType = jest.fn()
const initRequestBodyValidateError = jest.fn()
const clearResponse = jest.fn()
const clearRequest = jest.fn()
const clearValidateParams = jest.fn()

const props = {
parameters: List(),
operation: fromJS({ operationId: "testOp" }),
specActions: {
changeParamByIdentity: jest.fn(),
clearResponse,
clearRequest,
clearValidateParams,
},
oas3Actions: {
setRequestContentType,
setRequestBodyValue,
initRequestBodyValidateError,
},
oas3Selectors: {
hasUserEditedBody: () => true,
shouldRetainRequestBodyValue: () => false,
requestContentType: () => "application/json",
requestBodyValue: () => '{"foo":"bar"}',
activeExamplesMember: () => null,
},
specSelectors: {
isOAS3: () => true,
specResolvedSubtree: () => fromJS({}),
},
getComponent: () => "div",
getConfigs: () => ({}),
fn: {},
pathMethod: ["/pet", "put"],
specPath: List(["paths", "/pet", "put"]),
}

const wrapper = shallow(<Parameters {...props} />)
const instance = wrapper.instance()

instance.onChangeMediaType({
value: "application/xml",
pathMethod: ["/pet", "put"],
})

expect(setRequestContentType).toHaveBeenCalledWith({
value: "application/xml",
pathMethod: ["/pet", "put"],
})
expect(setRequestBodyValue).toHaveBeenCalledWith({
value: undefined,
pathMethod: ["/pet", "put"],
})
expect(clearResponse).toHaveBeenCalledWith("/pet", "put")
expect(clearRequest).toHaveBeenCalledWith("/pet", "put")
expect(clearValidateParams).toHaveBeenCalledWith(["/pet", "put"])
})

it("should not reset request body value when shouldRetainRequestBodyValue is true", function () {
const setRequestBodyValue = jest.fn()
const setRequestContentType = jest.fn()
const initRequestBodyValidateError = jest.fn()
const clearResponse = jest.fn()
const clearRequest = jest.fn()
const clearValidateParams = jest.fn()

const props = {
parameters: List(),
operation: fromJS({ operationId: "testOp" }),
specActions: {
changeParamByIdentity: jest.fn(),
clearResponse,
clearRequest,
clearValidateParams,
},
oas3Actions: {
setRequestContentType,
setRequestBodyValue,
initRequestBodyValidateError,
},
oas3Selectors: {
hasUserEditedBody: () => true,
shouldRetainRequestBodyValue: () => true,
requestContentType: () => "application/json",
requestBodyValue: () => '{"foo":"bar"}',
activeExamplesMember: () => null,
},
specSelectors: {
isOAS3: () => true,
specResolvedSubtree: () => fromJS({}),
},
getComponent: () => "div",
getConfigs: () => ({}),
fn: {},
pathMethod: ["/pet", "put"],
specPath: List(["paths", "/pet", "put"]),
}

const wrapper = shallow(<Parameters {...props} />)
const instance = wrapper.instance()

instance.onChangeMediaType({
value: "application/xml",
pathMethod: ["/pet", "put"],
})

expect(setRequestContentType).toHaveBeenCalledWith({
value: "application/xml",
pathMethod: ["/pet", "put"],
})
expect(setRequestBodyValue).not.toHaveBeenCalled()
expect(clearResponse).toHaveBeenCalledWith("/pet", "put")
expect(clearRequest).toHaveBeenCalledWith("/pet", "put")
expect(clearValidateParams).toHaveBeenCalledWith(["/pet", "put"])
})
})
})