-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Components: Refactor
AlignmentMatrixControl
tests to use `@testing-…
…library/react` (#44670) * Components: Refactor AlignmentMatrixControl tests to use @testing-library/react render * Refactor some tests * Remove unnecessary window.focus mocks
- Loading branch information
Showing
1 changed file
with
17 additions
and
62 deletions.
There are no files selected for viewing
79 changes: 17 additions & 62 deletions
79
packages/components/src/alignment-matrix-control/test/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
); | ||
} ); | ||
} ); |