1010import { jest } from "@jest/globals" ;
1111import { icon_border_progress } from "../code.js" ;
1212
13+ function verifyStrokeDashArray ( progressPath , ...values ) {
14+ expect ( progressPath ) . toBeTruthy ( ) ;
15+ const strokeDashArray = progressPath . getAttribute ( "stroke-dasharray" ) ;
16+ expect ( strokeDashArray ) . toBeTruthy ( ) ;
17+ const dashArrayValues = strokeDashArray . split ( " " ) . map ( Number ) ;
18+ expect ( dashArrayValues . length ) . toBe ( values . length ) ;
19+ // verify all values match expected using toBeCloseTo for floats
20+ values . forEach ( ( value , index ) => {
21+ expect ( dashArrayValues [ index ] ) . toBeCloseTo ( value ) ; // 2 d.p. default
22+ } ) ;
23+ }
1324describe ( "icon_border_progress - Integration Tests" , ( ) => {
1425 let mockCard , mockThis , mockHass ;
1526
@@ -442,7 +453,8 @@ describe("icon_border_progress - Integration Tests", () => {
442453 mockHass . states [ "sensor.start_value" ] = { state : "0" } ;
443454 mockHass . states [ "sensor.end_value" ] = { state : "200" } ;
444455
445- // Exercise - Execute the module function
456+ // Exercise - Execute the module function twice to verify progress after initial load
457+ icon_border_progress . call ( mockThis , mockCard , mockHass ) ;
446458 icon_border_progress . call ( mockThis , mockCard , mockHass ) ;
447459
448460 // Verify - Check SVG progress border exists
@@ -455,7 +467,46 @@ describe("icon_border_progress - Integration Tests", () => {
455467 const progressPath = svg . querySelector ( ".progress-path" ) ;
456468 expect ( progressPath ) . toBeTruthy ( ) ;
457469 expect ( progressPath . getAttribute ( "stroke" ) ) . not . toBe ( "transparent" ) ;
458- expect ( progressPath . getAttribute ( "stroke-dasharray" ) ) . toBeTruthy ( ) ;
470+ verifyStrokeDashArray ( progressPath , 0 , 0 , 112.5 , 187.5 ) ;
471+ expect ( subButton1 . style . background ) . toBe ( "rgb(10, 10, 10)" ) ; // JSDOM converts #0a0a0a to rgb
472+ } ) ;
473+
474+ it ( "should render progress with custom float ranges" , ( ) => {
475+ mockThis . config . icon_border_progress = [
476+ {
477+ button : "sub-button-1" ,
478+ source : "sensor.saros_10_battery" ,
479+ start : "sensor.start_value" ,
480+ end : "sensor.end_value" ,
481+ color_stops : [
482+ { percent : 0 , color : "#424242" } ,
483+ { percent : 100 , color : "#eeeeee" } ,
484+ ] ,
485+ remaining_color : "#222" ,
486+ background_color : "#0a0a0a" ,
487+ } ,
488+ ] ;
489+
490+ // Battery is at 0.7, with start=0.2 and end=1.2, progress should be 50%
491+ mockHass . states [ "sensor.saros_10_battery" ] . state = "0.7" ;
492+ mockHass . states [ "sensor.start_value" ] = { state : "0.2" } ;
493+ mockHass . states [ "sensor.end_value" ] = { state : "1.2" } ;
494+
495+ // Exercise - Execute the module function twice to verify progress after initial load
496+ icon_border_progress . call ( mockThis , mockCard , mockHass ) ;
497+ icon_border_progress . call ( mockThis , mockCard , mockHass ) ;
498+
499+ // Verify - Check SVG progress border exists
500+ const subButton1 = mockCard . querySelector ( ".bubble-sub-button-1" ) ;
501+ expect ( subButton1 ) . toBeTruthy ( ) ;
502+
503+ const svg = subButton1 . querySelector ( ".stroke-dash-aligned-svg" ) ;
504+ expect ( svg ) . toBeTruthy ( ) ;
505+
506+ const progressPath = svg . querySelector ( ".progress-path" ) ;
507+ expect ( progressPath ) . toBeTruthy ( ) ;
508+ expect ( progressPath . getAttribute ( "stroke" ) ) . toBe ( "#424242" ) ;
509+ verifyStrokeDashArray ( progressPath , 0 , 0 , 150 , 150 ) ; // 50% of 300px path length
459510 expect ( subButton1 . style . background ) . toBe ( "rgb(10, 10, 10)" ) ; // JSDOM converts #0a0a0a to rgb
460511 } ) ;
461512
0 commit comments