1
0
mirror of https://github.com/home-assistant/frontend.git synced 2025-12-20 02:38:53 +00:00
Files
frontend/test/util/ha-pref-storage.test.ts
Paul Bottein 1ac3cf199f Save default panel at user and system level (#27899)
* Save default panel in user data

* Change logic for default panel

* Fix types

* Fix typings

* Fix user and local storage

* Use user data and system data

* Update url path and update dashboard settings

* Fix tests

* Wait for panels and user/system data to be loaded

* Update comment

* Update comment

* Set empty object instead of null

* Update comment

* Feedbacks

* Apply suggestions from code review

* format

---------

Co-authored-by: Petar Petrov <MindFreeze@users.noreply.github.com>
2025-11-19 15:03:20 +01:00

114 lines
3.4 KiB
TypeScript

import { describe, expect, afterEach, vi, test, beforeEach } from "vitest";
import type { HomeAssistant } from "../../src/types";
import { FallbackStorage } from "../test_helper/local-storage-fallback";
describe("ha-pref-storage", () => {
const mockHass = {
dockedSidebar: "auto",
selectedTheme: { theme: "default" },
unknownKey: "unknownValue",
};
beforeEach(() => {
window.localStorage = new FallbackStorage();
});
afterEach(() => {
vi.resetModules();
vi.resetAllMocks();
});
test("storeState", async () => {
const { storeState } = await import("../../src/util/ha-pref-storage");
window.localStorage.setItem = vi.fn();
storeState(mockHass as unknown as HomeAssistant);
expect(window.localStorage.setItem).toHaveBeenCalledTimes(7);
expect(window.localStorage.setItem).toHaveBeenCalledWith(
"dockedSidebar",
JSON.stringify("auto")
);
expect(window.localStorage.setItem).toHaveBeenCalledWith(
"selectedTheme",
JSON.stringify({ theme: "default" })
);
expect(window.localStorage.setItem).toHaveBeenCalledWith(
"selectedLanguage",
JSON.stringify(null)
);
expect(window.localStorage.setItem).not.toHaveBeenCalledWith(
"unknownKey",
JSON.stringify("unknownValue")
);
});
test("storeState fails", async () => {
const { storeState } = await import("../../src/util/ha-pref-storage");
window.localStorage.setItem = vi.fn((key) => {
if (key === "selectedTheme") {
throw new Error("Test error");
}
});
// eslint-disable-next-line no-global-assign
console = {
warn: vi.fn(),
error: vi.fn(),
} as unknown as Console;
storeState(mockHass as unknown as HomeAssistant);
expect(window.localStorage.setItem).toHaveBeenCalledTimes(2);
expect(window.localStorage.setItem).toHaveBeenCalledWith(
"dockedSidebar",
JSON.stringify("auto")
);
expect(window.localStorage.setItem).toHaveBeenCalledWith(
"selectedTheme",
JSON.stringify({ theme: "default" })
);
expect(window.localStorage.setItem).not.toHaveBeenCalledWith(
"selectedLanguage",
JSON.stringify(null)
);
// eslint-disable-next-line no-console
expect(console.warn).toHaveBeenCalledOnce();
// eslint-disable-next-line no-console
expect(console.warn).toHaveBeenCalledWith(
"Cannot store state; Are you in private mode or is your storage full?"
);
// eslint-disable-next-line no-console
expect(console.error).toHaveBeenCalledOnce();
});
test("getState", async () => {
const { getState } = await import("../../src/util/ha-pref-storage");
window.localStorage.setItem("selectedTheme", JSON.stringify("test"));
window.localStorage.setItem("dockedSidebar", JSON.stringify(true));
window.localStorage.setItem("selectedLanguage", JSON.stringify("german"));
// should not be in state
window.localStorage.setItem("testEntry", JSON.stringify("this is a test"));
const state = getState();
expect(state).toEqual({
dockedSidebar: "docked",
selectedTheme: { theme: "test" },
selectedLanguage: "german",
});
});
test("clearState", async () => {
const { clearState } = await import("../../src/util/ha-pref-storage");
window.localStorage.setItem("test", "test");
expect(window.localStorage.length).toEqual(1);
clearState();
expect(window.localStorage.length).toEqual(0);
});
});