-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,043 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { beforeEach, describe, expect, it, vi } from "vitest" | ||
|
||
import { useTheme } from "@/components/theme/theme-provider" | ||
import { act, renderHook } from "@/test/test-utils" | ||
|
||
describe("useTheme", () => { | ||
beforeEach(() => { | ||
// 清理 localStorage | ||
localStorage.clear() | ||
// 清理 document root 的主题类 | ||
document.documentElement.classList.remove("light", "dark") | ||
// 重置 matchMedia | ||
window.matchMedia = vi.fn().mockImplementation((query) => ({ | ||
matches: false, | ||
media: query, | ||
onchange: null, | ||
addListener: vi.fn(), | ||
removeListener: vi.fn(), | ||
addEventListener: vi.fn(), | ||
removeEventListener: vi.fn(), | ||
dispatchEvent: vi.fn(), | ||
})) | ||
}) | ||
|
||
it("should start with system theme and apply light theme when system prefers light", async () => { | ||
const { result } = renderHook(() => useTheme()) | ||
|
||
expect(result.current.theme).toBe("system") | ||
// 等待 useEffect 执行完成 | ||
await new Promise((resolve) => setTimeout(resolve, 0)) | ||
expect(document.documentElement.classList.contains("light")).toBe(true) | ||
}) | ||
|
||
it("should apply dark theme when system prefers dark", async () => { | ||
// Mock system dark theme preference | ||
window.matchMedia = vi.fn().mockImplementation((query) => ({ | ||
matches: query === "(prefers-color-scheme: dark)", | ||
media: query, | ||
onchange: null, | ||
addListener: vi.fn(), | ||
removeListener: vi.fn(), | ||
addEventListener: vi.fn(), | ||
removeEventListener: vi.fn(), | ||
dispatchEvent: vi.fn(), | ||
})) | ||
|
||
const { result } = renderHook(() => useTheme()) | ||
|
||
expect(result.current.theme).toBe("system") | ||
// 等待 useEffect 执行完成 | ||
await new Promise((resolve) => setTimeout(resolve, 0)) | ||
expect(document.documentElement.classList.contains("dark")).toBe(true) | ||
}) | ||
|
||
it("should load theme from localStorage if available", async () => { | ||
localStorage.setItem("vite-ui-theme", "dark") | ||
|
||
const { result } = renderHook(() => useTheme()) | ||
|
||
expect(result.current.theme).toBe("dark") | ||
// 等待 useEffect 执行完成 | ||
await new Promise((resolve) => setTimeout(resolve, 0)) | ||
expect(document.documentElement.classList.contains("dark")).toBe(true) | ||
}) | ||
|
||
it("should change theme to dark", async () => { | ||
const { result } = renderHook(() => useTheme()) | ||
|
||
act(() => { | ||
result.current.setTheme("dark") | ||
}) | ||
|
||
// 等待 useEffect 执行完成 | ||
await new Promise((resolve) => setTimeout(resolve, 0)) | ||
expect(result.current.theme).toBe("dark") | ||
expect(localStorage.getItem("vite-ui-theme")).toBe("dark") | ||
expect(document.documentElement.classList.contains("dark")).toBe(true) | ||
}) | ||
|
||
it("should change theme to light", () => { | ||
const { result } = renderHook(() => useTheme()) | ||
|
||
act(() => { | ||
result.current.setTheme("light") | ||
}) | ||
|
||
expect(result.current.theme).toBe("light") | ||
expect(localStorage.getItem("vite-ui-theme")).toBe("light") | ||
expect(document.documentElement.classList.contains("light")).toBe(true) | ||
expect(document.documentElement.classList.contains("dark")).toBe(false) | ||
}) | ||
|
||
it("should change theme to system", async () => { | ||
localStorage.setItem("vite-ui-theme", "dark") | ||
|
||
// 模拟系统主题为 light | ||
window.matchMedia = vi.fn().mockImplementation((query) => ({ | ||
matches: query === "(prefers-color-scheme: light)", | ||
media: query, | ||
onchange: null, | ||
addListener: vi.fn(), | ||
removeListener: vi.fn(), | ||
addEventListener: vi.fn(), | ||
removeEventListener: vi.fn(), | ||
dispatchEvent: vi.fn(), | ||
})) | ||
|
||
const { result } = renderHook(() => useTheme()) | ||
|
||
act(() => { | ||
result.current.setTheme("system") | ||
}) | ||
|
||
// 等待 useEffect 执行完成 | ||
await new Promise((resolve) => setTimeout(resolve, 0)) | ||
|
||
expect(result.current.theme).toBe("system") | ||
expect(localStorage.getItem("vite-ui-theme")).toBe("system") | ||
expect(document.documentElement.classList.contains("light")).toBe(true) | ||
}) | ||
|
||
it("should throw error when used outside ThemeProvider", () => { | ||
const { result } = renderHook(() => useTheme(), { | ||
wrapper: ({ children }) => children, | ||
}) | ||
|
||
expect(() => result.current).toThrow("useTheme must be used within a ThemeProvider") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { describe, expect, it } from "vitest" | ||
|
||
import { cn } from "./utils" | ||
|
||
describe("cn utility", () => { | ||
it("merges class names correctly", () => { | ||
const result = cn("base-class", "additional", { conditional: true }) | ||
expect(result).toBe("base-class additional conditional") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { describe, expect, it } from "vitest" | ||
|
||
import { render, screen } from "@/test/test-utils" | ||
|
||
import { Component as HomePage } from "./index" | ||
|
||
describe("HomePage", () => { | ||
it("renders main heading", () => { | ||
render(<HomePage />) | ||
expect(screen.getByText("Shadcn UI Boilerplate")).toBeInTheDocument() | ||
}) | ||
|
||
it("contains link to documentation", () => { | ||
render(<HomePage />) | ||
const link = screen.getByText("Getting Started guide") | ||
expect(link).toHaveAttribute( | ||
"href", | ||
"https://shadcnui-boilerplate.pages.dev/guide/what-is-shadcn-ui-boilerplate", | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import "@testing-library/jest-dom" | ||
|
||
import { cleanup } from "@testing-library/react" | ||
import { afterEach } from "vitest" | ||
|
||
// 每个测试后清理 | ||
afterEach(() => { | ||
cleanup() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query" | ||
import { render as rtlRender, renderHook as rtlRenderHook } from "@testing-library/react" | ||
import { I18nextProvider } from "react-i18next" | ||
import { BrowserRouter } from "react-router-dom" | ||
|
||
import type { Theme } from "@/components/theme/theme-provider" | ||
import { ThemeProvider } from "@/components/theme/theme-provider" | ||
import { i18n } from "@/i18n" | ||
|
||
type RenderHookOptions = { | ||
initialProps?: { | ||
defaultTheme?: Theme | ||
} | ||
wrapper?: React.ComponentType<{ children: React.ReactNode }> | ||
} | ||
|
||
export const renderHook = <Result,>( | ||
hook: () => Result, | ||
options: RenderHookOptions = {}, | ||
) => { | ||
return rtlRenderHook(hook, { | ||
wrapper: ({ children }) => | ||
options.wrapper ? | ||
( | ||
<options.wrapper>{children}</options.wrapper> | ||
) : | ||
( | ||
<ThemeProvider defaultTheme={options.initialProps?.defaultTheme}> | ||
{children} | ||
</ThemeProvider> | ||
), | ||
}) | ||
} | ||
|
||
function render(ui: React.ReactElement, { route = "/" } = {}) { | ||
window.history.pushState({}, "Test page", route) | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
retry: false, | ||
}, | ||
}, | ||
}) | ||
|
||
return rtlRender( | ||
<QueryClientProvider client={queryClient}> | ||
<I18nextProvider i18n={i18n}> | ||
<BrowserRouter> | ||
{ui} | ||
</BrowserRouter> | ||
</I18nextProvider> | ||
</QueryClientProvider>, | ||
) | ||
} | ||
|
||
export * from "@testing-library/react" | ||
export { render } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,31 @@ | ||
import { fileURLToPath } from "node:url" | ||
|
||
import react from "@vitejs/plugin-react-swc" | ||
import { mergeConfig } from "vite" | ||
import { configDefaults, defineConfig } from "vitest/config" | ||
|
||
import viteConfig from "./vite.config" | ||
|
||
export default defineConfig((configEnv) => mergeConfig( | ||
viteConfig(configEnv), | ||
defineConfig({ | ||
test: { | ||
environment: "jsdom", | ||
exclude: [...configDefaults.exclude, "e2e/*"], | ||
root: fileURLToPath(new URL("./", import.meta.url)), | ||
setupFiles: ["./__test__/setup.ts"], | ||
}, | ||
}), | ||
)) | ||
export default defineConfig((configEnv) => | ||
mergeConfig( | ||
viteConfig(configEnv), | ||
defineConfig({ | ||
plugins: [react()], | ||
test: { | ||
environment: "jsdom", | ||
globals: true, | ||
setupFiles: "./src/test/setup.ts", | ||
coverage: { | ||
provider: "v8", | ||
reporter: ["text", "json", "html"], | ||
exclude: [ | ||
...(configDefaults.exclude ?? []), | ||
"node_modules/", | ||
"src/test/", | ||
"**/*.d.ts", | ||
"**/*.config.*", | ||
"**/.*", | ||
], | ||
}, | ||
}, | ||
}), | ||
), | ||
) |