Skip to content

Commit 41ff046

Browse files
committed
improve coverage
1 parent 44c8d04 commit 41ff046

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

lib/nthul/src/client/color-switch/color-switch.test.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { act, cleanup, fireEvent, render, renderHook, screen } from "@testing-library/react";
2-
import { ColorSwitch } from "./color-switch";
32
import { afterEach, describe, test } from "vitest";
43
import { useTheme } from "../../hooks/use-theme";
4+
import { ColorSwitch } from "./color-switch";
55

66
describe("color-switch", () => {
77
afterEach(cleanup);
@@ -19,4 +19,19 @@ describe("color-switch", () => {
1919
await act(() => fireEvent.click(element));
2020
expect(hook.result.current.colorSchemePreference).toBe("dark");
2121
});
22+
23+
test("color-scheme-toggle with skip system", async ({ expect }) => {
24+
const hook = renderHook(() => useTheme());
25+
act(() => {
26+
hook.result.current.setColorSchemePreference("system");
27+
});
28+
render(<ColorSwitch skipSystem />);
29+
const element = screen.getByTestId("color-switch");
30+
await act(() => fireEvent.click(element));
31+
expect(hook.result.current.colorSchemePreference).toBe("dark");
32+
await act(() => fireEvent.click(element));
33+
expect(hook.result.current.colorSchemePreference).toBe("light");
34+
await act(() => fireEvent.click(element));
35+
expect(hook.result.current.colorSchemePreference).toBe("dark");
36+
});
2237
});

lib/nthul/src/client/theme-switcher/theme-switcher.test.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { act, cleanup, fireEvent, render, renderHook } from "@testing-library/re
22
import { afterEach, beforeEach, describe, test } from "vitest";
33
import { useTheme } from "../../hooks/use-theme";
44
import { DEFAULT_ID } from "../../constants";
5+
import { ServerTarget } from "../../server";
56
import { ThemeSwitcher } from "./theme-switcher";
67

78
describe("theme-switcher", () => {
@@ -52,7 +53,7 @@ describe("theme-switcher", () => {
5253
test.todo("test media change event -- not supported by fireEvent");
5354
});
5455

55-
describe("test theme-switcher with props", () => {
56+
describe("test theme-switcher with props and server target", () => {
5657
afterEach(cleanup);
5758

5859
test("test dontSync", async ({ expect }) => {
@@ -72,4 +73,24 @@ describe("test theme-switcher with props", () => {
7273
test.todo("test invalid targetId", ({ expect }) => {
7374
expect(render(<ThemeSwitcher targetId="" />)).toThrow("id can not be an empty string");
7475
});
76+
77+
test("test with confined server target", ({ expect }) => {
78+
const THEME = "my-theme-with-server";
79+
const COLOR_SCHEME = "dark";
80+
const targetId = "my-custom-target";
81+
globalThis.cookies = {
82+
[targetId]: {
83+
value: `${THEME},${COLOR_SCHEME}`,
84+
},
85+
};
86+
render(<ServerTarget targetId={targetId} />);
87+
render(<ThemeSwitcher targetId={targetId} />);
88+
const { result } = renderHook(() => useTheme(targetId));
89+
const NEW_THEME = "new-theme";
90+
act(() => {
91+
result.current.setTheme(NEW_THEME);
92+
});
93+
expect(document.cookie).not.toContain(THEME);
94+
expect(document.cookie).toContain(NEW_THEME);
95+
});
7596
});

0 commit comments

Comments
 (0)