Skip to content

Commit

Permalink
Components: Refactor AlignmentMatrixControl tests to use `@testing-…
Browse files Browse the repository at this point in the history
…library/react` (#44670)

* Components: Refactor AlignmentMatrixControl tests to use @testing-library/react render

* Refactor some tests

* Remove unnecessary window.focus mocks
  • Loading branch information
tyxla authored Oct 4, 2022
1 parent 31dad37 commit 6e775c3
Showing 1 changed file with 17 additions and 62 deletions.
79 changes: 17 additions & 62 deletions packages/components/src/alignment-matrix-control/test/index.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,46 @@
/**
* External dependencies
*/
import { render, unmountComponentAtNode } from 'react-dom';
import { act } from 'react-dom/test-utils';
import { render, screen, within } from '@testing-library/react';

/**
* Internal dependencies
*/
import AlignmentMatrixControl from '../';

const __windowFocus = window.focus;

beforeAll( () => {
window.focus = jest.fn();
} );

afterAll( () => {
window.focus = __windowFocus;
} );

let container = null;

beforeEach( () => {
container = document.createElement( 'div' );
document.body.appendChild( container );
} );

afterEach( () => {
unmountComponentAtNode( container );
container.remove();
container = null;
} );

const getControl = () => {
return container.querySelector( '.component-alignment-matrix-control' );
return screen.getByRole( 'grid' );
};

const getCells = () => {
const control = getControl();
return control.querySelectorAll( '[role="gridcell"]' );
const getCell = ( name ) => {
return within( getControl() ).getByRole( 'gridcell', { name } );
};

describe( 'AlignmentMatrixControl', () => {
describe( 'Basic rendering', () => {
it( 'should render', () => {
act( () => {
render( <AlignmentMatrixControl />, container );
} );
const control = getControl();
render( <AlignmentMatrixControl /> );

expect( control ).toBeTruthy();
expect( getControl() ).toBeInTheDocument();
} );
} );

describe( 'Change value', () => {
it( 'should change value on cell click', () => {
const spy = jest.fn();
const alignments = [ 'center left', 'center center', 'bottom center' ];

it.each( alignments )(
'should change value on %s cell click',
( alignment ) => {
const spy = jest.fn();

act( () => {
render(
<AlignmentMatrixControl
value={ 'center' }
onChange={ spy }
/>,
container
<AlignmentMatrixControl value="center" onChange={ spy } />
);
} );

const cells = getCells();

act( () => {
cells[ 3 ].focus();
} );

expect( spy.mock.calls[ 0 ][ 0 ] ).toBe( 'center left' );
getCell( alignment ).focus();

act( () => {
cells[ 4 ].focus();
} );

expect( spy.mock.calls[ 1 ][ 0 ] ).toBe( 'center center' );

act( () => {
cells[ 7 ].focus();
} );

expect( spy.mock.calls[ 2 ][ 0 ] ).toBe( 'bottom center' );
} );
expect( spy ).toHaveBeenCalledWith( alignment );
}
);
} );
} );

0 comments on commit 6e775c3

Please sign in to comment.