fix(test): update component tests to match current implementation

- Add JsonEditor mock to McpFormModal tests (component uses CodeMirror
  instead of Textarea)
- Fix assertion for missing command error message
- Update ImportExportSection tests for new button behavior and file
  display format
This commit is contained in:
Jason
2025-11-28 16:20:18 +08:00
parent 3878a16c4f
commit f4c284f86c
2 changed files with 19 additions and 8 deletions
@@ -32,17 +32,17 @@ describe("ImportExportSection Component", () => {
it("should disable import button and show placeholder when no file selected", () => {
render(<ImportExportSection {...baseProps} />);
expect(screen.getByText("settings.noFileSelected")).toBeInTheDocument();
// When no file selected, button shows "selectConfigFile" and clicking it opens file dialog
expect(
screen.getByRole("button", { name: "settings.import" }),
).toBeDisabled();
screen.getByRole("button", { name: /settings\.selectConfigFile/ }),
).toBeInTheDocument();
fireEvent.click(
screen.getByRole("button", { name: "settings.exportConfig" }),
);
expect(baseProps.onExport).toHaveBeenCalledTimes(1);
fireEvent.click(
screen.getByRole("button", { name: "settings.selectConfigFile" }),
screen.getByRole("button", { name: /settings\.selectConfigFile/ }),
);
expect(baseProps.onSelectFile).toHaveBeenCalledTimes(1);
});
@@ -55,15 +55,15 @@ describe("ImportExportSection Component", () => {
/>,
);
expect(screen.getByText("config.json")).toBeInTheDocument();
expect(screen.getByText(/config\.json/)).toBeInTheDocument();
const importButton = screen.getByRole("button", {
name: "settings.import",
name: /settings\.import/,
});
expect(importButton).toBeEnabled();
fireEvent.click(importButton);
expect(baseProps.onImport).toHaveBeenCalledTimes(1);
fireEvent.click(screen.getByRole("button", { name: "common.clear" }));
fireEvent.click(screen.getByRole("button", { name: "Clear selection" }));
expect(baseProps.onClear).toHaveBeenCalledTimes(1);
});
+12 -1
View File
@@ -79,6 +79,17 @@ vi.mock("@/components/ui/textarea", () => ({
),
}));
vi.mock("@/components/JsonEditor", () => ({
default: ({ value, onChange, placeholder, ...rest }: any) => (
<textarea
value={value}
placeholder={placeholder}
onChange={(event) => onChange?.(event.target.value)}
{...rest}
/>
),
}));
vi.mock("@/components/ui/checkbox", () => ({
Checkbox: ({ id, checked, onCheckedChange, ...rest }: any) => (
<input
@@ -256,7 +267,7 @@ describe("McpFormModal", () => {
await waitFor(() => expect(toastErrorMock).toHaveBeenCalled());
expect(upsertMock).not.toHaveBeenCalled();
const [message] = toastErrorMock.mock.calls.at(-1) ?? [];
expect(message).toBe("mcp.error.jsonInvalid");
expect(message).toBe("mcp.error.commandRequired");
});
it("支持向导生成配置并自动填充 ID", async () => {