@@ -48,7 +48,7 @@ describe('<Table />', async () => {
4848
4949 const renderTable = ( props ?: TableProps ) =>
5050 render (
51- < Table caption = " Test table" { ...props } >
51+ < Table caption = { ( ) => ' Test table' } { ...props } >
5252 < Table . Head >
5353 < Table . Row >
5454 < Table . ColHeader id = "foo" > ColHeader</ Table . ColHeader >
@@ -82,7 +82,7 @@ describe('<Table />', async () => {
8282 it ( 'applies a fixed column layout' , async ( ) => {
8383 await renderTable ( {
8484 layout : 'fixed' ,
85- caption : 'Test table'
85+ caption : ( ) => 'Test table'
8686 } )
8787 const table = screen . getByRole ( 'table' )
8888
@@ -92,7 +92,7 @@ describe('<Table />', async () => {
9292 it ( 'passes hover to table row' , async ( ) => {
9393 renderTable ( {
9494 hover : true ,
95- caption : 'Test table'
95+ caption : ( ) => 'Test table'
9696 } )
9797 const tableRows = screen . getAllByRole ( 'row' )
9898
@@ -123,7 +123,7 @@ describe('<Table />', async () => {
123123 it ( 'can render table in stacked layout' , async ( ) => {
124124 renderTable ( {
125125 layout : 'stacked' ,
126- caption : 'Test table'
126+ caption : ( ) => 'Test table'
127127 } )
128128 const stackedTable = screen . getByRole ( 'table' )
129129
@@ -135,7 +135,7 @@ describe('<Table />', async () => {
135135
136136 it ( 'can handle non-existent head in stacked layout' , async ( ) => {
137137 render (
138- < Table caption = " Test table" layout = "stacked" >
138+ < Table caption = { ( ) => ' Test table' } layout = "stacked" >
139139 < Table . Body > </ Table . Body >
140140 </ Table >
141141 )
@@ -146,7 +146,7 @@ describe('<Table />', async () => {
146146
147147 it ( 'can handle empty head in stacked layout' , async ( ) => {
148148 render (
149- < Table caption = " Test table" layout = "stacked" >
149+ < Table caption = { ( ) => ' Test table' } layout = "stacked" >
150150 < Table . Head > </ Table . Head >
151151 </ Table >
152152 )
@@ -157,7 +157,7 @@ describe('<Table />', async () => {
157157
158158 it ( 'can handle invalid header in stacked layout' , async ( ) => {
159159 render (
160- < Table caption = " Test table" layout = "stacked" >
160+ < Table caption = { ( ) => ' Test table' } layout = "stacked" >
161161 < Table . Head >
162162 < Table . Row >
163163 < Table . Cell > Foo</ Table . Cell >
@@ -189,7 +189,7 @@ describe('<Table />', async () => {
189189
190190 it ( 'does not crash for invalid children in stacked layout' , async ( ) => {
191191 render (
192- < Table caption = " Test table" layout = "stacked" >
192+ < Table caption = { ( ) => ' Test table' } layout = "stacked" >
193193 test1
194194 < span > test</ span >
195195 { /* @ts -ignore error is normal here */ }
@@ -233,7 +233,7 @@ describe('<Table />', async () => {
233233 layout : TableProps [ 'layout' ] = 'auto'
234234 ) =>
235235 render (
236- < Table caption = " Sortable table" layout = { layout } >
236+ < Table caption = { ( ) => ' Sortable table' } layout = { layout } >
237237 < Table . Head >
238238 < Table . Row >
239239 < Table . ColHeader id = "foo" { ...props } { ...handlers } >
@@ -348,6 +348,39 @@ describe('<Table />', async () => {
348348
349349 expect ( header ) . toHaveAttribute ( 'aria-sort' , 'descending' )
350350 } )
351+
352+ it ( 'calls the caption function with the sorted header and direction' , async ( ) => {
353+ const caption = vi . fn ( ( header : string , direction : string ) =>
354+ header ? `Movies, sorted by ${ header } ${ direction } ` : 'Movies'
355+ )
356+ const { container } = render (
357+ < Table caption = { caption } >
358+ < Table . Head >
359+ < Table . Row >
360+ < Table . ColHeader id = "foo" sortDirection = "ascending" >
361+ Foo
362+ </ Table . ColHeader >
363+ < Table . ColHeader id = "bar" > Bar</ Table . ColHeader >
364+ </ Table . Row >
365+ </ Table . Head >
366+ < Table . Body >
367+ < Table . Row > </ Table . Row >
368+ </ Table . Body >
369+ </ Table >
370+ )
371+
372+ expect ( caption ) . toHaveBeenCalledWith ( 'Foo' , 'ascending' )
373+ expect ( container . querySelector ( 'caption' ) ) . toHaveTextContent (
374+ 'Movies, sorted by Foo ascending'
375+ )
376+ } )
377+
378+ it ( 'calls the caption function with an empty header and "none" when nothing is sorted' , async ( ) => {
379+ const caption = vi . fn ( ( ) => 'Movies' )
380+ renderTable ( { caption } as Partial < TableProps > as TableProps )
381+
382+ expect ( caption ) . toHaveBeenCalledWith ( '' , 'none' )
383+ } )
351384 } )
352385
353386 describe ( 'when using custom components' , ( ) => {
@@ -369,7 +402,7 @@ describe('<Table />', async () => {
369402 }
370403 }
371404 const table = render (
372- < Table caption = " Test custom table" >
405+ < Table caption = { ( ) => ' Test custom table' } >
373406 < Table . Head >
374407 < Table . Row >
375408 < Table . ColHeader id = "foo" > ColHeader</ Table . ColHeader >
@@ -410,7 +443,7 @@ describe('<Table />', async () => {
410443 }
411444
412445 const table = render (
413- < Table caption = " Test custom table" >
446+ < Table caption = { ( ) => ' Test custom table' } >
414447 < Table . Head >
415448 < CustomTableRow >
416449 < CustomTableCell id = "foo" > ColHeader</ CustomTableCell >
0 commit comments