/
index.tsx
145 lines (118 loc) · 3.32 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* External dependencies
*/
import { render, screen, waitFor, within } from '@testing-library/react';
import { press, click, sleep } from '@ariakit/test';
/**
* Internal dependencies
*/
import AlignmentMatrixControl from '..';
const getControl = () => {
return screen.getByRole( 'grid' );
};
const getCell = ( name: string ) => {
return within( getControl() ).getByRole( 'gridcell', { name } );
};
const renderAndInitCompositeStore = async (
jsx: JSX.Element,
focusedCell = 'center center'
) => {
const view = render( jsx );
await waitFor( () => {
expect( getCell( focusedCell ) ).toHaveAttribute( 'tabindex', '0' );
} );
return view;
};
describe( 'AlignmentMatrixControl', () => {
describe( 'Basic rendering', () => {
it( 'should render', async () => {
await renderAndInitCompositeStore( <AlignmentMatrixControl /> );
expect( getControl() ).toBeInTheDocument();
} );
it( 'should be centered by default', async () => {
await renderAndInitCompositeStore( <AlignmentMatrixControl /> );
await sleep();
await press.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 right',
'bottom left',
'bottom center',
'bottom right',
] )( '%s', async ( alignment ) => {
const spy = jest.fn();
await renderAndInitCompositeStore(
<AlignmentMatrixControl
value="center"
onChange={ spy }
/>
);
const cell = getCell( alignment );
await click( cell );
expect( cell ).toHaveFocus();
expect( spy ).toHaveBeenCalledWith( alignment );
} );
it( 'unless already focused', async () => {
const spy = jest.fn();
await renderAndInitCompositeStore(
<AlignmentMatrixControl
value="center"
onChange={ spy }
/>
);
const cell = getCell( 'center center' );
await click( cell );
expect( cell ).toHaveFocus();
expect( spy ).not.toHaveBeenCalled();
} );
} );
} );
describe( 'with Keyboard', () => {
describe( 'on arrow press', () => {
it.each( [
[ 'ArrowUp', 'top center' ],
[ 'ArrowLeft', 'center left' ],
[ 'ArrowDown', 'bottom center' ],
[ 'ArrowRight', 'center right' ],
] as const )( '%s', async ( keyRef, cellRef ) => {
const spy = jest.fn();
await renderAndInitCompositeStore(
<AlignmentMatrixControl onChange={ spy } />
);
await sleep();
await press.Tab();
await press[ 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' ],
] as const )( '%s', async ( keyRef, cellRef ) => {
const spy = jest.fn();
await renderAndInitCompositeStore(
<AlignmentMatrixControl onChange={ spy } />
);
const cell = getCell( cellRef );
await click( cell );
await press[ keyRef ]();
expect( cell ).toHaveFocus();
expect( spy ).toHaveBeenCalledWith( cellRef );
} );
} );
} );
} );
} );