test: add comprehensive tests for settings dialog components
Add component tests for ImportExportSection and SettingsDialog with full coverage of UI interactions, state management, and async workflows. ImportExportSection.test.tsx (5 tests): - Verify button states based on file selection - Test import/export/clear interactions - Validate loading, success, and error UI states SettingsDialog.test.tsx (5 tests): - Test loading state rendering - Verify tab navigation and child component callbacks - Validate save/cancel workflows with cleanup - Test restart prompt and immediate restart flow - Use Context Provider pattern to mock Tabs component - Mock 7 child components for isolation Test patterns demonstrated: - Complex component isolation with deep mocking - Context Provider mocking for UI library components - Async workflow validation with waitFor - Multi-hook mocking (useSettings + useImportExport) All 45 tests passing (7 files, 1.13s execution time)
This commit is contained in:
107
tests/components/ImportExportSection.test.tsx
Normal file
107
tests/components/ImportExportSection.test.tsx
Normal file
@@ -0,0 +1,107 @@
|
||||
import { render, screen, fireEvent } from "@testing-library/react";
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { ImportExportSection } from "@/components/settings/ImportExportSection";
|
||||
|
||||
const tMock = vi.fn((key: string) => key);
|
||||
|
||||
vi.mock("react-i18next", () => ({
|
||||
useTranslation: () => ({ t: tMock }),
|
||||
}));
|
||||
|
||||
describe("ImportExportSection Component", () => {
|
||||
const baseProps = {
|
||||
status: "idle" as const,
|
||||
selectedFile: "",
|
||||
errorMessage: null,
|
||||
backupId: null,
|
||||
isImporting: false,
|
||||
onSelectFile: vi.fn(),
|
||||
onImport: vi.fn(),
|
||||
onExport: vi.fn(),
|
||||
onClear: vi.fn(),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
tMock.mockImplementation((key: string) => key);
|
||||
baseProps.onSelectFile.mockReset();
|
||||
baseProps.onImport.mockReset();
|
||||
baseProps.onExport.mockReset();
|
||||
baseProps.onClear.mockReset();
|
||||
});
|
||||
|
||||
it("should disable import button and show placeholder when no file selected", () => {
|
||||
render(<ImportExportSection {...baseProps} />);
|
||||
|
||||
expect(screen.getByText("settings.noFileSelected")).toBeInTheDocument();
|
||||
expect(screen.getByRole("button", { name: "settings.import" })).toBeDisabled();
|
||||
fireEvent.click(screen.getByRole("button", { name: "settings.exportConfig" }));
|
||||
expect(baseProps.onExport).toHaveBeenCalledTimes(1);
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: "settings.selectConfigFile" }));
|
||||
expect(baseProps.onSelectFile).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("should show filename and enable import/clear when file is selected", () => {
|
||||
render(
|
||||
<ImportExportSection
|
||||
{...baseProps}
|
||||
selectedFile={"/tmp/test/config.json"}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByText("config.json")).toBeInTheDocument();
|
||||
const importButton = screen.getByRole("button", { name: "settings.import" });
|
||||
expect(importButton).toBeEnabled();
|
||||
fireEvent.click(importButton);
|
||||
expect(baseProps.onImport).toHaveBeenCalledTimes(1);
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: "common.clear" }));
|
||||
expect(baseProps.onClear).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("should show loading text and disable import button during import", () => {
|
||||
render(
|
||||
<ImportExportSection
|
||||
{...baseProps}
|
||||
selectedFile={"/tmp/test/config.json"}
|
||||
isImporting
|
||||
status="importing"
|
||||
/>,
|
||||
);
|
||||
|
||||
const importingLabels = screen.getAllByText("settings.importing");
|
||||
expect(importingLabels.length).toBeGreaterThanOrEqual(2);
|
||||
expect(
|
||||
screen.getByRole("button", { name: "settings.importing" }),
|
||||
).toBeDisabled();
|
||||
expect(screen.getByText("common.loading")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("should display backup information on successful import", () => {
|
||||
render(
|
||||
<ImportExportSection
|
||||
{...baseProps}
|
||||
selectedFile={"/tmp/test/config.json"}
|
||||
status="success"
|
||||
backupId="backup-001"
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByText("settings.importSuccess")).toBeInTheDocument();
|
||||
expect(screen.getByText(/backup-001/)).toBeInTheDocument();
|
||||
expect(screen.getByText("settings.autoReload")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("should display error message when import fails", () => {
|
||||
render(
|
||||
<ImportExportSection
|
||||
{...baseProps}
|
||||
status="error"
|
||||
errorMessage="Parse failed"
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByText("settings.importFailed")).toBeInTheDocument();
|
||||
expect(screen.getByText("Parse failed")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user