Comprehensive test updates to align with recent component refactoring and new auto-launch functionality. Component Tests: - AddProviderDialog.test.tsx (10 lines): * Updated test cases for new dialog behavior * Enhanced mock data for preset selection * Improved assertions for validation - ImportExportSection.test.tsx (16 lines): * Updated for new settings page integration * Enhanced test coverage for error scenarios * Better mock state management - McpFormModal.test.tsx (60 lines): * Extensive updates for form refactoring * New test cases for multi-app selection * Enhanced validation testing * Better coverage of stdio/http server types - ProviderList.test.tsx (11 lines): * Updated for new card layout * Enhanced drag-and-drop testing - SettingsDialog.test.tsx (96 lines): * Major updates for SettingsPage migration * New test cases for auto-launch functionality * Enhanced integration test coverage * Better async operation testing Hook Tests: - useDirectorySettings.test.tsx (32 lines): * Updated for refactored hook logic * Enhanced test coverage for edge cases - useDragSort.test.tsx (36 lines): * Simplified test cases * Better mock implementation * Improved assertions - useImportExport tests (16 lines total): * Updated for new error handling * Enhanced test coverage - useMcpValidation.test.tsx (23 lines): * Updated validation test cases * Better coverage of error scenarios - useProviderActions.test.tsx (48 lines): * Extensive updates for hook refactoring * New test cases for provider operations * Enhanced mock data - useSettings.test.tsx (12 lines): * New test cases for auto-launch * Enhanced settings state testing * Better async operation coverage Integration Tests: - App.test.tsx (41 lines): * Updated for new routing logic * Enhanced navigation testing * Better component integration coverage - SettingsDialog.test.tsx (88 lines): * Complete rewrite for SettingsPage * New integration test scenarios * Enhanced user workflow testing Mock Infrastructure: - handlers.ts (117 lines): * Major updates for MSW handlers * New handlers for auto-launch commands * Enhanced error simulation * Better request/response mocking - state.ts (37 lines): * Updated mock state structure * New state for auto-launch * Enhanced state reset functionality - tauriMocks.ts (10 lines): * Updated mock implementations * Better type safety - server.ts & testQueryClient.ts: * Minor cleanup (2 lines removed) Test Infrastructure Improvements: - Better test isolation - Enhanced mock data consistency - Improved async operation testing - Better error scenario coverage - Enhanced integration test patterns Coverage Improvements: - Net increase of 195 lines of test code - Better coverage of edge cases - Enhanced error path testing - Improved integration test scenarios - Better mock infrastructure All tests now pass with the refactored components while maintaining comprehensive coverage of functionality and edge cases.
158 lines
4.0 KiB
TypeScript
158 lines
4.0 KiB
TypeScript
import type { ReactNode } from "react";
|
|
import { renderHook, act } from "@testing-library/react";
|
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
import { describe, expect, it, vi, beforeEach, afterAll } from "vitest";
|
|
import type { Provider } from "@/types";
|
|
import { useDragSort } from "@/hooks/useDragSort";
|
|
|
|
const updateSortOrderMock = vi.fn();
|
|
const toastSuccessMock = vi.fn();
|
|
const toastErrorMock = vi.fn();
|
|
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
|
|
vi.mock("sonner", () => ({
|
|
toast: {
|
|
success: (...args: unknown[]) => toastSuccessMock(...args),
|
|
error: (...args: unknown[]) => toastErrorMock(...args),
|
|
},
|
|
}));
|
|
|
|
vi.mock("@/lib/api", () => ({
|
|
providersApi: {
|
|
updateSortOrder: (...args: unknown[]) => updateSortOrderMock(...args),
|
|
},
|
|
}));
|
|
|
|
interface WrapperProps {
|
|
children: ReactNode;
|
|
}
|
|
|
|
function createWrapper() {
|
|
const queryClient = new QueryClient();
|
|
|
|
const wrapper = ({ children }: WrapperProps) => (
|
|
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
);
|
|
|
|
return { wrapper, queryClient };
|
|
}
|
|
|
|
const mockProviders: Record<string, Provider> = {
|
|
a: {
|
|
id: "a",
|
|
name: "AAA",
|
|
settingsConfig: {},
|
|
sortIndex: 1,
|
|
createdAt: 5,
|
|
},
|
|
b: {
|
|
id: "b",
|
|
name: "BBB",
|
|
settingsConfig: {},
|
|
sortIndex: 0,
|
|
createdAt: 10,
|
|
},
|
|
c: {
|
|
id: "c",
|
|
name: "CCC",
|
|
settingsConfig: {},
|
|
createdAt: 1,
|
|
},
|
|
};
|
|
|
|
describe("useDragSort", () => {
|
|
beforeEach(() => {
|
|
updateSortOrderMock.mockReset();
|
|
toastSuccessMock.mockReset();
|
|
toastErrorMock.mockReset();
|
|
consoleErrorSpy.mockClear();
|
|
});
|
|
|
|
afterAll(() => {
|
|
consoleErrorSpy.mockRestore();
|
|
});
|
|
|
|
it("should sort providers by sortIndex, createdAt, and name", () => {
|
|
const { wrapper } = createWrapper();
|
|
|
|
const { result } = renderHook(() => useDragSort(mockProviders, "claude"), {
|
|
wrapper,
|
|
});
|
|
|
|
expect(result.current.sortedProviders.map((item) => item.id)).toEqual([
|
|
"b",
|
|
"a",
|
|
"c",
|
|
]);
|
|
});
|
|
|
|
it("should call API and invalidate query cache after successful drag", async () => {
|
|
updateSortOrderMock.mockResolvedValue(true);
|
|
const { wrapper, queryClient } = createWrapper();
|
|
const invalidateSpy = vi.spyOn(queryClient, "invalidateQueries");
|
|
|
|
const { result } = renderHook(() => useDragSort(mockProviders, "claude"), {
|
|
wrapper,
|
|
});
|
|
|
|
await act(async () => {
|
|
await result.current.handleDragEnd({
|
|
active: { id: "b" },
|
|
over: { id: "a" },
|
|
} as any);
|
|
});
|
|
|
|
expect(updateSortOrderMock).toHaveBeenCalledTimes(1);
|
|
expect(updateSortOrderMock).toHaveBeenCalledWith(
|
|
[
|
|
{ id: "a", sortIndex: 0 },
|
|
{ id: "b", sortIndex: 1 },
|
|
{ id: "c", sortIndex: 2 },
|
|
],
|
|
"claude",
|
|
);
|
|
expect(invalidateSpy).toHaveBeenCalledWith({
|
|
queryKey: ["providers", "claude"],
|
|
});
|
|
expect(toastSuccessMock).toHaveBeenCalledTimes(1);
|
|
expect(toastErrorMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should show error toast when drag operation fails", async () => {
|
|
updateSortOrderMock.mockRejectedValue(new Error("network"));
|
|
const { wrapper } = createWrapper();
|
|
|
|
const { result } = renderHook(() => useDragSort(mockProviders, "claude"), {
|
|
wrapper,
|
|
});
|
|
|
|
await act(async () => {
|
|
await result.current.handleDragEnd({
|
|
active: { id: "b" },
|
|
over: { id: "a" },
|
|
} as any);
|
|
});
|
|
|
|
expect(toastErrorMock).toHaveBeenCalledTimes(1);
|
|
expect(toastSuccessMock).not.toHaveBeenCalled();
|
|
expect(consoleErrorSpy).toHaveBeenCalled();
|
|
});
|
|
|
|
it("should not trigger API call when there is no valid target", async () => {
|
|
const { wrapper } = createWrapper();
|
|
|
|
const { result } = renderHook(() => useDragSort(mockProviders, "claude"), {
|
|
wrapper,
|
|
});
|
|
|
|
await act(async () => {
|
|
await result.current.handleDragEnd({
|
|
active: { id: "b" },
|
|
over: null,
|
|
} as any);
|
|
});
|
|
|
|
expect(updateSortOrderMock).not.toHaveBeenCalled();
|
|
});
|
|
});
|