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

Fix: UrlInput autoFocus Issue With Custom Block #6197

Merged
merged 2 commits into from
May 10, 2018
Merged

Fix: UrlInput autoFocus Issue With Custom Block #6197

merged 2 commits into from
May 10, 2018

Conversation

yahilmadakiya
Copy link
Contributor

Fix: #6196

Added option to set autofocus true/false. Default can be true.

@danielbachhuber danielbachhuber self-requested a review April 18, 2018 13:09
Copy link
Member

@danielbachhuber danielbachhuber left a comment

Choose a reason for hiding this comment

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

Thanks @yahilmadakiya ! Can you include unit tests for this change?

@yahilmadakiya
Copy link
Contributor Author

Sure @danielbachhuber, I'll try to add unit test.

@yahilmadakiya
Copy link
Contributor Author

@danielbachhuber I have tried a unit test for the changes.

On my research, I came to know that as of now enzyme not providing an option to test for :focus.
Issue link

My unit test 👇

it( 'should not have autofocus if autofocus is false', () => {
	const wrapper = mount( <UrlInputButton autofocus={ false } /> );
	wrapper.setState( { expanded: true } );
	const elem = wrapper.find( 'input[type="text"]' );
	expect( elem.is( ':focus' ) ).toBe( false );
} );

@danielbachhuber
Copy link
Member

@yahilmadakiya Looks fine to me. Can you go ahead and commit the unit test?

@danielbachhuber danielbachhuber requested a review from aduth April 25, 2018 12:11
@yahilmadakiya
Copy link
Contributor Author

@danielbachhuber Above test throw error
Enzyme::Selector does not support pseudo-element or pseudo-class selectors.

Please check my comment here 👉 enzymejs/enzyme#703 (comment)

@@ -180,11 +180,12 @@ class UrlInput extends Component {
render() {
const { value = '', instanceId } = this.props;
const { showSuggestions, posts, selectedSuggestion, loading } = this.state;
const autoFocus = 'undefined' === typeof this.props.autoFocus || this.props.autoFocus;
Copy link
Member

Choose a reason for hiding this comment

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

This is a good use case for destructuring default assignment, just as we're doing with value:

const { value = '', autoFocus = true, instanceId } = this.props;

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Default_values_2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aduth Thanks.

@aduth
Copy link
Member

aduth commented Apr 26, 2018

For your test, could this work?

it( 'should not have autofocus if autofocus is false', () => {
	const wrapper = mount( <UrlInputButton autofocus={ false } /> );
	wrapper.setState( { expanded: true } );
	const elem = wrapper.find( 'input[type="text"]' ).getDOMNode();
	expect( document.activeElement ).toBe( elem );
} );

(I've not tried it, and not sure if JSDOM is capable of assigning activeElement by autofocus)

@yahilmadakiya
Copy link
Contributor Author

yahilmadakiya commented Apr 27, 2018

@aduth I have already tried with activeElement. It was not working as per expectation.

Also, I have tried with the above test which you have provided. The Unit test passed in both case autofocus={ true } and autofocus={ false }.

Thought: activeElement has Working Draft status. So, I dont think we should use that.
https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/activeElement

@aduth
Copy link
Member

aduth commented Apr 30, 2018

That MDN link is unfortunately a bit confusing. I think the working draft status is referring to shadow DOM specifically. It is part of the WHATWG standard:

https://html.spec.whatwg.org/multipage/interaction.html#dom-document-activeelement

The Unit test passed in both case autofocus={ true } and autofocus={ false }.

Could it be that this needs to be corrected to the camel-case form autoFocus ?

@danielbachhuber danielbachhuber self-requested a review May 10, 2018 13:06
Copy link
Member

@danielbachhuber danielbachhuber left a comment

Choose a reason for hiding this comment

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

Given the difficulty of testing this, I think it's fine to land without tests.

@danielbachhuber danielbachhuber added this to the 2.9 milestone May 10, 2018
@danielbachhuber
Copy link
Member

Rebased with master and force-pushed to resolve merge conflict.

@danielbachhuber danielbachhuber merged commit 2b76039 into WordPress:master May 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants