@@ -7,6 +7,8 @@ import type { Column, DataGridProps } from '../../../src';
77import { getCellsAtRowIndex , getRowWithCell , safeTab , scrollGrid , testCount } from '../utils' ;
88
99const grid = page . getGrid ( ) ;
10+ const col1Editor = page . getByRole ( 'spinbutton' , { name : 'col1-editor' } ) ;
11+ const col2Editor = page . getByRole ( 'textbox' , { name : 'col2-editor' } ) ;
1012
1113interface Row {
1214 col1 : number ;
@@ -16,28 +18,26 @@ interface Row {
1618describe ( 'Editor' , ( ) => {
1719 it ( 'should open editor on double click' , async ( ) => {
1820 await page . render ( < EditorTest /> ) ;
19- const editor = page . getByRole ( 'spinbutton' , { name : 'col1-editor' } ) ;
2021 await userEvent . click ( getCellsAtRowIndex ( 0 ) . nth ( 0 ) ) ;
21- await expect . element ( editor ) . not . toBeInTheDocument ( ) ;
22+ await expect . element ( col1Editor ) . not . toBeInTheDocument ( ) ;
2223 await userEvent . dblClick ( getCellsAtRowIndex ( 0 ) . nth ( 0 ) ) ;
23- await expect . element ( editor ) . toHaveValue ( 1 ) ;
24+ await expect . element ( col1Editor ) . toHaveValue ( 1 ) ;
2425 await userEvent . keyboard ( '2' ) ;
2526 await safeTab ( ) ;
26- await expect . element ( editor ) . not . toBeInTheDocument ( ) ;
27+ await expect . element ( col1Editor ) . not . toBeInTheDocument ( ) ;
2728 await expect . element ( getCellsAtRowIndex ( 0 ) . nth ( 0 ) ) . toHaveTextContent ( / ^ 1 2 $ / ) ;
2829 } ) ;
2930
3031 it ( 'should open and commit changes on enter' , async ( ) => {
3132 await page . render ( < EditorTest /> ) ;
32- const editor = page . getByRole ( 'spinbutton' , { name : 'col1-editor' } ) ;
3333 await userEvent . click ( getCellsAtRowIndex ( 0 ) . nth ( 0 ) ) ;
34- await expect . element ( editor ) . not . toBeInTheDocument ( ) ;
34+ await expect . element ( col1Editor ) . not . toBeInTheDocument ( ) ;
3535 await userEvent . keyboard ( '{enter}' ) ;
36- await expect . element ( editor ) . toHaveValue ( 1 ) ;
36+ await expect . element ( col1Editor ) . toHaveValue ( 1 ) ;
3737 await userEvent . keyboard ( '3{enter}' ) ;
3838 await expect . element ( getCellsAtRowIndex ( 0 ) . nth ( 0 ) ) . toHaveTextContent ( / ^ 1 3 $ / ) ;
3939 await expect . element ( getCellsAtRowIndex ( 0 ) . nth ( 0 ) ) . toHaveFocus ( ) ;
40- await expect . element ( editor ) . not . toBeInTheDocument ( ) ;
40+ await expect . element ( col1Editor ) . not . toBeInTheDocument ( ) ;
4141 } ) ;
4242
4343 it ( 'should open editor when user types' , async ( ) => {
@@ -51,22 +51,20 @@ describe('Editor', () => {
5151 it ( 'should close editor and discard changes on escape' , async ( ) => {
5252 await page . render ( < EditorTest /> ) ;
5353 await userEvent . dblClick ( getCellsAtRowIndex ( 0 ) . nth ( 0 ) ) ;
54- const editor = page . getByRole ( 'spinbutton' , { name : 'col1-editor' } ) ;
55- await expect . element ( editor ) . toHaveValue ( 1 ) ;
54+ await expect . element ( col1Editor ) . toHaveValue ( 1 ) ;
5655 await userEvent . keyboard ( '2222{escape}' ) ;
57- await expect . element ( editor ) . not . toBeInTheDocument ( ) ;
56+ await expect . element ( col1Editor ) . not . toBeInTheDocument ( ) ;
5857 await expect . element ( getCellsAtRowIndex ( 0 ) . nth ( 0 ) ) . toHaveTextContent ( / ^ 1 $ / ) ;
5958 await expect . element ( getCellsAtRowIndex ( 0 ) . nth ( 0 ) ) . toHaveFocus ( ) ;
6059 } ) ;
6160
6261 it ( 'should commit changes and close editor when clicked outside' , async ( ) => {
6362 await page . render ( < EditorTest /> ) ;
6463 await userEvent . dblClick ( getCellsAtRowIndex ( 0 ) . nth ( 0 ) ) ;
65- const editor = page . getByRole ( 'spinbutton' , { name : 'col1-editor' } ) ;
66- await expect . element ( editor ) . toHaveValue ( 1 ) ;
64+ await expect . element ( col1Editor ) . toHaveValue ( 1 ) ;
6765 await userEvent . keyboard ( '2222' ) ;
6866 await userEvent . click ( page . getByText ( 'outside' ) ) ;
69- await expect . element ( editor ) . not . toBeInTheDocument ( ) ;
67+ await expect . element ( col1Editor ) . not . toBeInTheDocument ( ) ;
7068 await expect . element ( getCellsAtRowIndex ( 0 ) . nth ( 0 ) ) . toHaveTextContent ( / ^ 1 2 2 2 2 $ / ) ;
7169 } ) ;
7270
@@ -100,13 +98,12 @@ describe('Editor', () => {
10098 await testCount ( activeRowCells , 2 ) ;
10199 await scrollGrid ( { top : 2001 } ) ;
102100 await testCount ( activeRowCells , 1 ) ;
103- const editor = grid . getByRole ( 'spinbutton' , { name : 'col1-editor' } ) ;
104- await expect . element ( editor ) . not . toBeInTheDocument ( ) ;
101+ await expect . element ( col1Editor ) . not . toBeInTheDocument ( ) ;
105102 await expect . element ( grid ) . toHaveProperty ( 'scrollTop' , 2001 ) ;
106103 // TODO: await userEvent.keyboard('123'); fails in FF
107104 await userEvent . keyboard ( '{enter}123' ) ;
108105 await testCount ( activeRowCells , 2 ) ;
109- await expect . element ( editor ) . toHaveValue ( 123 ) ;
106+ await expect . element ( col1Editor ) . toHaveValue ( 123 ) ;
110107 await expect . element ( grid ) . toHaveProperty ( 'scrollTop' , 0 ) ;
111108 } ) ;
112109
@@ -116,13 +113,13 @@ describe('Editor', () => {
116113 const cell = getCellsAtRowIndex ( 0 ) . nth ( 1 ) ;
117114 await expect . element ( cell ) . not . toHaveAttribute ( 'aria-readonly' ) ;
118115 await userEvent . dblClick ( cell ) ;
119- await expect . element ( page . getByRole ( 'textbox' , { name : 'col2-editor' } ) ) . toBeInTheDocument ( ) ;
116+ await expect . element ( col2Editor ) . toBeInTheDocument ( ) ;
120117 } ) ;
121118
122119 it ( 'should be editable if an editor is specified and editable is set to true' , async ( ) => {
123120 await page . render ( < EditorTest editable /> ) ;
124121 await userEvent . dblClick ( getCellsAtRowIndex ( 0 ) . nth ( 1 ) ) ;
125- await expect . element ( page . getByRole ( 'textbox' , { name : 'col2-editor' } ) ) . toBeInTheDocument ( ) ;
122+ await expect . element ( col2Editor ) . toBeInTheDocument ( ) ;
126123 } ) ;
127124
128125 it ( 'should not be editable if editable is false' , async ( ) => {
@@ -131,19 +128,16 @@ describe('Editor', () => {
131128 await expect . element ( cell ) . toHaveAttribute ( 'aria-readonly' , 'true' ) ;
132129 await userEvent . dblClick ( cell ) ;
133130
134- await expect
135- . element ( page . getByRole ( 'textbox' , { name : 'col2-editor' } ) )
136- . not . toBeInTheDocument ( ) ;
131+ await expect . element ( col2Editor ) . not . toBeInTheDocument ( ) ;
137132 } ) ;
138133
139134 it ( 'should not be editable if editable function returns false' , async ( ) => {
140135 await page . render ( < EditorTest editable = { ( row ) => row . col1 === 2 } /> ) ;
141136 await userEvent . dblClick ( getCellsAtRowIndex ( 0 ) . nth ( 1 ) ) ;
142- const editor = page . getByRole ( 'textbox' , { name : 'col2-editor' } ) ;
143- await expect . element ( editor ) . not . toBeInTheDocument ( ) ;
137+ await expect . element ( col2Editor ) . not . toBeInTheDocument ( ) ;
144138
145139 await userEvent . dblClick ( getCellsAtRowIndex ( 1 ) . nth ( 1 ) ) ;
146- await expect . element ( editor ) . toBeInTheDocument ( ) ;
140+ await expect . element ( col2Editor ) . toBeInTheDocument ( ) ;
147141 } ) ;
148142 } ) ;
149143
@@ -153,21 +147,20 @@ describe('Editor', () => {
153147 < EditorTest createEditorPortal editorOptions = { { displayCellContent : true } } />
154148 ) ;
155149 await userEvent . dblClick ( getCellsAtRowIndex ( 0 ) . nth ( 1 ) ) ;
156- const editor1 = page . getByRole ( 'textbox' , { name : 'col2-editor' } ) ;
157- await expect . element ( editor1 ) . toHaveValue ( 'a1' ) ;
150+ await expect . element ( col2Editor ) . toHaveValue ( 'a1' ) ;
158151 await userEvent . keyboard ( '23' ) ;
159152 // The cell value should update as the editor value is changed
160153 await expect . element ( getCellsAtRowIndex ( 0 ) . nth ( 1 ) ) . toHaveTextContent ( / ^ a 1 2 3 $ / ) ;
161154 // clicking in a portal does not count as an outside click
162- await userEvent . click ( editor1 ) ;
163- await expect . element ( editor1 ) . toBeInTheDocument ( ) ;
155+ await userEvent . click ( col2Editor ) ;
156+ await expect . element ( col2Editor ) . toBeInTheDocument ( ) ;
164157 // true outside clicks are still detected
165158 await userEvent . click ( page . getByText ( 'outside' ) ) ;
166- await expect . element ( editor1 ) . not . toBeInTheDocument ( ) ;
159+ await expect . element ( col2Editor ) . not . toBeInTheDocument ( ) ;
167160 await expect . element ( getCellsAtRowIndex ( 0 ) . nth ( 1 ) ) . not . toHaveFocus ( ) ;
168161
169162 await userEvent . dblClick ( getCellsAtRowIndex ( 0 ) . nth ( 1 ) ) ;
170- await userEvent . click ( page . getByRole ( 'textbox' , { name : 'col2-editor' } ) ) ;
163+ await userEvent . click ( col2Editor ) ;
171164 await userEvent . keyboard ( '{enter}' ) ;
172165 await expect . element ( getCellsAtRowIndex ( 0 ) . nth ( 1 ) ) . toHaveFocus ( ) ;
173166 } ) ;
@@ -181,13 +174,12 @@ describe('Editor', () => {
181174 />
182175 ) ;
183176 await userEvent . dblClick ( getCellsAtRowIndex ( 0 ) . nth ( 1 ) ) ;
184- const editor = page . getByRole ( 'textbox' , { name : 'col2-editor' } ) ;
185- await expect . element ( editor ) . toBeInTheDocument ( ) ;
177+ await expect . element ( col2Editor ) . toBeInTheDocument ( ) ;
186178 await userEvent . click ( page . getByText ( 'outside' ) ) ;
187- await expect . element ( editor ) . toBeInTheDocument ( ) ;
188- await userEvent . click ( editor ) ;
179+ await expect . element ( col2Editor ) . toBeInTheDocument ( ) ;
180+ await userEvent . click ( col2Editor ) ;
189181 await userEvent . keyboard ( '{enter}' ) ;
190- await expect . element ( editor ) . not . toBeInTheDocument ( ) ;
182+ await expect . element ( col2Editor ) . not . toBeInTheDocument ( ) ;
191183 } ) ;
192184
193185 it ( 'should not open editor if onCellKeyDown prevents the default event' , async ( ) => {
@@ -205,9 +197,7 @@ describe('Editor', () => {
205197 await userEvent . keyboard ( '{enter}yz{enter}' ) ;
206198 await expect . element ( getCellsAtRowIndex ( 0 ) . nth ( 1 ) ) . toHaveTextContent ( / ^ a 1 y z $ / ) ;
207199 await userEvent . keyboard ( 'x' ) ;
208- await expect
209- . element ( page . getByRole ( 'textbox' , { name : 'col2-editor' } ) )
210- . not . toBeInTheDocument ( ) ;
200+ await expect . element ( col2Editor ) . not . toBeInTheDocument ( ) ;
211201 } ) ;
212202
213203 it ( 'should prevent navigation if onCellKeyDown prevents the default event' , async ( ) => {
@@ -236,10 +226,9 @@ describe('Editor', () => {
236226 />
237227 ) ;
238228 await userEvent . dblClick ( getCellsAtRowIndex ( 0 ) . nth ( 1 ) ) ;
239- const editor = page . getByRole ( 'textbox' , { name : 'col2-editor' } ) ;
240- await expect . element ( editor ) . toBeInTheDocument ( ) ;
229+ await expect . element ( col2Editor ) . toBeInTheDocument ( ) ;
241230 await userEvent . click ( page . getByRole ( 'button' , { name : 'update' } ) ) ;
242- await expect . element ( editor ) . not . toBeInTheDocument ( ) ;
231+ await expect . element ( col2Editor ) . not . toBeInTheDocument ( ) ;
243232 } ) ;
244233
245234 it ( 'should not close the editor when closeOnExternalRowChange is false and row is changed from outside' , async ( ) => {
@@ -252,10 +241,9 @@ describe('Editor', () => {
252241 />
253242 ) ;
254243 await userEvent . dblClick ( getCellsAtRowIndex ( 0 ) . nth ( 1 ) ) ;
255- const editor = page . getByRole ( 'textbox' , { name : 'col2-editor' } ) ;
256- await expect . element ( editor ) . toBeInTheDocument ( ) ;
244+ await expect . element ( col2Editor ) . toBeInTheDocument ( ) ;
257245 await userEvent . click ( page . getByRole ( 'button' , { name : 'update' } ) ) ;
258- await expect . element ( editor ) . toBeInTheDocument ( ) ;
246+ await expect . element ( col2Editor ) . toBeInTheDocument ( ) ;
259247 } ) ;
260248 } ) ;
261249
0 commit comments