test: add error handling and edge case tests for hooks
useSettings Tests: - Add test for null settings state protection - Returns null immediately without calling APIs - Prevents null pointer errors in save flow - Add test for save mutation failure - Verifies error propagates to caller - Ensures restart flag remains untouched on failure - Validates no side effects when save fails useProviderActions Tests: - Add error propagation tests for CRUD operations - updateProvider: propagates errors to caller - addProvider: propagates errors to caller - deleteProvider: propagates errors to caller - Add switch mutation error handling tests - Claude switch: handles errors silently (no throw) - Codex switch: skips plugin sync when mutation fails - Verifies plugin sync APIs not called on failure - Add loading state verification - Test all mutations pending scenario (existing) - Test all mutations idle scenario (new) - Ensures isLoading flag accuracy useImportExport Edge Case Tests (new file): - Add test for user cancelling file dialog - File dialog returns null (user cancelled) - State remains unchanged (selectedFile: "", status: "idle") - No error toast shown - Add test for resetStatus behavior - Clears error message and status - Preserves selected file path for retry - Resets backupId to null All tests passing: 79/79 (10 new tests)
This commit is contained in:
76
tests/hooks/useImportExport.extra.test.tsx
Normal file
76
tests/hooks/useImportExport.extra.test.tsx
Normal file
@@ -0,0 +1,76 @@
|
||||
import { renderHook, act } from "@testing-library/react";
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { useImportExport } from "@/hooks/useImportExport";
|
||||
|
||||
const toastSuccessMock = vi.fn();
|
||||
const toastErrorMock = vi.fn();
|
||||
|
||||
vi.mock("sonner", () => ({
|
||||
toast: {
|
||||
success: (...args: unknown[]) => toastSuccessMock(...args),
|
||||
error: (...args: unknown[]) => toastErrorMock(...args),
|
||||
},
|
||||
}));
|
||||
|
||||
const openFileDialogMock = vi.fn();
|
||||
const importConfigMock = vi.fn();
|
||||
|
||||
vi.mock("@/lib/api", () => ({
|
||||
settingsApi: {
|
||||
openFileDialog: (...args: unknown[]) => openFileDialogMock(...args),
|
||||
importConfigFromFile: (...args: unknown[]) => importConfigMock(...args),
|
||||
saveFileDialog: vi.fn(),
|
||||
exportConfigToFile: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
describe("useImportExport Hook (edge cases)", () => {
|
||||
beforeEach(() => {
|
||||
openFileDialogMock.mockReset();
|
||||
importConfigMock.mockReset();
|
||||
toastSuccessMock.mockReset();
|
||||
toastErrorMock.mockReset();
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("keeps state unchanged when file dialog resolves to null", async () => {
|
||||
openFileDialogMock.mockResolvedValue(null);
|
||||
const { result } = renderHook(() => useImportExport());
|
||||
|
||||
await act(async () => {
|
||||
await result.current.selectImportFile();
|
||||
});
|
||||
|
||||
expect(result.current.selectedFile).toBe("");
|
||||
expect(result.current.status).toBe("idle");
|
||||
expect(toastErrorMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("resetStatus clears errors but preserves selected file", async () => {
|
||||
openFileDialogMock.mockResolvedValue("/config.json");
|
||||
importConfigMock.mockResolvedValue({ success: false, message: "broken" });
|
||||
const { result } = renderHook(() => useImportExport());
|
||||
|
||||
await act(async () => {
|
||||
await result.current.selectImportFile();
|
||||
});
|
||||
|
||||
await act(async () => {
|
||||
await result.current.importConfig();
|
||||
});
|
||||
|
||||
act(() => {
|
||||
result.current.resetStatus();
|
||||
});
|
||||
|
||||
expect(result.current.selectedFile).toBe("/config.json");
|
||||
expect(result.current.status).toBe("idle");
|
||||
expect(result.current.errorMessage).toBeNull();
|
||||
expect(result.current.backupId).toBeNull();
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user