Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for the UrlInputButton component. #3550

Merged
merged 8 commits into from Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion blocks/url-input/button.js
Expand Up @@ -59,7 +59,7 @@ class UrlInputButton extends Component {
label={ __( 'Close' ) }
onClick={ this.toggle }
/>
<UrlInput value={ url || '' } onChange={ onChange } />
<UrlInput value={ url || '' } onChange={ onChange } data-test="UrlInput" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be affecting the output of the original component just to satisfy needs of unit testing. We could have achieved the same with wrapper.find( UrlInput )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agreed. It was a really bad idea which would only work if we would be able to provide Babel plugin which removes those data-test attributes ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #7981

<IconButton
icon="editor-break"
label={ __( 'Submit' ) }
Expand Down
67 changes: 67 additions & 0 deletions blocks/url-input/test/button.js
@@ -0,0 +1,67 @@
/**
* External dependencies
*/
import { shallow, mount } from 'enzyme';

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same section is already defined above and it doesn't contain any import statements. Let's remove this obsolete comment.

* Internal dependencies
*/
import UrlInputButton from '../button';

describe( 'UrlInputButton', () => {
const clickEditLink = ( wrapper ) => wrapper.find( 'IconButton.components-toolbar__control' ).simulate( 'click' );

it( 'should have a valid class name in the wrapper tag', () => {
const wrapper = shallow( <UrlInputButton /> );
expect( wrapper.hasClass( 'blocks-url-input__button' ) ).toBe( true );
} );
it( 'should not have is-active class when url prop not defined', () => {
const wrapper = shallow( <UrlInputButton /> );
expect( wrapper.find( 'IconButton' ).hasClass( 'is-active' ) ).toBe( false );
} );
it( 'should have is-active class name if url prop defined', () => {
const wrapper = shallow( <UrlInputButton url="https://example.com" /> );
expect( wrapper.find( 'IconButton' ).hasClass( 'is-active' ) ).toBe( true );
} );
it( 'should have hidden form by default', () => {
const wrapper = shallow( <UrlInputButton /> );
expect( wrapper.find( 'form' ).length ).toBe( 0 );
expect( wrapper.state().expanded ).toBe( false );
} );
it( 'should have visible form when Edit Link button clicked', () => {
const wrapper = shallow( <UrlInputButton /> );
clickEditLink( wrapper );
expect( wrapper.find( 'form' ).length ).toBe( 1 );
expect( wrapper.state().expanded ).toBe( true );
} );
it( 'should call onChange function once when value changes once', () => {
const onChangeMock = jest.fn();
const wrapper = shallow( <UrlInputButton onChange={ onChangeMock } /> );
clickEditLink( wrapper );
wrapper.find( '[data-test=\'UrlInput\']' ).simulate( 'change' );
expect( onChangeMock ).toHaveBeenCalledTimes( 1 );
} );
it( 'should call onChange function twice when value changes twice', () => {
const onChangeMock = jest.fn();
const wrapper = shallow( <UrlInputButton onChange={ onChangeMock } /> );
clickEditLink( wrapper );
wrapper.find( '[data-test=\'UrlInput\']' ).simulate( 'change' );
wrapper.find( '[data-test=\'UrlInput\']' ).simulate( 'change' );
expect( onChangeMock ).toHaveBeenCalledTimes( 2 );
} );
it( 'should close the form when user clicks Close button', () => {
const wrapper = shallow( <UrlInputButton /> );
clickEditLink( wrapper );
expect( wrapper.state().expanded ).toBe( true );
wrapper.find( '.blocks-url-input__back' ).simulate( 'click' );
expect( wrapper.state().expanded ).toBe( false );
} );
it( 'should close the form when user submits it', () => {
const wrapper = mount( <UrlInputButton /> );
clickEditLink( wrapper );
expect( wrapper.state().expanded ).toBe( true );
wrapper.find( 'form' ).simulate( 'submit' );
expect( wrapper.state().expanded ).toBe( false );
wrapper.unmount();
} );
} );