Skip to content

Commit

Permalink
Tests: Refactor TabbableContainer tests to use React Testing Library
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Apr 7, 2020
1 parent db3ba2e commit fe59e79
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions packages/components/src/navigable-container/test/tabbable.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { mount } from 'enzyme';
import { fireEvent, render } from '@testing-library/react';
import { each } from 'lodash';

/**
Expand All @@ -14,16 +14,15 @@ import { TAB, SPACE } from '@wordpress/keycodes';
*/
import { TabbableContainer } from '../tabbable';

function simulateVisible( wrapper, selector ) {
const elements = wrapper.getDOMNode().querySelectorAll( selector );
function simulateVisible( elements ) {
each( elements, ( elem ) => {
elem.getClientRects = () => [
'trick-jsdom-into-having-size-for-element-rect',
];
} );
}

function fireKeyDown( container, keyCode, shiftKey ) {
function fireKeyDown( node, keyCode, shiftKey ) {
const interaction = {
stopped: false,
};
Expand All @@ -35,15 +34,15 @@ function fireKeyDown( container, keyCode, shiftKey ) {
event.stopImmediatePropagation = () => {
interaction.stopped = true;
};
container.getDOMNode().dispatchEvent( event );
fireEvent( node, event );

return interaction;
}

describe( 'TabbableContainer', () => {
it( 'should navigate by keypresses', () => {
let currentIndex = 0;
const wrapper = mount(
const { container } = render(
/*
Disabled because of our rule restricting literal IDs, preferring
`withInstanceId`. In this case, it's fine to use literal IDs.
Expand Down Expand Up @@ -71,13 +70,9 @@ describe( 'TabbableContainer', () => {
/* eslint-enable no-restricted-syntax */
);

simulateVisible( wrapper, '*' );
simulateVisible( container.querySelectorAll( '*' ) );

const container = wrapper.find( 'div.wrapper' );
wrapper
.getDOMNode()
.querySelector( '#section1' )
.focus();
container.querySelector( '#section1' ).focus();

// Navigate options
function assertKeyDown(
Expand All @@ -86,9 +81,13 @@ describe( 'TabbableContainer', () => {
expectedActiveIndex,
expectedStop
) {
const interaction = fireKeyDown( container, keyCode, shiftKey );
expect( currentIndex ).toBe( expectedActiveIndex );
const interaction = fireKeyDown(
container.querySelector( '.wrapper' ),
keyCode,
shiftKey
);
expect( interaction.stopped ).toBe( expectedStop );
expect( currentIndex ).toBe( expectedActiveIndex );
}

assertKeyDown( TAB, false, 1, true );
Expand All @@ -104,7 +103,7 @@ describe( 'TabbableContainer', () => {

it( 'should navigate by keypresses and stop at edges', () => {
let currentIndex = 0;
const wrapper = mount(
const { container } = render(
/*
Disabled because of our rule restricting literal IDs, preferring
`withInstanceId`. In this case, it's fine to use literal IDs.
Expand All @@ -128,13 +127,9 @@ describe( 'TabbableContainer', () => {
/* eslint-enable no-restricted-syntax */
);

simulateVisible( wrapper, '*' );
simulateVisible( container.querySelectorAll( '*' ) );

const container = wrapper.find( 'div.wrapper' );
wrapper
.getDOMNode()
.querySelector( '#section1' )
.focus();
container.querySelector( '#section1' ).focus();

// Navigate options
function assertKeyDown(
Expand All @@ -143,7 +138,11 @@ describe( 'TabbableContainer', () => {
expectedActiveIndex,
expectedStop
) {
const interaction = fireKeyDown( container, keyCode, shiftKey );
const interaction = fireKeyDown(
container.querySelector( '.wrapper' ),
keyCode,
shiftKey
);
expect( currentIndex ).toBe( expectedActiveIndex );
expect( interaction.stopped ).toBe( expectedStop );
}
Expand Down

0 comments on commit fe59e79

Please sign in to comment.