Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/higher-order/with-focus-return/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function withFocusReturn( WrappedComponent ) {
if (
this.activeElement && (
( document.activeElement && wrapper && wrapper.contains( document.activeElement ) ) ||
! document.activeElement
( ! document.activeElement || document.body === document.activeElement )
Copy link
Member

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.activeElement at all?

Copy link
Contributor Author

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.activeElement would be falsy. I think keeping it in is okay. Adds to the readability a bit in my opinion as well.

)
) {
this.activeElement.focus();
Expand Down
77 changes: 77 additions & 0 deletions components/higher-order/with-focus-return/test/index.js
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', () => {
Copy link
Member

Choose a reason for hiding this comment

The 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 );
} );
} );
} );