@@ -69,16 +69,7 @@ describe("Table", () => {
6969 { key : "Age" , label : "Age" , sortable : true } ,
7070 ] ;
7171
72- const onPageChangeMock = vi . fn ( ) ;
73-
74- const paginationOptions = {
75- pageSize : 4 ,
76- currentPage : 1 ,
77- onPageChange : onPageChangeMock ,
78- totalRows : data . length ,
79- } ;
80-
81- render ( < Table data = { data } columns = { columns } /> ) ;
72+ render ( < Table data = { data } columns = { columns } /> ) ;
8273
8374 // First click on Age sorts by Age ascending
8475 fireEvent . click ( screen . getByText ( "Age" ) ) ;
@@ -216,4 +207,62 @@ describe("Table", () => {
216207 // Restore the original console.error
217208 consoleErrorSpy . mockRestore ( ) ;
218209 } ) ;
210+
211+ it ( "applies defaultSort correctly and renders rows in the expected order" , ( ) => {
212+ const data = [
213+ { id : 1 , firstName : "Alice" , lastName : "Smith" , age : 25 } ,
214+ { id : 2 , firstName : "Bob" , lastName : "Jones" , age : 30 } ,
215+ { id : 3 , firstName : "Charlie" , lastName : "Smith" , age : 20 } ,
216+ { id : 4 , firstName : "Dave" , lastName : "Jones" , age : 40 } ,
217+ { id : 5 , firstName : "Eva" , lastName : "Brown" , age : 35 } ,
218+ ] ;
219+
220+ const columns = [
221+ { key : "firstName" , label : "First Name" , sortable : true } ,
222+ { key : "lastName" , label : "Last Name" , sortable : true } ,
223+ { key : "age" , label : "Age" , sortable : true } ,
224+ ] ;
225+
226+ const defaultSort = [
227+ { key : "lastName" , direction : "asc" } ,
228+ { key : "firstName" , direction : "desc" } ,
229+ ] ;
230+
231+ const onPageChangeMock = vi . fn ( ) ;
232+
233+ const paginationOptions = {
234+ pageSize : 5 ,
235+ currentPage : 1 ,
236+ onPageChange : onPageChangeMock ,
237+ totalRows : data . length ,
238+ } ;
239+
240+ render (
241+ < Table
242+ data = { data }
243+ columns = { columns }
244+ defaultSort = { defaultSort }
245+ paginationOptions = { paginationOptions }
246+ /> ,
247+ ) ;
248+
249+ // Get all data rows (excluding the header row)
250+ const rows = screen . getAllByRole ( "row" ) . slice ( 1 ) ; // Remove header row
251+
252+ // Expected order based on defaultSort
253+ const expectedOrder = [
254+ { firstName : "Eva" , lastName : "Brown" } ,
255+ { firstName : "Dave" , lastName : "Jones" } ,
256+ { firstName : "Bob" , lastName : "Jones" } ,
257+ { firstName : "Charlie" , lastName : "Smith" } ,
258+ { firstName : "Alice" , lastName : "Smith" } ,
259+ ] ;
260+
261+ // Assert that the rows are in the expected order
262+ expectedOrder . forEach ( ( expectedData , index ) => {
263+ const row = rows [ index ] ;
264+ expect ( within ( row ) . getByText ( expectedData . firstName ) ) . toBeInTheDocument ( ) ;
265+ expect ( within ( row ) . getByText ( expectedData . lastName ) ) . toBeInTheDocument ( ) ;
266+ } ) ;
267+ } ) ;
219268} ) ;
0 commit comments