@@ -3,14 +3,17 @@ import { render, screen } from '@testing-library/react';
33import userEvent from '@testing-library/user-event' ;
44
55import DataGrid , {
6+ Cell ,
67 DataGridDefaultRenderersProvider ,
8+ Row as DefaultRow ,
79 renderSortIcon ,
810 SelectColumn
911} from '../../src' ;
1012import type {
1113 CellRendererProps ,
1214 Column ,
1315 DataGridProps ,
16+ RenderRowProps ,
1417 RenderSortStatusProps ,
1518 SortColumn
1619} from '../../src' ;
@@ -38,39 +41,39 @@ const columns: readonly Column<Row>[] = [
3841 }
3942] ;
4043
41- function globalCellRenderer ( key : React . Key , props : CellRendererProps < Row , unknown > ) {
42- return (
43- < div key = { key } role = "gridcell" >
44- { props . row [ props . column . key as keyof Row ] }
45- </ div >
46- ) ;
44+ function renderGlobalCell ( key : React . Key , props : CellRendererProps < Row , unknown > ) {
45+ return < Cell key = { key } { ...props } className = "global" style = { { fontStyle : 'italic' } } /> ;
4746}
4847
49- function localCellRenderer ( key : React . Key ) {
50- return (
51- < div key = { key } role = "gridcell" >
52- local
53- </ div >
54- ) ;
48+ function renderLocalCell ( key : React . Key , props : CellRendererProps < Row , unknown > ) {
49+ return < Cell key = { key } { ...props } className = "local" style = { { fontStyle : 'normal' } } /> ;
50+ }
51+
52+ function renderGlobalRow ( key : React . Key , props : RenderRowProps < Row > ) {
53+ return < DefaultRow key = { key } { ...props } className = "global" /> ;
54+ }
55+
56+ function renderLocalRow ( key : React . Key , props : RenderRowProps < Row > ) {
57+ return < DefaultRow key = { key } { ...props } className = "local" /> ;
5558}
5659
5760function NoRowsFallback ( ) {
5861 return < div > Local no rows fallback</ div > ;
5962}
6063
61- function GlobalNoRowsFallback ( ) {
64+ function NoRowsGlobalFallback ( ) {
6265 return < div > Global no rows fallback</ div > ;
6366}
6467
65- function localRenderCheckbox ( ) {
68+ function renderLocalCheckbox ( ) {
6669 return < div > Local checkbox</ div > ;
6770}
6871
69- function globalRenderCheckbox ( ) {
72+ function renderGlobalCheckbox ( ) {
7073 return < div > Global checkbox</ div > ;
7174}
7275
73- function globalSortStatus ( { sortDirection, priority } : RenderSortStatusProps ) {
76+ function renderGlobalSortStatus ( { sortDirection, priority } : RenderSortStatusProps ) {
7477 return (
7578 < >
7679 { renderSortIcon ( { sortDirection } ) }
@@ -79,7 +82,7 @@ function globalSortStatus({ sortDirection, priority }: RenderSortStatusProps) {
7982 ) ;
8083}
8184
82- function renderSortStatus ( { sortDirection, priority } : RenderSortStatusProps ) {
85+ function renderLocalSortStatus ( { sortDirection, priority } : RenderSortStatusProps ) {
8386 return (
8487 < >
8588 { renderSortIcon ( { sortDirection } ) }
@@ -98,10 +101,11 @@ function setupProvider<R, SR, K extends React.Key>(props: DataGridProps<R, SR, K
98101 return render (
99102 < DataGridDefaultRenderersProvider
100103 value = { {
101- noRowsFallback : < GlobalNoRowsFallback /> ,
102- renderCheckbox : globalRenderCheckbox ,
103- renderSortStatus : globalSortStatus ,
104- renderCell : globalCellRenderer
104+ noRowsFallback : < NoRowsGlobalFallback /> ,
105+ renderCheckbox : renderGlobalCheckbox ,
106+ renderSortStatus : renderGlobalSortStatus ,
107+ renderCell : renderGlobalCell ,
108+ renderRow : renderGlobalRow
105109 } }
106110 >
107111 < TestGrid { ...props } />
@@ -133,7 +137,7 @@ test('fallback defined using both provider and renderers with no rows', () => {
133137test ( 'fallback defined using renderers prop with a row' , ( ) => {
134138 setup ( {
135139 columns,
136- rows : [ { id : 1 , col1 : 'col 1 value ' , col2 : 'col 2 value ' } ] ,
140+ rows : [ { id : 1 , col1 : 'value 1 ' , col2 : 'value 2 ' } ] ,
137141 renderers : { noRowsFallback : < NoRowsFallback /> }
138142 } ) ;
139143
@@ -142,7 +146,7 @@ test('fallback defined using renderers prop with a row', () => {
142146} ) ;
143147
144148test ( 'fallback defined using provider with a row' , ( ) => {
145- setupProvider ( { columns, rows : [ { id : 1 , col1 : 'col 1 value ' , col2 : 'col 2 value ' } ] } ) ;
149+ setupProvider ( { columns, rows : [ { id : 1 , col1 : 'value 1 ' , col2 : 'value 2 ' } ] } ) ;
146150
147151 expect ( getRows ( ) ) . toHaveLength ( 1 ) ;
148152 expect ( screen . queryByText ( 'Global no rows fallback' ) ) . not . toBeInTheDocument ( ) ;
@@ -151,7 +155,7 @@ test('fallback defined using provider with a row', () => {
151155test ( 'fallback defined using both provider and renderers with a row' , ( ) => {
152156 setupProvider ( {
153157 columns,
154- rows : [ { id : 1 , col1 : 'col 1 value ' , col2 : 'col 2 value ' } ] ,
158+ rows : [ { id : 1 , col1 : 'value 1 ' , col2 : 'value 2 ' } ] ,
155159 renderers : { noRowsFallback : < NoRowsFallback /> }
156160 } ) ;
157161
@@ -161,7 +165,7 @@ test('fallback defined using both provider and renderers with a row', () => {
161165} ) ;
162166
163167test ( 'checkbox defined using renderers prop' , ( ) => {
164- setup ( { columns, rows : noRows , renderers : { renderCheckbox : localRenderCheckbox } } ) ;
168+ setup ( { columns, rows : noRows , renderers : { renderCheckbox : renderLocalCheckbox } } ) ;
165169
166170 expect ( getRows ( ) ) . toHaveLength ( 0 ) ;
167171 expect ( screen . getByText ( 'Local checkbox' ) ) . toBeInTheDocument ( ) ;
@@ -175,7 +179,7 @@ test('checkbox defined using provider', () => {
175179} ) ;
176180
177181test ( 'checkbox defined using both provider and renderers' , ( ) => {
178- setupProvider ( { columns, rows : noRows , renderers : { renderCheckbox : localRenderCheckbox } } ) ;
182+ setupProvider ( { columns, rows : noRows , renderers : { renderCheckbox : renderLocalCheckbox } } ) ;
179183
180184 expect ( getRows ( ) ) . toHaveLength ( 0 ) ;
181185 expect ( screen . getByText ( 'Local checkbox' ) ) . toBeInTheDocument ( ) ;
@@ -199,7 +203,7 @@ test('sortPriority defined using both providers', async () => {
199203} ) ;
200204
201205test ( 'sortPriority defined using both providers and renderers' , async ( ) => {
202- setupProvider ( { columns, rows : noRows , renderers : { renderSortStatus } } ) ;
206+ setupProvider ( { columns, rows : noRows , renderers : { renderSortStatus : renderLocalSortStatus } } ) ;
203207
204208 const [ , headerCell2 , headerCell3 ] = getHeaderCells ( ) ;
205209 const user = userEvent . setup ( ) ;
@@ -215,22 +219,55 @@ test('sortPriority defined using both providers and renderers', async () => {
215219} ) ;
216220
217221test ( 'renderCell defined using provider' , ( ) => {
218- setupProvider ( { columns, rows : [ { id : 1 , col1 : 'col 1 value ' , col2 : 'col 2 value ' } ] } ) ;
222+ setupProvider ( { columns, rows : [ { id : 1 , col1 : 'value 1 ' , col2 : 'value 2 ' } ] } ) ;
219223
220224 const [ , cell1 , cell2 ] = getCells ( ) ;
221- expect ( cell1 ) . toHaveTextContent ( 'col 1 value' ) ;
222- expect ( cell2 ) . toHaveTextContent ( 'col 2 value' ) ;
225+ expect ( cell1 ) . toHaveTextContent ( 'value 1' ) ;
226+ expect ( cell1 ) . toHaveClass ( 'global' ) ;
227+ expect ( cell1 ) . not . toHaveClass ( 'local' ) ;
228+ expect ( cell1 ) . toHaveStyle ( { fontStyle : 'italic' } ) ;
229+
230+ expect ( cell2 ) . toHaveTextContent ( 'value 2' ) ;
231+ expect ( cell2 ) . toHaveClass ( 'global' ) ;
232+ expect ( cell2 ) . not . toHaveClass ( 'local' ) ;
233+ expect ( cell2 ) . toHaveStyle ( { fontStyle : 'italic' } ) ;
223234} ) ;
224235
225236test ( 'renderCell defined using both providers and renderers' , ( ) => {
226237 setupProvider ( {
227238 columns,
228- rows : [ { id : 1 , col1 : 'col 1 value' , col2 : 'col 2 value' } ] ,
229- renderers : { renderCell : localCellRenderer }
239+ rows : [ { id : 1 , col1 : 'value 1' , col2 : 'value 2' } ] ,
240+ renderers : { renderCell : renderLocalCell }
241+ } ) ;
242+
243+ const [ , cell1 , cell2 ] = getCells ( ) ;
244+ expect ( cell1 ) . toHaveTextContent ( 'value 1' ) ;
245+ expect ( cell1 ) . toHaveClass ( 'local' ) ;
246+ expect ( cell1 ) . not . toHaveClass ( 'global' ) ;
247+ expect ( cell1 ) . toHaveStyle ( { fontStyle : 'normal' } ) ;
248+
249+ expect ( cell2 ) . toHaveTextContent ( 'value 2' ) ;
250+ expect ( cell2 ) . toHaveClass ( 'local' ) ;
251+ expect ( cell2 ) . not . toHaveClass ( 'global' ) ;
252+ expect ( cell2 ) . toHaveStyle ( { fontStyle : 'normal' } ) ;
253+ } ) ;
254+
255+ test ( 'renderRow defined using provider' , ( ) => {
256+ setupProvider ( { columns, rows : [ { id : 1 , col1 : 'value 1' , col2 : 'value 2' } ] } ) ;
257+
258+ const [ row ] = getRows ( ) ;
259+ expect ( row ) . toHaveClass ( 'global' ) ;
260+ expect ( row ) . not . toHaveClass ( 'local' ) ;
261+ } ) ;
262+
263+ test ( 'renderRow defined using both providers and renderers' , ( ) => {
264+ setupProvider ( {
265+ columns,
266+ rows : [ { id : 1 , col1 : 'value 1' , col2 : 'value 2' } ] ,
267+ renderers : { renderRow : renderLocalRow }
230268 } ) ;
231269
232- const [ selectCell , cell1 , cell2 ] = getCells ( ) ;
233- expect ( selectCell ) . toHaveTextContent ( 'local' ) ;
234- expect ( cell1 ) . toHaveTextContent ( 'local' ) ;
235- expect ( cell2 ) . toHaveTextContent ( 'local' ) ;
270+ const [ row ] = getRows ( ) ;
271+ expect ( row ) . toHaveClass ( 'local' ) ;
272+ expect ( row ) . not . toHaveClass ( 'global' ) ;
236273} ) ;
0 commit comments