Fix global state issues with Schemes component

This commit is contained in:
Kyle Shockey
2017-08-09 17:06:48 -07:00
parent be293de220
commit 9dee2daa14
3 changed files with 38 additions and 4 deletions

View File

@@ -69,7 +69,10 @@ export default class BaseLayout extends React.Component {
<div className="scheme-container"> <div className="scheme-container">
<Col className="schemes wrapper" mobile={12}> <Col className="schemes wrapper" mobile={12}>
{ schemes && schemes.size ? ( { schemes && schemes.size ? (
<Schemes schemes={ schemes } specActions={ specActions } /> <Schemes
operationScheme={specSelectors.operationScheme()}
schemes={ schemes }
specActions={ specActions } />
) : null } ) : null }
{ securityDefinitions ? ( { securityDefinitions ? (

View File

@@ -19,7 +19,7 @@ export default class Schemes extends React.Component {
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
if ( !this.props.operationScheme || !nextProps.schemes.has(this.props.operationScheme) ) { if ( !this.props.operationScheme || !nextProps.schemes.includes(this.props.operationScheme) ) {
// if we don't have a selected operationScheme or if our selected scheme is no longer an option, // if we don't have a selected operationScheme or if our selected scheme is no longer an option,
// then fire 'change' event and select the first scheme in the list of options // then fire 'change' event and select the first scheme in the list of options
this.setScheme(nextProps.schemes.first()) this.setScheme(nextProps.schemes.first())

View File

@@ -9,10 +9,12 @@ import Schemes from "components/schemes"
describe("<Schemes/>", function(){ describe("<Schemes/>", function(){
it("calls props.specActions.setScheme() when no operationScheme is selected", function(){ it("calls props.specActions.setScheme() when no operationScheme is selected", function(){
let setSchemeSpy = createSpy()
// Given // Given
let props = { let props = {
specActions: { specActions: {
setScheme: createSpy() setScheme: setSchemeSpy
}, },
schemes: fromJS([ schemes: fromJS([
"http", "http",
@@ -38,4 +40,33 @@ describe("<Schemes/>", function(){
// Then operationScheme should default to first scheme in options list // Then operationScheme should default to first scheme in options list
expect(props.specActions.setScheme).toHaveBeenCalledWith("https", "/test", "get") expect(props.specActions.setScheme).toHaveBeenCalledWith("https", "/test", "get")
}) })
it.only("doesn't call props.specActions.setScheme() when schemes hasn't changed", function(){
let setSchemeSpy = createSpy()
// Given
let props = {
specActions: {
setScheme: setSchemeSpy
},
schemes: fromJS([
"http",
"https"
]),
operationScheme: "https"
}
// When
let wrapper = shallow(<Schemes {...props}/>)
// Should be called initially, to set the global state
expect(setSchemeSpy.calls.length).toEqual(1)
// After an update
wrapper.instance().componentWillReceiveProps(props)
// Should not be called again, since `operationScheme` is in schemes
expect(setSchemeSpy.calls.length).toEqual(1)
})
}) })