Skip to content

Commit 96da650

Browse files
committed
Update icon_border_progress to v1.4.1
1 parent 130c6ce commit 96da650

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

modules/icon_border_progress/__tests__/code.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,26 @@ describe("icon_border_progress", () => {
264264
);
265265
});
266266

267+
// https://github.com/Clooos/Bubble-Card/discussions/1525#discussioncomment-13732526
268+
// Ensure inputs accept floats
269+
it("should calculate progress percentage correctly for floats", () => {
270+
mockHass.states["sensor.progress"].state = "0.7";
271+
mockHass.states["sensor.start"].state = "0.2";
272+
mockHass.states["sensor.end"].state = "0.8";
273+
274+
icon_border_progress.call(mockThis, mockCard, mockHass);
275+
276+
// Verify SVG helper was called with correct progress value (75%)
277+
expect(strokeDashProgress.createProgressBorder).toHaveBeenCalledWith(
278+
mockElement,
279+
expect.closeTo(500 / 6), // 75% progress
280+
"rgb(255, 255, 0)",
281+
"white",
282+
283+
{ animationDuration: 800, borderRadiusOverride: undefined, offsetPercent: 0, strokeWidth: 3 },
284+
);
285+
});
286+
267287
it("should handle custom start and end values", () => {
268288
mockHass.states["sensor.progress"].state = "150";
269289
mockHass.states["sensor.start"].state = "100";

modules/icon_border_progress/__tests__/integration.test.js

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
import { jest } from "@jest/globals";
1111
import { 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+
}
1324
describe("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

modules/icon_border_progress/code.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export function icon_border_progress(card, hass) {
3232

3333
function calculateProgressValue(progressSource, buttonConfig) {
3434
let progressValue = parseFloat(getState(progressSource));
35-
let startValue = parseInt(getState(buttonConfig.start));
36-
let endValue = parseInt(getState(buttonConfig.end));
35+
let startValue = parseFloat(getState(buttonConfig.start));
36+
let endValue = parseFloat(getState(buttonConfig.end));
3737

3838
startValue = isNaN(startValue) ? 0 : startValue;
3939
endValue = isNaN(endValue) ? 100 : endValue;

modules/icon_border_progress/module.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module_info:
22
id: icon_border_progress
33
name: Icon Border Progress
4-
version: 1.4.0
4+
version: 1.4.1
55
creator: lsmarsden
66
link: >-
77
https://github.com/lsmarsden/bubble-card-modules/tree/main/icon_border_progress

0 commit comments

Comments
 (0)