@@ -2,40 +2,25 @@ import { useState } from 'react';
22import { commands , page , userEvent } from '@vitest/browser/context' ;
33
44import { DataGrid , type Column , type ColumnWidth , type ColumnWidths } from '../../../src' ;
5- import { getGrid , getHeaderCells , setup } from '../utils' ;
5+ import { getGrid , getHeaderCell , setup } from '../utils' ;
66
77interface Row {
88 readonly col1 : number ;
99 readonly col2 : string ;
1010}
1111
12- function queryResizeHandle ( column : Element ) {
13- return column . querySelector ( '.rdg-resize-handle' ) ;
12+ function getResizeHandle ( name : string ) {
13+ return getHeaderCell ( name ) . getBySelector ( '.rdg-resize-handle' ) ;
1414}
1515
16- function getResizeHandle ( column : Element ) {
17- const resizeHandle = queryResizeHandle ( column ) ;
16+ async function resize ( columnName : string , resizeBy : number | readonly number [ ] ) {
17+ await expect . element ( getResizeHandle ( columnName ) ) . toBeInTheDocument ( ) ;
1818
19- if ( resizeHandle === null ) {
20- throw new Error ( 'Resize handle not found' ) ;
21- }
22-
23- return resizeHandle ;
24- }
25-
26- interface ResizeArgs {
27- readonly column : Element ;
28- readonly resizeBy : number | readonly number [ ] ;
29- }
30-
31- async function resize ( { column, resizeBy } : ResizeArgs ) {
32- expect ( getResizeHandle ( column ) ) . toBeInTheDocument ( ) ;
33-
34- await commands . resizeColumn ( resizeBy ) ;
19+ await commands . resizeColumn ( 'col2' , resizeBy ) ;
3520}
3621
37- async function autoResize ( column : Element ) {
38- const resizeHandle = getResizeHandle ( column ) ;
22+ async function autoResize ( columnName : string ) {
23+ const resizeHandle = getResizeHandle ( columnName ) ;
3924
4025 await userEvent . dblClick ( resizeHandle ) ;
4126}
@@ -56,10 +41,9 @@ const columns: readonly Column<Row>[] = [
5641 }
5742] ;
5843
59- test ( 'cannot resize or auto resize column when resizable is not specified' , ( ) => {
44+ test ( 'cannot resize or auto resize column when resizable is not specified' , async ( ) => {
6045 setup < Row , unknown > ( { columns, rows : [ ] } ) ;
61- const [ col1 ] = getHeaderCells ( ) ;
62- expect ( queryResizeHandle ( col1 ) ) . not . toBeInTheDocument ( ) ;
46+ await expect . element ( getResizeHandle ( 'col1' ) ) . not . toBeInTheDocument ( ) ;
6347} ) ;
6448
6549test ( 'should resize column when dragging the handle' , async ( ) => {
@@ -68,8 +52,7 @@ test('should resize column when dragging the handle', async () => {
6852 const grid = getGrid ( ) ;
6953 expect ( onColumnResize ) . not . toHaveBeenCalled ( ) ;
7054 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
71- const [ , col2 ] = getHeaderCells ( ) ;
72- await resize ( { column : col2 , resizeBy : - 50 } ) ;
55+ await resize ( 'col2' , - 50 ) ;
7356 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 150px' } ) ;
7457 expect ( onColumnResize ) . toHaveBeenCalledExactlyOnceWith ( expect . objectContaining ( columns [ 1 ] ) , 150 ) ;
7558} ) ;
@@ -78,17 +61,15 @@ test('should use the maxWidth if specified when dragging the handle', async () =
7861 setup < Row , unknown > ( { columns, rows : [ ] } ) ;
7962 const grid = getGrid ( ) ;
8063 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 200px ' } ) ;
81- const [ , col2 ] = getHeaderCells ( ) ;
82- await resize ( { column : col2 , resizeBy : 1000 } ) ;
64+ await resize ( 'col2' , 1000 ) ;
8365 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 400px' } ) ;
8466} ) ;
8567
8668test ( 'should use the minWidth if specified when dragging the handle' , async ( ) => {
8769 setup < Row , unknown > ( { columns, rows : [ ] } ) ;
8870 const grid = getGrid ( ) ;
8971 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
90- const [ , col2 ] = getHeaderCells ( ) ;
91- await resize ( { column : col2 , resizeBy : - 150 } ) ;
72+ await resize ( 'col2' , - 150 ) ;
9273 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 100px' } ) ;
9374} ) ;
9475
@@ -98,7 +79,7 @@ test('should resize column using keboard', async () => {
9879 const grid = getGrid ( ) ;
9980 expect ( onColumnResize ) . not . toHaveBeenCalled ( ) ;
10081 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
101- const [ , col2 ] = getHeaderCells ( ) ;
82+ const col2 = getHeaderCell ( 'col2' ) ;
10283 await userEvent . click ( col2 ) ;
10384
10485 await userEvent . keyboard ( '{Control>}{ArrowRight}{/Control}' ) ;
@@ -116,7 +97,7 @@ test('should use the maxWidth if specified when resizing using keyboard', async
11697 setup < Row , unknown > ( { columns, rows : [ ] , onColumnResize } ) ;
11798 const grid = getGrid ( ) ;
11899 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 200px ' } ) ;
119- const [ , col2 ] = getHeaderCells ( ) ;
100+ const col2 = getHeaderCell ( 'col2' ) ;
120101 await userEvent . click ( col2 ) ;
121102 await userEvent . keyboard ( `{Control>}${ '{ArrowRight}' . repeat ( 22 ) } {/Control}` ) ;
122103 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 400px' } ) ;
@@ -128,7 +109,7 @@ test('should use the minWidth if specified resizing using keyboard', async () =>
128109 setup < Row , unknown > ( { columns, rows : [ ] , onColumnResize } ) ;
129110 const grid = getGrid ( ) ;
130111 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
131- const [ , col2 ] = getHeaderCells ( ) ;
112+ const col2 = getHeaderCell ( 'col2' ) ;
132113 await userEvent . click ( col2 ) ;
133114 await userEvent . keyboard ( `{Control>}${ '{ArrowLeft}' . repeat ( 12 ) } {/Control}` ) ;
134115 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 100px' } ) ;
@@ -149,8 +130,7 @@ test('should auto resize column when resize handle is double clicked', async ()
149130 } ) ;
150131 const grid = getGrid ( ) ;
151132 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
152- const [ , col2 ] = getHeaderCells ( ) ;
153- await autoResize ( col2 ) ;
133+ await autoResize ( 'col2' ) ;
154134 await testGridTemplateColumns ( '100px 327.703px' , '100px 327.833px' , '100px 400px' ) ;
155135 expect ( onColumnResize ) . toHaveBeenCalledExactlyOnceWith (
156136 expect . objectContaining ( columns [ 1 ] ) ,
@@ -177,8 +157,7 @@ test('should use the maxWidth if specified on auto resize', async () => {
177157 } ) ;
178158 const grid = getGrid ( ) ;
179159 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
180- const [ , col2 ] = getHeaderCells ( ) ;
181- await autoResize ( col2 ) ;
160+ await autoResize ( 'col2' ) ;
182161 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 400px' } ) ;
183162} ) ;
184163
@@ -194,8 +173,7 @@ test('should use the minWidth if specified on auto resize', async () => {
194173 } ) ;
195174 const grid = getGrid ( ) ;
196175 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
197- const [ , col2 ] = getHeaderCells ( ) ;
198- await autoResize ( col2 ) ;
176+ await autoResize ( 'col2' ) ;
199177 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 100px' } ) ;
200178} ) ;
201179
@@ -237,16 +215,15 @@ test('should remeasure flex columns when resizing a column', async () => {
237215 } ) ;
238216
239217 await testGridTemplateColumns ( '639.328px 639.328px 639.344px' , '639.333px 639.333px 639.333px' ) ;
240- const [ col1 ] = getHeaderCells ( ) ;
241- await autoResize ( col1 ) ;
218+ await autoResize ( 'col1' ) ;
242219 await testGridTemplateColumns (
243220 '79.1406px 919.422px 919.438px' ,
244221 '79.1667px 919.417px 919.417px' ,
245222 '100.5px 908.75px 908.75px'
246223 ) ;
247224 expect ( onColumnResize ) . toHaveBeenCalledOnce ( ) ;
248225 // onColumnResize is not called if width is not changed
249- await autoResize ( col1 ) ;
226+ await autoResize ( ' col1' ) ;
250227 await testGridTemplateColumns (
251228 '79.1406px 919.422px 919.438px' ,
252229 '79.1667px 919.417px 919.417px' ,
@@ -302,8 +279,7 @@ test('should use columnWidths and onColumnWidthsChange props when provided', asy
302279
303280 const grid = getGrid ( ) ;
304281 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '101px 201px' } ) ;
305- const [ , col2 ] = getHeaderCells ( ) ;
306- await autoResize ( col2 ) ;
282+ await autoResize ( 'col2' ) ;
307283 expect ( onColumnWidthsChangeSpy ) . toHaveBeenCalledExactlyOnceWith (
308284 new Map ( [
309285 [ 'col1' , { width : 101 , type : 'measured' } ] ,
@@ -317,7 +293,7 @@ test('should use columnWidths and onColumnWidthsChange props when provided', asy
317293 onColumnWidthsChangeSpy . mockClear ( ) ;
318294 onColumnResizeSpy . mockClear ( ) ;
319295
320- await resize ( { column : col2 , resizeBy : [ 5 , 5 , 5 ] } ) ;
296+ await resize ( ' col2' , [ 5 , 5 , 5 ] ) ;
321297 expect ( onColumnWidthsChangeSpy ) . toHaveBeenCalledExactlyOnceWith (
322298 new Map ( [
323299 [ 'col1' , { width : 101 , type : 'measured' } ] ,
@@ -335,7 +311,7 @@ test('should use columnWidths and onColumnWidthsChange props when provided', asy
335311 expect ( onColumnWidthsChangeSpy ) . not . toHaveBeenCalled ( ) ;
336312 expect ( onColumnResizeSpy ) . not . toHaveBeenCalled ( ) ;
337313 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : '120px 120px' } ) ;
338- await resize ( { column : col2 , resizeBy : [ 5 , 5 ] } ) ;
314+ await resize ( ' col2' , [ 5 , 5 ] ) ;
339315 expect ( onColumnWidthsChangeSpy ) . toHaveBeenCalledExactlyOnceWith (
340316 new Map ( [
341317 [ 'col1' , { width : 120 , type : 'measured' } ] ,
@@ -349,9 +325,11 @@ async function testGridTemplateColumns(chrome: string, firefox: string, firefoxC
349325 if ( navigator . userAgent . includes ( 'Chrome' ) ) {
350326 await expect . element ( grid ) . toHaveStyle ( { gridTemplateColumns : chrome } ) ;
351327 } else {
352- expect ( ( grid . element ( ) as HTMLDivElement ) . style . gridTemplateColumns ) . toBeOneOf ( [
353- firefox ,
354- firefoxCI
355- ] ) ;
328+ await vi . waitFor ( ( ) => {
329+ expect ( ( grid . element ( ) as HTMLDivElement ) . style . gridTemplateColumns ) . toBeOneOf ( [
330+ firefox ,
331+ firefoxCI
332+ ] ) ;
333+ } ) ;
356334 }
357335}
0 commit comments