nandi/oripublic Fork 0
5d476ff5f3dc3b7db3afaccaedc7d9661981449e
Commits
Clone
git clone https://git.rickub.com/nandi/ori.git
git clone ssh://git@rickub.com/nandi/ori.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

forked from bots-garden/ori

theme.test.ts · 54 lines · 1.4 KBTypeScript Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { beforeEach, describe, expect, it } from "vitest";
import {
	DEFAULT_THEME,
	THEME_KEY,
	isTheme,
	resetTheme,
	setTheme,
	toggleTheme,
	useTheme,
} from "./theme";

describe("theme store", () => {
	beforeEach(() => {
		localStorage.clear();
		resetTheme();
	});

	it("starts light and stamps the document", () => {
		expect(useTheme.getState().theme).toBe(DEFAULT_THEME);
		expect(document.documentElement.dataset.theme).toBe("light");
	});

	it("setTheme applies, stamps and remembers the theme", () => {
		setTheme("dark");
		expect(useTheme.getState().theme).toBe("dark");
		expect(document.documentElement.dataset.theme).toBe("dark");
		expect(localStorage.getItem(THEME_KEY)).toBe("dark");
	});

	it("toggleTheme alternates between light and dark", () => {
		toggleTheme();
		expect(useTheme.getState().theme).toBe("dark");
		toggleTheme();
		expect(useTheme.getState().theme).toBe("light");
		expect(document.documentElement.dataset.theme).toBe("light");
	});

	it("resetTheme goes back to light", () => {
		setTheme("dark");
		resetTheme();
		expect(useTheme.getState().theme).toBe("light");
		expect(localStorage.getItem(THEME_KEY)).toBe("light");
	});
});

describe("isTheme", () => {
	it("accepts the two themes and rejects anything else", () => {
		expect(isTheme("light")).toBe(true);
		expect(isTheme("dark")).toBe(true);
		expect(isTheme("system")).toBe(false);
		expect(isTheme(null)).toBe(false);
		expect(isTheme(42)).toBe(false);
	});
});