Skip to content

Commit

Permalink
Lint code
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohaz committed Feb 6, 2020
1 parent f1d05fe commit e317fe4
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 60 deletions.
6 changes: 5 additions & 1 deletion packages/test-utils/src/blur.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
23 changes: 8 additions & 15 deletions packages/test-utils/src/click.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 );

Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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
Expand Down
42 changes: 19 additions & 23 deletions packages/test-utils/src/press.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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 ) {
Expand All @@ -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 );
}
Expand All @@ -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 );
Expand All @@ -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 );
Expand All @@ -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
Expand Down Expand Up @@ -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 );
}

Expand Down
4 changes: 3 additions & 1 deletion packages/test-utils/src/test/click.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ describe( 'click', () => {

it( 'should not focus button on click when event.preventDefault() was called on mouse down', () => {
const { getByText } = render(
<button onMouseDown={ ( event ) => event.preventDefault() }>button</button>
<button onMouseDown={ ( event ) => event.preventDefault() }>
button
</button>
);
const button = getByText( 'button' );
click( button );
Expand Down
27 changes: 21 additions & 6 deletions packages/test-utils/src/test/press.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ describe( 'press', () => {

it( 'should click button when pressing enter', () => {
const onClick = jest.fn();
const { getByText } = render( <button onClick={ onClick }>button</button> );
const { getByText } = render(
<button onClick={ onClick }>button</button>
);
const button = getByText( 'button' );
expect( onClick ).not.toHaveBeenCalled();
press.Enter( button );
Expand All @@ -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(
<button onClick={ onClick } onKeyDown={ ( event ) => event.preventDefault() }>
<button
onClick={ onClick }
onKeyDown={ ( event ) => event.preventDefault() }
>
button
</button>
);
Expand All @@ -42,7 +47,9 @@ describe( 'press', () => {

it( 'should click button when pressing space', () => {
const onClick = jest.fn();
const { getByText } = render( <button onClick={ onClick }>button</button> );
const { getByText } = render(
<button onClick={ onClick }>button</button>
);
const button = getByText( 'button' );
expect( onClick ).not.toHaveBeenCalled();
press.Space( button );
Expand All @@ -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(
<button onClick={ onClick } onKeyDown={ ( event ) => event.preventDefault() }>
<button
onClick={ onClick }
onKeyDown={ ( event ) => event.preventDefault() }
>
button
</button>
);
Expand All @@ -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(
<button onClick={ onClick } onKeyUp={ ( event ) => event.preventDefault() }>
<button
onClick={ onClick }
onKeyUp={ ( event ) => event.preventDefault() }
>
button
</button>
);
Expand Down Expand Up @@ -151,7 +164,9 @@ describe( 'press', () => {
<>
<button>button1</button>
<span>span</span>
<button onKeyDown={ ( event ) => event.preventDefault() }>button2</button>
<button onKeyDown={ ( event ) => event.preventDefault() }>
button2
</button>
</>
);
const button1 = getByText( 'button1' );
Expand Down
15 changes: 12 additions & 3 deletions packages/test-utils/src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } );
Expand Down
4 changes: 1 addition & 3 deletions packages/test-utils/src/utils/get-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 6 additions & 4 deletions packages/test-utils/src/utils/get-next-tabbable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
}
10 changes: 6 additions & 4 deletions packages/test-utils/src/utils/get-previous-tabbable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
}

0 comments on commit e317fe4

Please sign in to comment.