-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
gziolo
merged 8 commits into
WordPress:master
from
hideokamoto:add/test/url-input/button
Nov 21, 2017
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fcfd888
Add UrlInputButton unit test
hideokamoto 9085014
UrlInputButton unit test (change / click / submit event support)
hideokamoto 96df57e
fix test script
hideokamoto 8d47323
update test for pr-#3550
hideokamoto 175bf6a
fix lint error
hideokamoto 3234b52
update url-input button test
hideokamoto 8f61a40
Testing: Refine test descriptions for UrlInput
gziolo ebfa9c3
Testing: Fix Eslint issue with trailing spaces
gziolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow, mount } from 'enzyme'; | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} ); | ||
} ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 )
There was a problem hiding this comment.
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 ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #7981