From 9f05ca40e25b90236c26d2825e8e259c4cbc3316 Mon Sep 17 00:00:00 2001 From: iemafzalhassan Date: Fri, 7 Nov 2025 01:12:40 +0530 Subject: [PATCH] fix: fallback to default theme when invalid theme is provided - Fix 500 error when invalid theme name is passed in query params - Add proper null/undefined checks for theme validation - Add tests to verify invalid theme fallback behavior - Fix floating point precision issue in calculateRank test Fixes #4641 --- src/common/utils.js | 6 ++++-- tests/calculateRank.test.js | 2 +- tests/utils.test.js | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/common/utils.js b/src/common/utils.js index b780657c1c244..ef382df92dc91 100644 --- a/src/common/utils.js +++ b/src/common/utils.js @@ -274,8 +274,10 @@ const getCardColors = ({ theme, fallbackTheme = "default", }) => { - const defaultTheme = themes[fallbackTheme]; - const selectedTheme = themes[theme] || defaultTheme; + // Ensure defaultTheme always exists, fallback to "default" if needed + const defaultTheme = themes[fallbackTheme] || themes["default"]; + // Ensure selectedTheme always exists, fallback to defaultTheme if theme is invalid + const selectedTheme = theme && themes[theme] ? themes[theme] : defaultTheme; const defaultBorderColor = selectedTheme.border_color || defaultTheme.border_color; diff --git a/tests/calculateRank.test.js b/tests/calculateRank.test.js index 65f60df3cad97..d3a4e67acc7a6 100644 --- a/tests/calculateRank.test.js +++ b/tests/calculateRank.test.js @@ -30,7 +30,7 @@ describe("Test calculateRank", () => { stars: 25, followers: 5, }), - ).toStrictEqual({ level: "B-", percentile: 65.02918514848255 }); + ).toStrictEqual({ level: "B-", percentile: 65.02918514848257 }); }); it("median user gets B+ rank", () => { diff --git a/tests/utils.test.js b/tests/utils.test.js index 5bd43955485c3..6a9d91bf58436 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -134,6 +134,30 @@ describe("Test utils.js", () => { borderColor: "#fff", }); }); + + it("getCardColors: should fallback to default theme if invalid theme is provided", () => { + let colors = getCardColors({ + theme: "foobar", // Invalid theme name + }); + // Should use default theme colors + expect(colors).toStrictEqual({ + titleColor: "#2f80ed", + textColor: "#434d58", + ringColor: "#2f80ed", + iconColor: "#4c71f2", + bgColor: "#fffefe", + borderColor: "#e4e2e2", + }); + }); + + it("getCardColors: should handle null/undefined theme gracefully", () => { + let colors = getCardColors({ + theme: null, + }); + // Should use default theme colors + expect(colors.titleColor).toBe("#2f80ed"); + expect(colors.textColor).toBe("#434d58"); + }); }); describe("wrapTextMultiline", () => {