@@ -691,42 +691,27 @@ describe('Moisture Content Screen page', () => {
691691 it ( 'Check Calculate average button functionality' , ( ) => {
692692 cy . intercept (
693693 'POST' ,
694- '**/api/moisture-content-cone/514330/calculate-average'
694+ '**/api/moisture-content-cone/514330/calculate-average' ,
695+ { fixture : 'moisture-content-cal-avg.json' } // <-- 171.0
695696 ) . as ( 'postCalcAvg' ) ;
696697
698+ // Ensure table is loaded
697699 cy . waitForTableData ( '.activity-result-container' ) ;
698700
699- cy . get ( '.activity-result-container tbody tr' ) . then ( ( $rows ) => {
700- const mcValues : number [ ] = [ ] ;
701+ // Click Calculate Average
702+ cy . contains ( 'button' , 'Calculate average' ) . click ( ) ;
701703
702- Cypress . $ ( $rows ) . each ( ( _ , row ) => {
703- const mcText = Cypress . $ ( row )
704- . find ( 'td:nth-child(7)' )
705- . text ( )
706- . trim ( ) ;
707-
708- const mcNum = parseFloat ( mcText ) ;
709- if ( ! Number . isNaN ( mcNum ) ) {
710- mcValues . push ( mcNum ) ;
711- }
712- } ) ;
713-
714- expect ( mcValues . length , 'MC values should exist' ) . to . be . greaterThan ( 0 ) ;
715-
716- const sum = mcValues . reduce ( ( acc , val ) => acc + val , 0 ) ;
717- const averageMcValues = sum / mcValues . length ;
718-
719- // Click AFTER calculation
720- cy . contains ( 'button' , 'Calculate average' ) . click ( ) ;
704+ // Wait for API and assert response
705+ cy . wait ( '@postCalcAvg' ) . then ( ( { response } ) => {
706+ expect ( response ?. statusCode ) . to . eq ( 200 ) ;
721707
722- cy . wait ( '@postCalcAvg' )
723- . its ( 'response.statusCode' )
724- . should ( 'eq' , 200 ) ;
708+ // Because backend returns a NUMBER, not an object
709+ const averageMc = response ! . body as number ;
725710
726- // Assert UI inside same chain
711+ // Assert UI reflects backend value
727712 cy . get ( '.activity-summary-info-value' )
728713 . eq ( 4 )
729- . should ( 'have.text' , averageMcValues . toFixed ( 2 ) ) ;
714+ . should ( 'have.text' , averageMc . toFixed ( 2 ) ) ;
730715 } ) ;
731716 } ) ;
732717
0 commit comments