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

Components: Refactor AlignmentMatrixControl tests to use @testing-library/react #44670

Merged
merged 3 commits into from
Oct 4, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 17 additions & 52 deletions packages/components/src/alignment-matrix-control/test/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* 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
Expand All @@ -19,73 +18,39 @@ 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();
getCell( alignment ).focus();

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

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

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 );
}
);
} );
} );