1
- import { fireEvent } from '@testing-library/react' ;
1
+ import { act } from 'react' ;
2
+ import { commands , userEvent } from '@vitest/browser/context' ;
2
3
3
4
import type { Column } from '../../src' ;
4
5
import { resizeHandleClassname } from '../../src/HeaderCell' ;
5
6
import { getGrid , getHeaderCells , setup } from '../utils' ;
6
7
7
- const pointerId = 1 ;
8
-
9
- // TODO: https://github.com/jsdom/jsdom/issues/2527
10
- class PointerEvent extends Event {
11
- pointerId : number | undefined ;
12
- clientX : number | undefined ;
8
+ interface Row {
9
+ readonly col1 : number ;
10
+ readonly col2 : string ;
11
+ }
13
12
14
- constructor ( type : string , { pointerId, clientX, ...rest } : PointerEventInit ) {
15
- super ( type , rest ) ;
16
- this . pointerId = pointerId ;
17
- this . clientX = clientX ;
18
- }
13
+ interface ResizeArgs {
14
+ readonly column : HTMLElement ;
15
+ readonly resizeBy : number ;
19
16
}
20
17
21
- // @ts -expect-error
22
- globalThis . PointerEvent = PointerEvent ;
18
+ async function resize ( { column, resizeBy } : ResizeArgs ) {
19
+ const resizeHandle = column . querySelector ( `.${ resizeHandleClassname } ` ) ;
20
+ if ( resizeHandle === null ) return ;
23
21
24
- interface ResizeEvent < K extends keyof DOMRect > {
25
- column : HTMLElement ;
26
- clientXStart : number ;
27
- clientXEnd : number ;
28
- rect : Pick < DOMRect , K > ;
22
+ await act ( async ( ) => {
23
+ // @ts -expect-error
24
+ await commands . resizeColumn ( resizeBy ) ;
25
+ } ) ;
29
26
}
30
27
31
- function resize < K extends keyof DOMRect > ( {
32
- column,
33
- clientXStart,
34
- clientXEnd,
35
- rect
36
- } : ResizeEvent < K > ) {
28
+ async function autoResize ( column : HTMLElement ) {
37
29
const resizeHandle = column . querySelector ( `.${ resizeHandleClassname } ` ) ;
38
30
if ( resizeHandle === null ) return ;
39
31
40
- const original = column . getBoundingClientRect . bind ( column ) ;
41
- column . getBoundingClientRect = ( ) => ( {
42
- ...original ( ) ,
43
- ...rect
32
+ // eslint-disable-next-line testing-library/no-unnecessary-act
33
+ await act ( async ( ) => {
34
+ await userEvent . dblClick ( resizeHandle ) ;
44
35
} ) ;
45
- // eslint-disable-next-line testing-library/prefer-user-event
46
- fireEvent . pointerDown (
47
- resizeHandle ,
48
- new PointerEvent ( 'pointerdown' , { pointerId, clientX : clientXStart } )
49
- ) ;
50
- // eslint-disable-next-line testing-library/prefer-user-event
51
- fireEvent . pointerMove ( resizeHandle , new PointerEvent ( 'pointermove' , { clientX : clientXEnd } ) ) ;
52
- fireEvent . lostPointerCapture ( resizeHandle , new PointerEvent ( 'lostpointercapture' , { } ) ) ;
53
36
}
54
37
55
- const columns : readonly Column < never > [ ] = [
38
+ const columns : readonly Column < Row > [ ] = [
56
39
{
57
40
key : 'col1' ,
58
41
name : 'col1' ,
@@ -68,34 +51,85 @@ const columns: readonly Column<never>[] = [
68
51
}
69
52
] ;
70
53
71
- test ( 'should not resize column if resizable is not specified' , ( ) => {
72
- setup ( { columns, rows : [ ] } ) ;
54
+ test ( 'should not resize column if resizable is not specified' , async ( ) => {
55
+ setup < Row , unknown > ( { columns, rows : [ ] } ) ;
73
56
const [ col1 ] = getHeaderCells ( ) ;
74
57
expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
75
- resize ( { column : col1 , clientXStart : 95 , clientXEnd : 200 , rect : { right : 100 , left : 0 } } ) ;
58
+ await resize ( { column : col1 , resizeBy : 50 } ) ;
59
+ expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
60
+ await resize ( { column : col1 , resizeBy : - 50 } ) ;
76
61
expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
77
62
} ) ;
78
63
79
- test ( 'should resize column when dragging the handle' , ( ) => {
80
- setup ( { columns, rows : [ ] } ) ;
64
+ test ( 'should resize column when dragging the handle' , async ( ) => {
65
+ setup < Row , unknown > ( { columns, rows : [ ] } ) ;
81
66
const [ , col2 ] = getHeaderCells ( ) ;
82
- expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
83
- resize ( { column : col2 , clientXStart : 289 , clientXEnd : 250 , rect : { right : 300 , left : 100 } } ) ;
84
- expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 161px' } ) ;
67
+ const grid = getGrid ( ) ;
68
+ expect ( grid ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
69
+ await resize ( { column : col2 , resizeBy : - 50 } ) ;
70
+ expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 150px' } ) ;
85
71
} ) ;
86
72
87
- test ( 'should use the maxWidth if specified' , ( ) => {
88
- setup ( { columns, rows : [ ] } ) ;
73
+ test ( 'should use the maxWidth if specified' , async ( ) => {
74
+ setup < Row , unknown > ( { columns, rows : [ ] } ) ;
89
75
const [ , col2 ] = getHeaderCells ( ) ;
90
- expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
91
- resize ( { column : col2 , clientXStart : 295 , clientXEnd : 1000 , rect : { right : 300 , left : 100 } } ) ;
76
+ expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 200px ' } ) ;
77
+ await resize ( { column : col2 , resizeBy : 1000 } ) ;
92
78
expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 400px' } ) ;
93
79
} ) ;
94
80
95
- test ( 'should use the minWidth if specified' , ( ) => {
96
- setup ( { columns, rows : [ ] } ) ;
81
+ test ( 'should use the minWidth if specified' , async ( ) => {
82
+ setup < Row , unknown > ( { columns, rows : [ ] } ) ;
97
83
const [ , col2 ] = getHeaderCells ( ) ;
98
84
expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
99
- resize ( { column : col2 , clientXStart : 295 , clientXEnd : 100 , rect : { right : 300 , left : 100 } } ) ;
85
+ await resize ( { column : col2 , resizeBy : - 150 } ) ;
100
86
expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 100px' } ) ;
101
87
} ) ;
88
+
89
+ test ( 'should not auto resize column if resizable is not specified' , async ( ) => {
90
+ setup < Row , unknown > ( {
91
+ columns,
92
+ rows : [
93
+ {
94
+ col1 : 1 ,
95
+ col2 : 'a' . repeat ( 50 )
96
+ }
97
+ ]
98
+ } ) ;
99
+ const [ col1 ] = getHeaderCells ( ) ;
100
+ expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
101
+ await autoResize ( col1 ) ;
102
+ expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
103
+ } ) ;
104
+
105
+ test ( 'should auto resize column when resize handle is double clicked' , async ( ) => {
106
+ setup < Row , unknown > ( {
107
+ columns,
108
+ rows : [
109
+ {
110
+ col1 : 1 ,
111
+ col2 : 'a' . repeat ( 50 )
112
+ }
113
+ ]
114
+ } ) ;
115
+ const [ , col2 ] = getHeaderCells ( ) ;
116
+ expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
117
+ await autoResize ( col2 ) ;
118
+ expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 327.703px' } ) ;
119
+ } ) ;
120
+
121
+ test ( 'should use the maxWidth if specified on auto resize' , async ( ) => {
122
+ setup < Row , unknown > ( {
123
+ columns,
124
+ rows : [
125
+ {
126
+ col1 : 1 ,
127
+ col2 : 'a' . repeat ( 500 )
128
+ }
129
+ ]
130
+ } ) ;
131
+ const [ , col2 ] = getHeaderCells ( ) ;
132
+ expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 200px' } ) ;
133
+ await autoResize ( col2 ) ;
134
+ expect ( getGrid ( ) ) . toHaveStyle ( { gridTemplateColumns : '100px 400px' } ) ;
135
+ } ) ;
0 commit comments