From e317fe47856be6832a6340ae9f2090635b77d190 Mon Sep 17 00:00:00 2001 From: Haz Date: Thu, 6 Feb 2020 08:23:03 -0300 Subject: [PATCH] Lint code --- packages/test-utils/src/blur.js | 6 ++- packages/test-utils/src/click.js | 23 ++++------ packages/test-utils/src/press.js | 42 +++++++++---------- packages/test-utils/src/test/click.js | 4 +- packages/test-utils/src/test/press.js | 27 +++++++++--- packages/test-utils/src/type.js | 15 +++++-- packages/test-utils/src/utils/get-document.js | 4 +- .../test-utils/src/utils/get-next-tabbable.js | 10 +++-- .../src/utils/get-previous-tabbable.js | 10 +++-- 9 files changed, 81 insertions(+), 60 deletions(-) diff --git a/packages/test-utils/src/blur.js b/packages/test-utils/src/blur.js index ead02f37af0154..7ccf5625cb2204 100644 --- a/packages/test-utils/src/blur.js +++ b/packages/test-utils/src/blur.js @@ -16,7 +16,11 @@ export default function blur( element ) { element = getActiveElement(); } - if ( ! element || isBodyElement( element ) || getActiveElement( element ) !== element ) { + if ( + ! element || + isBodyElement( element ) || + getActiveElement( element ) !== element + ) { return; } diff --git a/packages/test-utils/src/click.js b/packages/test-utils/src/click.js index 1a36cc11641611..dce7967da458c3 100644 --- a/packages/test-utils/src/click.js +++ b/packages/test-utils/src/click.js @@ -25,9 +25,9 @@ function getClosestLabel( element ) { * @return {HTMLInputElement|HTMLTextAreaElement|HTMLSelectElement} Input element */ function getInputFromLabel( element ) { - const input = element.htmlFor ? - element.ownerDocument.getElementById( element.htmlFor ) : - element.querySelector( 'input,textarea,select' ); + const input = element.htmlFor + ? element.ownerDocument.getElementById( element.htmlFor ) + : element.querySelector( 'input,textarea,select' ); return input; } @@ -37,11 +37,7 @@ function getInputFromLabel( element ) { * @param {Object} defaultPrevented * @param {Object} options */ -function clickLabel( - element, - defaultPrevented, - options -) { +function clickLabel( element, defaultPrevented, options ) { const input = getInputFromLabel( element ); const isInputDisabled = Boolean( input && input.disabled ); @@ -80,10 +76,7 @@ function setSelected( element, selected ) { * @param {HTMLOptionElement} element * @param {Object} eventOptions */ -function clickOption( - element, - eventOptions -) { +function clickOption( element, eventOptions ) { const select = element.closest( 'select' ); if ( ! select ) { @@ -112,9 +105,9 @@ function clickOption( const elementIndex = options.indexOf( element ); // https://stackoverflow.com/a/16530782/5513909 const referenceOption = select.lastOptionSelectedNotByShiftKey; - const referenceOptionIndex = referenceOption ? - options.indexOf( referenceOption ) : - -1; + const referenceOptionIndex = referenceOption + ? options.indexOf( referenceOption ) + : -1; resetOptions(); // Select options between the reference option and the clicked element diff --git a/packages/test-utils/src/press.js b/packages/test-utils/src/press.js index e7c17ee18d71b2..d54e695b1bd0ab 100644 --- a/packages/test-utils/src/press.js +++ b/packages/test-utils/src/press.js @@ -25,10 +25,7 @@ const clickableInputTypes = [ * @param {HTMLInputElement} element * @param {Object} options */ -function submitFormByPressingEnterOn( - element, - options -) { +function submitFormByPressingEnterOn( element, options ) { const { form } = element; if ( ! form ) { @@ -55,8 +52,7 @@ function submitFormByPressingEnterOn( const submitButton = elements.find( ( el ) => - ( [ 'INPUT', 'BUTTON' ].includes( el.tagName ) ) && - el.type === 'submit' + [ 'INPUT', 'BUTTON' ].includes( el.tagName ) && el.type === 'submit' ); if ( validInputs.length === 1 || submitButton ) { @@ -67,9 +63,9 @@ function submitFormByPressingEnterOn( const keyDownMap = { Tab( element, { shiftKey } ) { const { body } = getDocument( element ); - const nextElement = shiftKey ? - getPreviousTabbable( body ) : - getNextTabbable( body ); + const nextElement = shiftKey + ? getPreviousTabbable( body ) + : getNextTabbable( body ); if ( nextElement ) { focus( nextElement ); } @@ -79,13 +75,13 @@ const keyDownMap = { const nonSubmittableTypes = [ ...clickableInputTypes, 'hidden' ]; const isClickable = - element.tagName === 'BUTTON' || - ( element.tagName === 'INPUT' && - clickableInputTypes.includes( element.type ) ); + element.tagName === 'BUTTON' || + ( element.tagName === 'INPUT' && + clickableInputTypes.includes( element.type ) ); const isSubmittable = - element.tagName === 'INPUT' && - ! nonSubmittableTypes.includes( element.type ); + element.tagName === 'INPUT' && + ! nonSubmittableTypes.includes( element.type ); if ( isClickable ) { fireEvent.click( element, options ); @@ -101,9 +97,9 @@ const keyUpMap = { const spaceableTypes = [ ...clickableInputTypes, 'checkbox', 'radio' ]; const isSpaceable = - element.tagName === 'BUTTON' || - ( element.tagName === 'INPUT' && - spaceableTypes.includes( element.type ) ); + element.tagName === 'BUTTON' || + ( element.tagName === 'INPUT' && + spaceableTypes.includes( element.type ) ); if ( isSpaceable ) { fireEvent.click( element, options ); @@ -118,11 +114,7 @@ const keyUpMap = { * @param {Element} [element] * @param {Object} [options] */ -export default function press( - key, - element, - options = {} -) { +export default function press( key, element, options = {} ) { const document = getDocument( element ); // eslint-disable-next-line eqeqeq @@ -157,7 +149,11 @@ export default function press( fireEvent.keyDown( element, { key, ...options } ); - if ( ! defaultPrevented.current && key in keyDownMap && ! options.metaKey ) { + if ( + ! defaultPrevented.current && + key in keyDownMap && + ! options.metaKey + ) { keyDownMap[ key ]( element, options ); } diff --git a/packages/test-utils/src/test/click.js b/packages/test-utils/src/test/click.js index 2345c4f387dce3..97c6dbe55e3cfe 100644 --- a/packages/test-utils/src/test/click.js +++ b/packages/test-utils/src/test/click.js @@ -19,7 +19,9 @@ describe( 'click', () => { it( 'should not focus button on click when event.preventDefault() was called on mouse down', () => { const { getByText } = render( - + ); const button = getByText( 'button' ); click( button ); diff --git a/packages/test-utils/src/test/press.js b/packages/test-utils/src/test/press.js index 15ec24b1f44654..b8705c03911be4 100644 --- a/packages/test-utils/src/test/press.js +++ b/packages/test-utils/src/test/press.js @@ -20,7 +20,9 @@ describe( 'press', () => { it( 'should click button when pressing enter', () => { const onClick = jest.fn(); - const { getByText } = render( ); + const { getByText } = render( + + ); const button = getByText( 'button' ); expect( onClick ).not.toHaveBeenCalled(); press.Enter( button ); @@ -30,7 +32,10 @@ describe( 'press', () => { it( 'should not click button when pressing enter if event.preventDefault() was called on key down', () => { const onClick = jest.fn(); const { getByText } = render( - ); @@ -42,7 +47,9 @@ describe( 'press', () => { it( 'should click button when pressing space', () => { const onClick = jest.fn(); - const { getByText } = render( ); + const { getByText } = render( + + ); const button = getByText( 'button' ); expect( onClick ).not.toHaveBeenCalled(); press.Space( button ); @@ -52,7 +59,10 @@ describe( 'press', () => { it( 'should not click button when pressing space if event.preventDefault() was called on key down', () => { const onClick = jest.fn(); const { getByText } = render( - ); @@ -65,7 +75,10 @@ describe( 'press', () => { it( 'should not click button when pressing space if event.preventDefault() was called on key up', () => { const onClick = jest.fn(); const { getByText } = render( - ); @@ -151,7 +164,9 @@ describe( 'press', () => { <> span - + ); const button1 = getByText( 'button1' ); diff --git a/packages/test-utils/src/type.js b/packages/test-utils/src/type.js index 7e0b0170271461..8a09a0ca246da3 100644 --- a/packages/test-utils/src/type.js +++ b/packages/test-utils/src/type.js @@ -46,14 +46,23 @@ export default function type( text, element, options = {} ) { for ( const char of text ) { const key = char in charMap ? charMap[ char ] : char; const value = - key in keyMap ? keyMap[ key ]( element, options ) : `${ element.value }${ char }`; + key in keyMap + ? keyMap[ key ]( element, options ) + : `${ element.value }${ char }`; - const defaultPrevented = subscribeDefaultPrevented( element, 'keydown' ); + const defaultPrevented = subscribeDefaultPrevented( + element, + 'keydown' + ); fireEvent.keyDown( element, { key, ...options } ); if ( ! defaultPrevented.current && ! element.readOnly ) { - fireEvent.input( element, { data: char, target: { value }, ...options } ); + fireEvent.input( element, { + data: char, + target: { value }, + ...options, + } ); } fireEvent.keyUp( element, { key, ...options } ); diff --git a/packages/test-utils/src/utils/get-document.js b/packages/test-utils/src/utils/get-document.js index cd13fbf02ec91d..44de903bfdbdcf 100644 --- a/packages/test-utils/src/utils/get-document.js +++ b/packages/test-utils/src/utils/get-document.js @@ -3,7 +3,5 @@ * @return {Document} Document */ export default function getDocument( element ) { - return element ? - element.ownerDocument || window.document : - window.document; + return element ? element.ownerDocument || window.document : window.document; } diff --git a/packages/test-utils/src/utils/get-next-tabbable.js b/packages/test-utils/src/utils/get-next-tabbable.js index 6fa520c8df5a9d..f77239558a3176 100644 --- a/packages/test-utils/src/utils/get-next-tabbable.js +++ b/packages/test-utils/src/utils/get-next-tabbable.js @@ -16,9 +16,11 @@ import './mock-client-rects'; */ export default function getNextTabbable( element ) { const tabbableElements = focus.tabbable.find( getDocument( element ) ); - const currentIndex = tabbableElements.indexOf( getActiveElement( element ) ); + const currentIndex = tabbableElements.indexOf( + getActiveElement( element ) + ); const nextIndex = currentIndex + 1; - return nextIndex >= tabbableElements.length ? - tabbableElements[ 0 ] : - tabbableElements[ nextIndex ]; + return nextIndex >= tabbableElements.length + ? tabbableElements[ 0 ] + : tabbableElements[ nextIndex ]; } diff --git a/packages/test-utils/src/utils/get-previous-tabbable.js b/packages/test-utils/src/utils/get-previous-tabbable.js index 2bd89774dbd33c..d4d2ee416c0fda 100644 --- a/packages/test-utils/src/utils/get-previous-tabbable.js +++ b/packages/test-utils/src/utils/get-previous-tabbable.js @@ -15,9 +15,11 @@ import './mock-client-rects'; */ export default function getPreviousTabbable( element ) { const tabbableElements = focus.tabbable.find( getDocument( element ) ); - const currentIndex = tabbableElements.indexOf( getActiveElement( element ) ); + const currentIndex = tabbableElements.indexOf( + getActiveElement( element ) + ); const previousIndex = currentIndex - 1; - return previousIndex < 0 ? - tabbableElements[ tabbableElements.length - 1 ] : - tabbableElements[ previousIndex ]; + return previousIndex < 0 + ? tabbableElements[ tabbableElements.length - 1 ] + : tabbableElements[ previousIndex ]; }