@@ -413,6 +413,17 @@ function headerCell(utils, tableId, sortKey) {
413413 . find ( th => th . querySelector ( `[data-sort-key="${ sortKey } "]` ) ) ;
414414}
415415
416+ /**
417+ * Finds all header cells in a rendered report table, in column order.
418+ * @param {object } utils - React Testing Library render result.
419+ * @param {string } tableId - Report table identifier.
420+ * @returns {Array<HTMLTableCellElement> } Matching table header cells.
421+ */
422+ function headerCells ( utils , tableId ) {
423+ return [ ...utils . container . querySelectorAll (
424+ `table[data-rf-table="${ tableId } "] thead th` ) ] ;
425+ }
426+
416427async function waitForPage ( utils , tableId , statusText ) {
417428 await waitFor ( ( ) => {
418429 expect ( paginationNav ( utils , tableId ) ?. textContent ?? '' ) . toContain ( statusText ) ;
@@ -462,21 +473,58 @@ describe('enhanced report tables: pagination', () => {
462473} ) ;
463474
464475describe ( 'enhanced report tables: sorting' , ( ) => {
465- test ( 'sorts ascending on first click, descending on second, updating aria-sort ' , async ( ) => {
476+ test ( 'initializes every Priority column sorted ascending with a visible up arrow ' , async ( ) => {
466477 respondJsonFor ( sampleJson ) ;
467478 const utils = await renderReport ( ) ;
468- const { fireEvent } = await import ( '@testing-library/react' ) ;
469479
470- const prioritiesOf = ( ) => tableRows ( utils , 'class-relationships' )
480+ // Static tables: sorted by priority asc out of the box (priority 1 first),
481+ // arrow signalling both the active sort and that headers are sortable.
482+ for ( const tableId of [ 'class-relationships' , 'package-relationships' , 'class-cycles-summary' ] ) {
483+ const th = headerCell ( utils , tableId , 'priority' ) ;
484+ expect ( th . getAttribute ( 'aria-sort' ) ) . toBe ( 'ascending' ) ;
485+ expect ( th . querySelector ( '.rf-sort-indicator' ) . textContent ) . toBe ( '▲' ) ;
486+ }
487+ // Rows are actually sorted: priority 1 first.
488+ const priorities = tableRows ( utils , 'class-relationships' )
471489 . map ( row => Number ( row . children [ 1 ] . textContent . trim ( ) ) ) ;
490+ expect ( priorities [ 0 ] ) . toBe ( 1 ) ;
491+ expect ( priorities [ 19 ] ) . toBe ( 20 ) ;
492+ // Disharmony tables carry the arrow on their Priority-labelled column.
493+ const godThs = headerCells ( utils , 'disharmony-GOD' ) ;
494+ const godPriority = godThs . find ( th => th . querySelector ( 'button' ) ?. textContent . includes ( 'Priority' ) ) ;
495+ expect ( godPriority . getAttribute ( 'aria-sort' ) ) . toBe ( 'ascending' ) ;
496+ expect ( godPriority . querySelector ( '.rf-sort-indicator' ) . textContent ) . toBe ( '▲' ) ;
497+ // Tables without a Priority column keep their original order untouched.
498+ for ( const th of headerCells ( utils , 'largest-cycle-breakdown' ) ) {
499+ expect ( th . getAttribute ( 'aria-sort' ) ) . toBe ( 'none' ) ;
500+ }
501+ } , SLOW_TEST_MS ) ;
472502
503+ test ( 'clicking the default-sorted Priority column flips it to descending' , async ( ) => {
504+ respondJsonFor ( sampleJson ) ;
505+ const utils = await renderReport ( ) ;
506+ const { fireEvent } = await import ( '@testing-library/react' ) ;
507+
508+ expect ( headerCell ( utils , 'class-relationships' , 'priority' ) . getAttribute ( 'aria-sort' ) )
509+ . toBe ( 'ascending' ) ;
473510 fireEvent . click ( headerCell ( utils , 'class-relationships' , 'priority' ) . querySelector ( 'button' ) ) ;
474511 await waitFor ( ( ) => {
475- expect ( headerCell ( utils , 'class-relationships' , 'priority' ) . getAttribute ( 'aria-sort' ) ) . toBe ( 'ascending' ) ;
512+ expect ( headerCell ( utils , 'class-relationships' , 'priority' ) . getAttribute ( 'aria-sort' ) )
513+ . toBe ( 'descending' ) ;
476514 } ) ;
477- expect ( prioritiesOf ( ) ) . toEqual ( [ ...Array ( 20 ) . keys ( ) ] . map ( i => i + 1 ) ) ;
478- expect ( headerCell ( utils , 'class-relationships' , 'priority' )
479- . querySelector ( '.rf-sort-indicator' ) . textContent ) . toBe ( '▲' ) ;
515+ } , SLOW_TEST_MS ) ;
516+
517+ test ( 'clicking Priority flips the default to descending, then back to ascending' , async ( ) => {
518+ respondJsonFor ( sampleJson ) ;
519+ const utils = await renderReport ( ) ;
520+ const { fireEvent } = await import ( '@testing-library/react' ) ;
521+
522+ /**
523+ * Reads the visible class relationship priorities in their current row order.
524+ * @returns {number[] } Priorities on the currently rendered page.
525+ */
526+ const prioritiesOf = ( ) => tableRows ( utils , 'class-relationships' )
527+ . map ( row => Number ( row . children [ 1 ] . textContent . trim ( ) ) ) ;
480528
481529 fireEvent . click ( headerCell ( utils , 'class-relationships' , 'priority' ) . querySelector ( 'button' ) ) ;
482530 await waitFor ( ( ) => {
@@ -485,17 +533,21 @@ describe('enhanced report tables: sorting', () => {
485533 expect ( prioritiesOf ( ) ) . toEqual ( [ ...Array ( 20 ) . keys ( ) ] . map ( i => 47 - i ) ) ;
486534 expect ( headerCell ( utils , 'class-relationships' , 'priority' )
487535 . querySelector ( '.rf-sort-indicator' ) . textContent ) . toBe ( '▼' ) ;
536+
537+ fireEvent . click ( headerCell ( utils , 'class-relationships' , 'priority' ) . querySelector ( 'button' ) ) ;
538+ await waitFor ( ( ) => {
539+ expect ( headerCell ( utils , 'class-relationships' , 'priority' ) . getAttribute ( 'aria-sort' ) ) . toBe ( 'ascending' ) ;
540+ } ) ;
541+ expect ( prioritiesOf ( ) ) . toEqual ( [ ...Array ( 20 ) . keys ( ) ] . map ( i => i + 1 ) ) ;
542+ expect ( headerCell ( utils , 'class-relationships' , 'priority' )
543+ . querySelector ( '.rf-sort-indicator' ) . textContent ) . toBe ( '▲' ) ;
488544 } , SLOW_TEST_MS ) ;
489545
490546 test ( 'applies the sort across the whole dataset and keeps it while paging' , async ( ) => {
491547 respondJsonFor ( sampleJson ) ;
492548 const utils = await renderReport ( ) ;
493549 const { fireEvent } = await import ( '@testing-library/react' ) ;
494550
495- fireEvent . click ( headerCell ( utils , 'class-relationships' , 'priority' ) . querySelector ( 'button' ) ) ;
496- await waitFor ( ( ) => {
497- expect ( headerCell ( utils , 'class-relationships' , 'priority' ) . getAttribute ( 'aria-sort' ) ) . toBe ( 'ascending' ) ;
498- } ) ;
499551 fireEvent . click ( headerCell ( utils , 'class-relationships' , 'priority' ) . querySelector ( 'button' ) ) ;
500552 await waitFor ( ( ) => {
501553 expect ( headerCell ( utils , 'class-relationships' , 'priority' ) . getAttribute ( 'aria-sort' ) ) . toBe ( 'descending' ) ;
@@ -660,10 +712,6 @@ describe('enhanced report tables: CSV export', () => {
660712
661713 // Sort by priority descending so the export order is observable.
662714 fireEvent . click ( headerCell ( utils , 'class-relationships' , 'priority' ) . querySelector ( 'button' ) ) ;
663- await waitFor ( ( ) => {
664- expect ( headerCell ( utils , 'class-relationships' , 'priority' ) . getAttribute ( 'aria-sort' ) ) . toBe ( 'ascending' ) ;
665- } ) ;
666- fireEvent . click ( headerCell ( utils , 'class-relationships' , 'priority' ) . querySelector ( 'button' ) ) ;
667715 await waitFor ( ( ) => {
668716 expect ( headerCell ( utils , 'class-relationships' , 'priority' ) . getAttribute ( 'aria-sort' ) ) . toBe ( 'descending' ) ;
669717 } ) ;
0 commit comments