Fix tests

This commit is contained in:
Wendelin 2025-02-13 11:43:02 +01:00
parent cd1ca72e45
commit c6617718b7
No known key found for this signature in database

View File

@ -6,6 +6,7 @@ describe("ha-pref-storage", () => {
const mockHass = { const mockHass = {
dockedSidebar: "auto", dockedSidebar: "auto",
selectedTheme: { theme: "default" }, selectedTheme: { theme: "default" },
vibrate: "false",
unknownKey: "unknownValue", unknownKey: "unknownValue",
}; };
@ -24,15 +25,11 @@ describe("ha-pref-storage", () => {
window.localStorage.setItem = vi.fn(); window.localStorage.setItem = vi.fn();
storeState(mockHass as unknown as HomeAssistant); storeState(mockHass as unknown as HomeAssistant);
expect(window.localStorage.setItem).toHaveBeenCalledTimes(8); expect(window.localStorage.setItem).toHaveBeenCalledTimes(7);
expect(window.localStorage.setItem).toHaveBeenCalledWith( expect(window.localStorage.setItem).toHaveBeenCalledWith(
"dockedSidebar", "dockedSidebar",
JSON.stringify("auto") JSON.stringify("auto")
); );
expect(window.localStorage.setItem).toHaveBeenCalledWith(
"selectedTheme",
JSON.stringify({ theme: "default" })
);
expect(window.localStorage.setItem).toHaveBeenCalledWith( expect(window.localStorage.setItem).toHaveBeenCalledWith(
"selectedLanguage", "selectedLanguage",
JSON.stringify(null) JSON.stringify(null)
@ -41,13 +38,19 @@ describe("ha-pref-storage", () => {
"unknownKey", "unknownKey",
JSON.stringify("unknownValue") JSON.stringify("unknownValue")
); );
// browserThemeEnabled is not set in mockHass, so selectedTheme should not be stored
expect(window.localStorage.setItem).not.toHaveBeenCalledWith(
"selectedTheme",
JSON.stringify({ theme: "default" })
);
}); });
test("storeState fails", async () => { test("storeState fails", async () => {
const { storeState } = await import("../../src/util/ha-pref-storage"); const { storeState } = await import("../../src/util/ha-pref-storage");
window.localStorage.setItem = vi.fn((key) => { window.localStorage.setItem = vi.fn((key) => {
if (key === "selectedTheme") { if (key === "selectedLanguage") {
throw new Error("Test error"); throw new Error("Test error");
} }
}); });
@ -65,13 +68,13 @@ describe("ha-pref-storage", () => {
JSON.stringify("auto") JSON.stringify("auto")
); );
expect(window.localStorage.setItem).toHaveBeenCalledWith( expect(window.localStorage.setItem).toHaveBeenCalledWith(
"selectedTheme",
JSON.stringify({ theme: "default" })
);
expect(window.localStorage.setItem).not.toHaveBeenCalledWith(
"selectedLanguage", "selectedLanguage",
JSON.stringify(null) JSON.stringify(null)
); );
expect(window.localStorage.setItem).not.toHaveBeenCalledWith(
"vibrate",
JSON.stringify("false")
);
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
expect(console.warn).toHaveBeenCalledOnce(); expect(console.warn).toHaveBeenCalledOnce();
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
@ -87,7 +90,7 @@ describe("ha-pref-storage", () => {
window.localStorage.setItem("selectedTheme", JSON.stringify("test")); window.localStorage.setItem("selectedTheme", JSON.stringify("test"));
window.localStorage.setItem("dockedSidebar", JSON.stringify(true)); window.localStorage.setItem("dockedSidebar", JSON.stringify(true));
window.localStorage.setItem("selectedLanguage", JSON.stringify("german")); window.localStorage.setItem("selectedLanguage", JSON.stringify("de"));
// should not be in state // should not be in state
window.localStorage.setItem("testEntry", JSON.stringify("this is a test")); window.localStorage.setItem("testEntry", JSON.stringify("this is a test"));
@ -96,7 +99,21 @@ describe("ha-pref-storage", () => {
expect(state).toEqual({ expect(state).toEqual({
dockedSidebar: "docked", dockedSidebar: "docked",
selectedTheme: { theme: "test" }, selectedTheme: { theme: "test" },
selectedLanguage: "german", browserThemeEnabled: true,
selectedLanguage: "de",
});
});
test("getState without theme", async () => {
const { getState } = await import("../../src/util/ha-pref-storage");
window.localStorage.setItem("dockedSidebar", JSON.stringify(true));
window.localStorage.setItem("selectedLanguage", JSON.stringify("de"));
const state = getState();
expect(state).toEqual({
dockedSidebar: "docked",
selectedLanguage: "de",
}); });
}); });