Skip to content

fix: resolve TypeScript errors in test files #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 55 additions & 15 deletions components/theme-toggle.test.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import { describe, it, expect, vi } from "vitest";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import { ThemeToggle } from "./theme-toggle";

// Mock next-themes
vi.mock("next-themes", () => ({
useTheme: () => ({
theme: "light",
setTheme: vi.fn(),
}),
}));
// Mock next-themes at the top level
vi.mock("next-themes", () => {
const mockSetTheme = vi.fn();
return {
useTheme: vi.fn(() => ({
theme: "light",
setTheme: mockSetTheme,
resolvedTheme: "light",
systemTheme: "light",
themes: ["light", "dark"],
forcedTheme: undefined,
})),
};
});

describe("ThemeToggle", () => {
let mockSetTheme: ReturnType<typeof vi.fn>;

beforeEach(async () => {
vi.clearAllMocks();
// Get the mocked module
const nextThemes = await import("next-themes");
const useThemeMock = vi.mocked(nextThemes.useTheme);

// Create fresh mock for each test
mockSetTheme = vi.fn();
useThemeMock.mockReturnValue({
theme: "light",
setTheme: mockSetTheme,
resolvedTheme: "light",
systemTheme: "light",
themes: ["light", "dark"],
forcedTheme: undefined,
});
});

it("should render the theme toggle button", () => {
render(<ThemeToggle />);
const button = screen.getByRole("button", { name: /toggle theme/i });
Expand All @@ -24,19 +51,32 @@ describe("ThemeToggle", () => {
expect(sunIcon).toHaveClass("scale-100");
});

it("should call setTheme when clicked", () => {
const mockSetTheme = vi.fn();
vi.mocked(vi.importActual("next-themes")).useTheme = () => ({
theme: "light",
it("should call setTheme when clicked", async () => {
render(<ThemeToggle />);
const button = screen.getByRole("button");
fireEvent.click(button);

expect(mockSetTheme).toHaveBeenCalledWith("dark");
});

it("should toggle to light theme when in dark mode", async () => {
// Update mock for dark mode
const nextThemes = await import("next-themes");
const useThemeMock = vi.mocked(nextThemes.useTheme);

useThemeMock.mockReturnValue({
theme: "dark",
setTheme: mockSetTheme,
resolvedTheme: "dark",
systemTheme: "light",
themes: ["light", "dark"],
forcedTheme: undefined,
});

render(<ThemeToggle />);
const button = screen.getByRole("button");
fireEvent.click(button);

// Note: In a real test, we'd verify mockSetTheme was called
// but this is just an example to show the setup works
expect(button).toBeInTheDocument();
expect(mockSetTheme).toHaveBeenCalledWith("light");
});
});
5 changes: 3 additions & 2 deletions lib/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ describe("handleError", () => {
});

it("should log errors with context", async () => {
const loggerModule = await vi.importMock("./logger");
const loggerModule =
await vi.importMock<typeof import("./logger")>("./logger");
const error = new BadRequestError("Test error");

handleError(error, "TestContext");
Expand All @@ -156,7 +157,7 @@ describe("handleError", () => {

describe("withErrorHandling", () => {
it("should wrap async handler and catch errors", async () => {
const handler = async () => {
const handler = async (): Promise<Response> => {
throw new BadRequestError("Test error");
};

Expand Down
6 changes: 4 additions & 2 deletions lib/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ describe("Logger", () => {
describe("log levels in production", () => {
beforeEach(async () => {
// Change mock to return production
const env = await vi.importMock("./environment");
const env =
await vi.importMock<typeof import("./environment")>("./environment");
vi.mocked(env.getCurrentEnvironment).mockReturnValue("production");
});

afterEach(async () => {
// Reset to development
const env = await vi.importMock("./environment");
const env =
await vi.importMock<typeof import("./environment")>("./environment");
vi.mocked(env.getCurrentEnvironment).mockReturnValue("development");
});

Expand Down