From 889b216a93e63374b431c9cfcf78e11aab3180cb Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 24 Oct 2024 10:22:43 +0100 Subject: [PATCH] Add theme tests --- packages/shared-core/src/tests/themes.ts | 28 ++++++++++++++++++++++++ packages/shared-core/src/themes.ts | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 packages/shared-core/src/tests/themes.ts diff --git a/packages/shared-core/src/tests/themes.ts b/packages/shared-core/src/tests/themes.ts new file mode 100644 index 0000000000..89c6910ad5 --- /dev/null +++ b/packages/shared-core/src/tests/themes.ts @@ -0,0 +1,28 @@ +import { getThemeClassNames, ensureValidTheme } from "../themes" +import { Theme } from "@budibase/types" + +describe("theme class names", () => { + it("generates class names for a theme without base theme", () => { + expect(getThemeClassNames(Theme.LIGHT)).toStrictEqual("spectrum--light") + }) + it("generates class names for a theme with base theme", () => { + expect(getThemeClassNames(Theme.NORD)).toStrictEqual( + "spectrum--darkest spectrum--nord" + ) + }) +}) + +describe("theme validity checking", () => { + it("handles no theme", () => { + expect(ensureValidTheme(undefined)).toStrictEqual(Theme.DARKEST) + }) + it("allows specifiying a fallback", () => { + expect(ensureValidTheme(undefined, Theme.NORD)).toStrictEqual(Theme.NORD) + }) + it("migrates lightest to light", () => { + expect(ensureValidTheme(Theme.LIGHTEST)).toStrictEqual(Theme.LIGHT) + }) + it("migrates dark to darkest", () => { + expect(ensureValidTheme(Theme.DARK)).toStrictEqual(Theme.DARKEST) + }) +}) diff --git a/packages/shared-core/src/themes.ts b/packages/shared-core/src/themes.ts index 1d1875d4e9..d70f188426 100644 --- a/packages/shared-core/src/themes.ts +++ b/packages/shared-core/src/themes.ts @@ -17,7 +17,7 @@ export const getThemeClassNames = (theme: Theme): string => { // Ensures a theme value is a valid option export const ensureValidTheme = ( - theme: Theme, + theme?: Theme, fallback: Theme = Theme.DARKEST ): Theme => { if (!theme) {