Skip to content

Commit

Permalink
Fix for bug with toggling opensInNewWindow state (#17794)
Browse files Browse the repository at this point in the history
* Update getDerivedStateFromProps to pass back all state property changes

* Add test to check that state.opensInNewWindow is set correctly on prop change

* Rename test file
  • Loading branch information
glendaviesnz authored and gziolo committed Oct 15, 2019
1 parent 1e9685b commit eecf130
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/format-library/src/link/inline.js
Expand Up @@ -89,13 +89,15 @@ class InlineLinkUI extends Component {
const opensInNewWindow = target === '_blank';

if ( ! isShowingInput( props, state ) ) {
const update = {};
if ( url !== state.inputValue ) {
return { inputValue: url };
update.inputValue = url;
}

if ( opensInNewWindow !== state.opensInNewWindow ) {
return { opensInNewWindow };
update.opensInNewWindow = opensInNewWindow;
}
return Object.keys( update ).length ? update : null;
}

return null;
Expand Down
38 changes: 38 additions & 0 deletions packages/format-library/src/link/test/inline.js
@@ -0,0 +1,38 @@
/**
* Internal dependencies
*/
import InlineLinkUI from '../inline';
/**
* External dependencies
*/
import { shallow } from 'enzyme';

describe( 'InlineLinkUI', () => {
it( 'InlineLinkUI renders', () => {
const wrapper = shallow(
<InlineLinkUI />
);
expect( wrapper ).toBeTruthy();
} );

it( 'should set state.opensInNewWindow to false by default', () => {
const wrapper = shallow(
<InlineLinkUI activeAttributes={ {} } />
).dive();

expect( wrapper.state( 'opensInNewWindow' ) ).toEqual( false );
} );

it( 'should set state.opensInNewWindow to true if props.activeAttributes.target is _blank', () => {
const givenProps = {
addingLink: false,
activeAttributes: { url: 'http://www.google.com', target: '_blank' },
};

const wrapper = shallow(
<InlineLinkUI activeAttributes={ {} } />
).dive();
wrapper.setProps( givenProps );
expect( wrapper.state( 'opensInNewWindow' ) ).toEqual( true );
} );
} );

0 comments on commit eecf130

Please sign in to comment.