|
| 1 | +// noinspection JSUnusedGlobalSymbols |
| 2 | + |
| 3 | +/** |
| 4 | + * @file Tests for the ColorMode module. |
| 5 | + * @description These are unit tests for the ColorMode components and hooks. They verify |
| 6 | + * the rendering of ColorModeProvider, functionality of useColorMode hook, ColorModeButton, |
| 7 | + * and ColorModeWrapper in a mocked environment. |
| 8 | + * @module ColorModeTests |
| 9 | + */ |
| 10 | + |
| 11 | +// region Imports |
| 12 | +import { |
| 13 | + ColorModeButton, |
| 14 | + ColorModeProvider, |
| 15 | + ColorModeWrapper, |
| 16 | + type UseColorModeReturn, |
| 17 | + useColorMode, |
| 18 | +} from "@/components/ui/color-mode" |
| 19 | +import { act, render, renderHook, screen } from "@testing-library/react" |
| 20 | +import userEvent from "@testing-library/user-event" |
| 21 | +import { useTheme } from "next-themes" |
| 22 | +import type React from "react" |
| 23 | +import { type RefObject, createRef } from "react" |
| 24 | +import type { Dispatch, SetStateAction } from "react" |
| 25 | +import { describe, expect, it, vi } from "vitest" |
| 26 | + |
| 27 | +// endregion |
| 28 | + |
| 29 | +// region Type Aliases |
| 30 | +interface UseThemeReturn { |
| 31 | + theme: string |
| 32 | + setTheme: Dispatch<SetStateAction<string>> |
| 33 | + themes: string[] |
| 34 | +} |
| 35 | +// endregion |
| 36 | + |
| 37 | +// region Mocks |
| 38 | +vi.mock("next-themes", () => ({ |
| 39 | + ThemeProvider: ({ children }: { children: React.ReactNode }): React.ReactElement => <div>{children}</div>, |
| 40 | + useTheme: vi.fn(), |
| 41 | +})) |
| 42 | + |
| 43 | +// endregion |
| 44 | + |
| 45 | +// region Tests |
| 46 | +describe("ColorMode", (): void => { |
| 47 | + /** |
| 48 | + * Test case: Renders ColorModeProvider with children. |
| 49 | + * It verifies that the provider renders its children correctly. |
| 50 | + */ |
| 51 | + it("renders ColorModeProvider with children", (): void => { |
| 52 | + render( |
| 53 | + <ColorModeProvider> |
| 54 | + <span data-testid="child">Test Child</span> |
| 55 | + </ColorModeProvider>, |
| 56 | + ) |
| 57 | + const child: HTMLElement = screen.getByTestId("child") |
| 58 | + expect(child).toBeInTheDocument() |
| 59 | + expect(child).toHaveTextContent("Test Child") |
| 60 | + }) |
| 61 | + |
| 62 | + /** |
| 63 | + * Test case: useColorMode hook returns correct values and toggles theme. |
| 64 | + * It verifies that the hook returns the current theme and toggles between light and dark. |
| 65 | + */ |
| 66 | + it("useColorMode returns correct values and toggles theme", (): void => { |
| 67 | + let currentTheme = "light" |
| 68 | + const setTheme = vi.fn((newTheme: string | ((prev: string) => string)): void => { |
| 69 | + currentTheme = typeof newTheme === "string" ? newTheme : newTheme(currentTheme) |
| 70 | + }) |
| 71 | + vi.mocked(useTheme).mockImplementation( |
| 72 | + (): UseThemeReturn => ({ theme: currentTheme, setTheme, themes: ["light", "dark", "system"] }), |
| 73 | + ) |
| 74 | + |
| 75 | + const { result, rerender } = renderHook((): UseColorModeReturn => useColorMode(), { |
| 76 | + wrapper: ({ children }: { children: React.ReactNode }): React.ReactElement => ( |
| 77 | + <ColorModeProvider>{children}</ColorModeProvider> |
| 78 | + ), |
| 79 | + }) |
| 80 | + expect(result.current.colorMode).toBe("light") |
| 81 | + expect(typeof result.current.setColorMode).toBe("function") |
| 82 | + expect(typeof result.current.toggleColorMode).toBe("function") |
| 83 | + |
| 84 | + act((): void => { |
| 85 | + result.current.toggleColorMode() |
| 86 | + }) |
| 87 | + expect(setTheme).toHaveBeenCalledWith("dark") |
| 88 | + rerender() |
| 89 | + expect(result.current.colorMode).toBe("dark") |
| 90 | + |
| 91 | + act((): void => { |
| 92 | + result.current.toggleColorMode() |
| 93 | + }) |
| 94 | + expect(setTheme).toHaveBeenCalledWith("light") |
| 95 | + rerender() |
| 96 | + expect(result.current.colorMode).toBe("light") |
| 97 | + }) |
| 98 | + |
| 99 | + /** |
| 100 | + * Test case: Renders ColorModeButton with default aria-label and icon. |
| 101 | + * It verifies that the button renders with the correct aria-label and icon based on the current theme. |
| 102 | + */ |
| 103 | + it("renders ColorModeButton with default aria-label and icon", (): void => { |
| 104 | + vi.mocked(useTheme).mockReturnValue({ theme: "light", setTheme: vi.fn(), themes: ["light", "dark", "system"] }) |
| 105 | + |
| 106 | + render( |
| 107 | + <ColorModeProvider> |
| 108 | + <ColorModeButton /> |
| 109 | + </ColorModeProvider>, |
| 110 | + ) |
| 111 | + const button: HTMLElement = screen.getByRole("button", { name: /switch to dark mode/i }) |
| 112 | + expect(button).toBeInTheDocument() |
| 113 | + expect(button).toHaveAttribute("aria-label", "Switch to dark mode") |
| 114 | + expect(button.querySelector("svg")).toBeInTheDocument() |
| 115 | + }) |
| 116 | + |
| 117 | + /** |
| 118 | + * Test case: Renders ColorModeButton with custom aria-label. |
| 119 | + * It verifies that the button uses a custom aria-label when provided. |
| 120 | + */ |
| 121 | + it("renders ColorModeButton with custom aria-label", (): void => { |
| 122 | + vi.mocked(useTheme).mockReturnValue({ theme: "light", setTheme: vi.fn(), themes: ["light", "dark", "system"] }) |
| 123 | + |
| 124 | + render( |
| 125 | + <ColorModeProvider> |
| 126 | + <ColorModeButton aria-label="Toggle Theme" /> |
| 127 | + </ColorModeProvider>, |
| 128 | + ) |
| 129 | + const button: HTMLElement = screen.getByRole("button", { name: /toggle theme/i }) |
| 130 | + expect(button).toBeInTheDocument() |
| 131 | + expect(button).toHaveAttribute("aria-label", "Toggle Theme") |
| 132 | + }) |
| 133 | + |
| 134 | + /** |
| 135 | + * Test case: ColorModeButton toggles theme on click. |
| 136 | + * It verifies that clicking the button toggles the theme using useColorMode. |
| 137 | + */ |
| 138 | + it("ColorModeButton toggles theme on click", async (): Promise<void> => { |
| 139 | + let currentTheme = "light" |
| 140 | + const setTheme = vi.fn((newTheme: string | ((prev: string) => string)) => { |
| 141 | + currentTheme = typeof newTheme === "string" ? newTheme : newTheme(currentTheme) |
| 142 | + }) |
| 143 | + vi.mocked(useTheme).mockImplementation( |
| 144 | + (): UseThemeReturn => ({ theme: currentTheme, setTheme, themes: ["light", "dark", "system"] }), |
| 145 | + ) |
| 146 | + |
| 147 | + render( |
| 148 | + <ColorModeProvider> |
| 149 | + <ColorModeButton /> |
| 150 | + </ColorModeProvider>, |
| 151 | + ) |
| 152 | + const button: HTMLElement = screen.getByRole("button", { name: /switch to dark mode/i }) |
| 153 | + await act(async (): Promise<void> => { |
| 154 | + await userEvent.click(button) |
| 155 | + }) |
| 156 | + expect(setTheme).toHaveBeenCalledWith("dark") |
| 157 | + |
| 158 | + render( |
| 159 | + <ColorModeProvider> |
| 160 | + <ColorModeButton /> |
| 161 | + </ColorModeProvider>, |
| 162 | + ) |
| 163 | + const updatedButton: HTMLElement = screen.getByRole("button", { name: /switch to light mode/i }) |
| 164 | + await act(async (): Promise<void> => { |
| 165 | + await userEvent.click(updatedButton) |
| 166 | + }) |
| 167 | + expect(setTheme).toHaveBeenCalledWith("light") |
| 168 | + }) |
| 169 | + |
| 170 | + /** |
| 171 | + * Test case: Renders ColorModeWrapper with correct mode and props. |
| 172 | + * It verifies that the wrapper renders with the specified mode and additional props. |
| 173 | + */ |
| 174 | + it("renders ColorModeWrapper with correct mode and props", (): void => { |
| 175 | + render(<ColorModeWrapper mode="dark" data-test="wrapper-prop" />) |
| 176 | + const wrapper: HTMLElement = screen.getByTestId("span") |
| 177 | + expect(wrapper).toBeInTheDocument() |
| 178 | + expect(wrapper).toHaveClass("chakra-theme", "dark") |
| 179 | + expect(wrapper).toHaveAttribute("data-test", "wrapper-prop") |
| 180 | + }) |
| 181 | + |
| 182 | + /** |
| 183 | + * Test case: Forwards ref to ColorModeButton. |
| 184 | + * It verifies that the ref is forwarded to the underlying button element. |
| 185 | + */ |
| 186 | + it("forwards ref to ColorModeButton", (): void => { |
| 187 | + vi.mocked(useTheme).mockReturnValue({ theme: "light", setTheme: vi.fn(), themes: ["light", "dark", "system"] }) |
| 188 | + |
| 189 | + const ref: RefObject<HTMLButtonElement> = createRef<HTMLButtonElement>() |
| 190 | + render( |
| 191 | + <ColorModeProvider> |
| 192 | + <ColorModeButton ref={ref} /> |
| 193 | + </ColorModeProvider>, |
| 194 | + ) |
| 195 | + expect(ref.current).toBeInstanceOf(HTMLButtonElement) |
| 196 | + }) |
| 197 | + |
| 198 | + /** |
| 199 | + * Test case: Forwards ref to ColorModeWrapper. |
| 200 | + * It verifies that the ref is forwarded to the underlying span element. |
| 201 | + */ |
| 202 | + it("forwards ref to ColorModeWrapper", (): void => { |
| 203 | + const ref: RefObject<HTMLSpanElement> = createRef<HTMLSpanElement>() |
| 204 | + render(<ColorModeWrapper mode="light" ref={ref} />) |
| 205 | + expect(ref.current).toBeInstanceOf(HTMLSpanElement) |
| 206 | + }) |
| 207 | +}) |
| 208 | +// endregion |
0 commit comments