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();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
@@ -223,6 +223,22 @@ describe("useProviderActions", () => {
|
|||||||
expect(toastErrorMock.mock.calls[0]?.[0]).toBe("Sync failed");
|
expect(toastErrorMock.mock.calls[0]?.[0]).toBe("Sync failed");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("propagates updateProvider errors", async () => {
|
||||||
|
updateProviderMutateAsync.mockRejectedValueOnce(new Error("update failed"));
|
||||||
|
const { wrapper } = createWrapper();
|
||||||
|
const provider = createProvider();
|
||||||
|
|
||||||
|
const { result } = renderHook(() => useProviderActions("claude"), {
|
||||||
|
wrapper,
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
act(async () => {
|
||||||
|
await result.current.updateProvider(provider);
|
||||||
|
}),
|
||||||
|
).rejects.toThrow("update failed");
|
||||||
|
});
|
||||||
|
|
||||||
it("should use default error message when plugin sync fails without error message", async () => {
|
it("should use default error message when plugin sync fails without error message", async () => {
|
||||||
switchProviderMutateAsync.mockResolvedValueOnce(undefined);
|
switchProviderMutateAsync.mockResolvedValueOnce(undefined);
|
||||||
settingsApiGetMock.mockResolvedValueOnce({
|
settingsApiGetMock.mockResolvedValueOnce({
|
||||||
@@ -244,6 +260,20 @@ describe("useProviderActions", () => {
|
|||||||
expect(toastErrorMock.mock.calls[0]?.[0]).toBe("同步 Claude 插件失败");
|
expect(toastErrorMock.mock.calls[0]?.[0]).toBe("同步 Claude 插件失败");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("handles mutation errors when plugin sync is skipped", async () => {
|
||||||
|
switchProviderMutateAsync.mockRejectedValueOnce(new Error("switch failed"));
|
||||||
|
const { wrapper } = createWrapper();
|
||||||
|
const provider = createProvider();
|
||||||
|
|
||||||
|
const { result } = renderHook(() => useProviderActions("codex"), {
|
||||||
|
wrapper,
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(result.current.switchProvider(provider)).resolves.toBeUndefined();
|
||||||
|
expect(settingsApiGetMock).not.toHaveBeenCalled();
|
||||||
|
expect(settingsApiApplyMock).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
it("should call delete mutation when calling deleteProvider", async () => {
|
it("should call delete mutation when calling deleteProvider", async () => {
|
||||||
deleteProviderMutateAsync.mockResolvedValueOnce(undefined);
|
deleteProviderMutateAsync.mockResolvedValueOnce(undefined);
|
||||||
const { wrapper } = createWrapper();
|
const { wrapper } = createWrapper();
|
||||||
@@ -349,6 +379,55 @@ describe("useProviderActions", () => {
|
|||||||
expect(toastErrorMock.mock.calls[0]?.[0]).toBe("用量查询配置保存失败");
|
expect(toastErrorMock.mock.calls[0]?.[0]).toBe("用量查询配置保存失败");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it("propagates addProvider errors to caller", async () => {
|
||||||
|
addProviderMutateAsync.mockRejectedValueOnce(new Error("add failed"));
|
||||||
|
const { wrapper } = createWrapper();
|
||||||
|
|
||||||
|
const { result } = renderHook(() => useProviderActions("claude"), {
|
||||||
|
wrapper,
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
act(async () => {
|
||||||
|
await result.current.addProvider({
|
||||||
|
name: "temp",
|
||||||
|
settingsConfig: {},
|
||||||
|
} as Omit<Provider, "id">);
|
||||||
|
}),
|
||||||
|
).rejects.toThrow("add failed");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("propagates deleteProvider errors to caller", async () => {
|
||||||
|
deleteProviderMutateAsync.mockRejectedValueOnce(new Error("delete failed"));
|
||||||
|
const { wrapper } = createWrapper();
|
||||||
|
|
||||||
|
const { result } = renderHook(() => useProviderActions("claude"), {
|
||||||
|
wrapper,
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
act(async () => {
|
||||||
|
await result.current.deleteProvider("provider-2");
|
||||||
|
}),
|
||||||
|
).rejects.toThrow("delete failed");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles switch mutation errors silently", async () => {
|
||||||
|
switchProviderMutateAsync.mockRejectedValueOnce(new Error("switch failed"));
|
||||||
|
const { wrapper } = createWrapper();
|
||||||
|
const provider = createProvider();
|
||||||
|
|
||||||
|
const { result } = renderHook(() => useProviderActions("claude"), {
|
||||||
|
wrapper,
|
||||||
|
});
|
||||||
|
|
||||||
|
await result.current.switchProvider(provider);
|
||||||
|
|
||||||
|
expect(settingsApiGetMock).not.toHaveBeenCalled();
|
||||||
|
expect(settingsApiApplyMock).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
it("should track pending state of all mutations in isLoading", () => {
|
it("should track pending state of all mutations in isLoading", () => {
|
||||||
addProviderMutation.isPending = true;
|
addProviderMutation.isPending = true;
|
||||||
const { wrapper } = createWrapper();
|
const { wrapper } = createWrapper();
|
||||||
@@ -360,3 +439,16 @@ describe("useProviderActions", () => {
|
|||||||
expect(result.current.isLoading).toBe(true);
|
expect(result.current.isLoading).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
it("clears loading flag when all mutations idle", () => {
|
||||||
|
addProviderMutation.isPending = false;
|
||||||
|
updateProviderMutation.isPending = false;
|
||||||
|
deleteProviderMutation.isPending = false;
|
||||||
|
switchProviderMutation.isPending = false;
|
||||||
|
|
||||||
|
const { wrapper } = createWrapper();
|
||||||
|
const { result } = renderHook(() => useProviderActions("claude"), {
|
||||||
|
wrapper,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.current.isLoading).toBe(false);
|
||||||
|
});
|
||||||
|
|||||||
@@ -275,4 +275,42 @@ describe("useSettings hook", () => {
|
|||||||
);
|
);
|
||||||
expect(metadataMock.setRequiresRestart).toHaveBeenCalledWith(false);
|
expect(metadataMock.setRequiresRestart).toHaveBeenCalledWith(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("returns null immediately when settings state is missing", async () => {
|
||||||
|
settingsFormMock = createSettingsFormMock({
|
||||||
|
settings: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const { result } = renderHook(() => useSettings());
|
||||||
|
|
||||||
|
let resultValue: { requiresRestart: boolean } | null = null;
|
||||||
|
await act(async () => {
|
||||||
|
resultValue = await result.current.saveSettings();
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(resultValue).toBeNull();
|
||||||
|
expect(mutateAsyncMock).not.toHaveBeenCalled();
|
||||||
|
expect(setAppConfigDirOverrideMock).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws when save mutation rejects and keeps restart flag untouched", async () => {
|
||||||
|
settingsFormMock = createSettingsFormMock();
|
||||||
|
directorySettingsMock = createDirectorySettingsMock({
|
||||||
|
appConfigDir: "/override/app",
|
||||||
|
initialAppConfigDir: "/override/app",
|
||||||
|
});
|
||||||
|
const rejection = new Error("save failed");
|
||||||
|
mutateAsyncMock.mockRejectedValueOnce(rejection);
|
||||||
|
|
||||||
|
const { result } = renderHook(() => useSettings());
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
act(async () => {
|
||||||
|
await result.current.saveSettings();
|
||||||
|
}),
|
||||||
|
).rejects.toThrow("save failed");
|
||||||
|
|
||||||
|
expect(setAppConfigDirOverrideMock).not.toHaveBeenCalled();
|
||||||
|
expect(metadataMock.setRequiresRestart).not.toHaveBeenCalledWith(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user