-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add tests to withFocusReturn HOC. #931
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,77 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { expect } from 'chai'; | ||
| import { shallow, mount } from 'enzyme'; | ||
| import { Component } from '../../../../element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import withFocusReturn from '../'; | ||
|
|
||
| class Test extends Component { | ||
| render() { | ||
| return ( | ||
| <div className="test">Testing</div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| describe( 'withFocusReturn()', () => { | ||
| describe( 'testing rendering and focus handling', () => { | ||
| const Composite = withFocusReturn( Test ); | ||
| const activeElement = document.createElement( 'button' ); | ||
| const switchFocusTo = document.createElement( 'input' ); | ||
|
|
||
| beforeEach( () => { | ||
| activeElement.focus(); | ||
| } ); | ||
|
|
||
| afterEach( () => { | ||
| activeElement.blur(); | ||
| } ); | ||
|
|
||
| it( 'should render a basic Test component inside the HOC', () => { | ||
| const renderedComposite = shallow( <Composite /> ); | ||
| const wrappedElement = renderedComposite.find( 'Test' ); | ||
| const wrappedElementShallow = wrappedElement.shallow(); | ||
| expect( wrappedElementShallow.hasClass( 'test' ) ).to.be.true(); | ||
| expect( wrappedElementShallow.type() ).to.equal( 'div' ); | ||
| expect( wrappedElementShallow.text() ).to.equal( 'Testing' ); | ||
| } ); | ||
|
|
||
| it( 'should pass additional props through to the wrapped element', () => { | ||
| const renderedComposite = shallow( <Composite test="test" /> ); | ||
| const wrappedElement = renderedComposite.find( 'Test' ); | ||
| // Ensure that the wrapped Test element has the appropriate props. | ||
| expect( wrappedElement.props().test ).to.equal( 'test' ); | ||
| } ); | ||
|
|
||
| it( 'should not switch focus back to the bound focus element', () => { | ||
| const mountedComposite = mount( <Composite /> ); | ||
| expect( mountedComposite.instance().activeElement ).to.equal( activeElement ); | ||
|
|
||
| // Change activeElement. | ||
| switchFocusTo.focus(); | ||
| expect( document.activeElement ).to.equal( switchFocusTo ); | ||
|
|
||
| // Should keep focus on switchFocusTo, because it is not within HOC. | ||
| mountedComposite.unmount(); | ||
| expect( document.activeElement ).to.equal( switchFocusTo ); | ||
| } ); | ||
|
|
||
| it( 'should return focus to element associated with HOC', () => { | ||
|
Member
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. This one reads well 👍 |
||
| const mountedComposite = mount( <Composite /> ); | ||
| expect( mountedComposite.instance().activeElement ).to.equal( activeElement ); | ||
|
|
||
| // Change activeElement. | ||
| document.activeElement.blur(); | ||
| expect( document.activeElement ).to.equal( document.body ); | ||
|
|
||
| // Should return to the activeElement saved with this component. | ||
| mountedComposite.unmount(); | ||
| expect( document.activeElement ).to.equal( activeElement ); | ||
| } ); | ||
| } ); | ||
| } ); | ||
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.
In this case I wonder if we need
! document.activeElementat all?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.
Yeah, there might be some possibility of this existing somehow, at one point JSDOM would not handle this to the HTML spec, so
! document.activeElementwould be falsy. I think keeping it in is okay. Adds to the readability a bit in my opinion as well.