-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.tsx
113 lines (93 loc) · 2.68 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* External dependencies
*/
import { render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
/**
* Internal dependencies
*/
import AlignmentMatrixControl from '..';
const getControl = () => {
return screen.getByRole( 'grid' );
};
const getCell = ( name: string ) => {
return within( getControl() ).getByRole( 'gridcell', { name } );
};
describe( 'AlignmentMatrixControl', () => {
describe( 'Basic rendering', () => {
it( 'should render', () => {
render( <AlignmentMatrixControl /> );
expect( getControl() ).toBeInTheDocument();
} );
it( 'should be centered by default', async () => {
const user = userEvent.setup();
render( <AlignmentMatrixControl /> );
await user.tab();
expect( getCell( 'center center' ) ).toHaveFocus();
} );
} );
describe( 'Should change value', () => {
describe( 'with Mouse', () => {
describe( 'on cell click', () => {
it.each( [
'top left',
'top center',
'top right',
'center left',
'center center',
'center right',
'bottom left',
'bottom center',
'bottom right',
] )( '%s', async ( alignment ) => {
const user = userEvent.setup();
const spy = jest.fn();
render(
<AlignmentMatrixControl
value="center"
onChange={ spy }
/>
);
await user.click( getCell( alignment ) );
expect( getCell( alignment ) ).toHaveFocus();
expect( spy ).toHaveBeenCalledWith( alignment );
} );
} );
} );
describe( 'with Keyboard', () => {
describe( 'on arrow press', () => {
it.each( [
[ 'ArrowUp', 'top center' ],
[ 'ArrowLeft', 'center left' ],
[ 'ArrowDown', 'bottom center' ],
[ 'ArrowRight', 'center right' ],
] )( '%s', async ( keyRef, cellRef ) => {
const user = userEvent.setup();
const spy = jest.fn();
render( <AlignmentMatrixControl onChange={ spy } /> );
await user.tab();
await user.keyboard( `[${ keyRef }]` );
expect( getCell( cellRef ) ).toHaveFocus();
expect( spy ).toHaveBeenCalledWith( cellRef );
} );
} );
describe( 'but not at at edge', () => {
it.each( [
[ 'ArrowUp', 'top left' ],
[ 'ArrowLeft', 'top left' ],
[ 'ArrowDown', 'bottom right' ],
[ 'ArrowRight', 'bottom right' ],
] )( '%s', async ( keyRef, cellRef ) => {
const user = userEvent.setup();
const spy = jest.fn();
render( <AlignmentMatrixControl onChange={ spy } /> );
const cell = getCell( cellRef );
await user.click( cell );
await user.keyboard( `[${ keyRef }]` );
expect( cell ).toHaveFocus();
expect( spy ).toHaveBeenCalledWith( cellRef );
} );
} );
} );
} );
} );