@@ -84,13 +84,13 @@ const onCellPasteSpy = vi.fn(({ row }: { row: Row }) => row);
8484function rowKeyGetter ( row : Row ) {
8585 return row . id ;
8686}
87-
88- interface TestGridProps {
87+ function TestGrid ( {
88+ groupBy,
89+ groupIdGetter
90+ } : {
8991 groupBy : string [ ] ;
90- generateGroupId : ( ( groupKey : string , parentId ?: string ) => string ) | undefined ;
91- }
92-
93- function TestGrid ( { groupBy, generateGroupId } : TestGridProps ) {
92+ groupIdGetter : ( ( groupKey : string , parentId ?: string ) => string ) | undefined ;
93+ } ) {
9494 const [ rows , setRows ] = useState ( initialRows ) ;
9595 const [ selectedRows , setSelectedRows ] = useState ( ( ) : ReadonlySet < number > => new Set ( ) ) ;
9696 const [ expandedGroupIds , setExpandedGroupIds ] = useState (
@@ -113,7 +113,7 @@ function TestGrid({ groupBy, generateGroupId }: TestGridProps) {
113113 onRowsChange = { setRows }
114114 onCellCopy = { onCellCopySpy }
115115 onCellPaste = { onCellPasteSpy }
116- { ... ( generateGroupId && { generateGroupId } ) }
116+ groupIdGetter = { groupIdGetter }
117117 />
118118 ) ;
119119}
@@ -124,11 +124,8 @@ function rowGrouper(rows: readonly Row[], columnKey: string) {
124124 return Object . groupBy ( rows , ( r ) => r [ columnKey ] ) as Record < string , readonly R [ ] > ;
125125}
126126
127- function setup (
128- groupBy : string [ ] ,
129- generateGroupId ?: ( groupKey : string , parentId ?: string ) => string
130- ) {
131- page . render ( < TestGrid groupBy = { groupBy } generateGroupId = { generateGroupId } /> ) ;
127+ function setup ( groupBy : string [ ] , groupIdGetter ?: ( groupKey : string , parentId ?: string ) => string ) {
128+ page . render ( < TestGrid groupBy = { groupBy } groupIdGetter = { groupIdGetter } /> ) ;
132129}
133130
134131function getHeaderCellsContent ( ) {
@@ -162,13 +159,23 @@ test('should group by multiple columns', async () => {
162159 expect ( getRows ( ) ) . toHaveLength ( 4 ) ;
163160} ) ;
164161
165- test ( 'should group by multiple columns when passing generateGroupId' , ( ) => {
166- setup ( [ 'country' , 'year' ] , ( groupKey , parentId ) =>
162+ test ( 'should use groupIdGetter when provided' , async ( ) => {
163+ const groupIdGetter = vi . fn ( ( groupKey : string , parentId ?: string ) =>
167164 parentId !== undefined ? `${ groupKey } #${ parentId } ` : groupKey
168165 ) ;
166+ setup ( [ 'country' , 'year' ] , groupIdGetter ) ;
167+ expect ( groupIdGetter ) . toHaveBeenCalled ( ) ;
169168 expect ( getTreeGrid ( ) ) . toHaveAttribute ( 'aria-rowcount' , '13' ) ;
170169 expect ( getHeaderCellsContent ( ) ) . toStrictEqual ( [ '' , 'Country' , 'Year' , 'Sport' , 'Id' ] ) ;
171170 expect ( getRows ( ) ) . toHaveLength ( 4 ) ;
171+ groupIdGetter . mockClear ( ) ;
172+ await userEvent . click ( page . getByRole ( 'gridcell' , { name : 'USA' } ) ) ;
173+ expect ( getRows ( ) ) . toHaveLength ( 6 ) ;
174+ expect ( groupIdGetter ) . toHaveBeenCalled ( ) ;
175+ await userEvent . click ( page . getByRole ( 'gridcell' , { name : 'Canada' } ) ) ;
176+ expect ( getRows ( ) ) . toHaveLength ( 8 ) ;
177+ await userEvent . click ( page . getByRole ( 'gridcell' , { name : '2020' } ) ) ;
178+ expect ( getRows ( ) ) . toHaveLength ( 9 ) ;
172179} ) ;
173180
174181test ( 'should ignore duplicate groupBy columns' , async ( ) => {
@@ -194,20 +201,6 @@ test('should toggle group when group cell is clicked', async () => {
194201 expect ( getRows ( ) ) . toHaveLength ( 5 ) ;
195202} ) ;
196203
197- test ( 'should toggle group when group cell is clicked and is passing `generateGroupId` props' , async ( ) => {
198- setup ( [ 'country' , 'year' ] , ( groupKey , parentId ) =>
199- parentId !== undefined ? `${ groupKey } #${ parentId } ` : groupKey
200- ) ;
201- expect ( getTreeGrid ( ) ) . toHaveAttribute ( 'aria-rowcount' , '13' ) ;
202- expect ( getHeaderCellsContent ( ) ) . toStrictEqual ( [ '' , 'Country' , 'Year' , 'Sport' , 'Id' ] ) ;
203- await userEvent . click ( page . getByRole ( 'gridcell' , { name : 'USA' } ) ) ;
204- expect ( getRows ( ) ) . toHaveLength ( 6 ) ;
205- await userEvent . click ( page . getByRole ( 'gridcell' , { name : 'Canada' } ) ) ;
206- expect ( getRows ( ) ) . toHaveLength ( 8 ) ;
207- await userEvent . click ( page . getByRole ( 'gridcell' , { name : '2020' } ) ) ;
208- expect ( getRows ( ) ) . toHaveLength ( 9 ) ;
209- } ) ;
210-
211204test ( 'should toggle group using keyboard' , async ( ) => {
212205 setup ( [ 'year' ] ) ;
213206 expect ( getRows ( ) ) . toHaveLength ( 5 ) ;
0 commit comments