Skip to content

Commit

Permalink
test: add more test to entry
Browse files Browse the repository at this point in the history
Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly committed Jul 11, 2024
1 parent 4824fae commit 2a7315c
Show file tree
Hide file tree
Showing 6 changed files with 15,373 additions and 1,978 deletions.
50 changes: 26 additions & 24 deletions ui/src/Tooltip.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { render } from "@testing-library/react";
import { expect, it } from "vitest";
import { describe, expect, it } from "vitest";
import { Tooltip } from "./Tooltip.tsx";
import type { Entry, EntryChildren, EntryType } from "./tool/entry.ts";

Expand Down Expand Up @@ -29,28 +29,30 @@ function getTestNode(): Entry {
};
}

it("tooltip should render", () => {
const { getByText } = render(
<Tooltip
x={0}
y={0}
node={getTestNode()}
/>,
);
expect(getByText("test")).toBeInTheDocument();
expect(getByText("test content")).toBeInTheDocument();
});
describe("tooltip", () => {
it("should render", () => {
const { getByText } = render(
<Tooltip
x={0}
y={0}
node={getTestNode()}
/>,
);
expect(getByText("test")).toBeInTheDocument();
expect(getByText("test content")).toBeInTheDocument();
});

it("tooltip should respond to position", () => {
const r = render(
<Tooltip
x={0}
y={0}
node={getTestNode()}
/>,
);
const tooltip = r.container.querySelector<HTMLElement>(".tooltip");
expect(tooltip).not.toBeNull();
expect(tooltip!.style.left).toBe("10px");
expect(tooltip!.style.top).toBe("30px");
it("should respond to position", () => {
const r = render(
<Tooltip
x={0}
y={0}
node={getTestNode()}
/>,
);
const tooltip = r.container.querySelector<HTMLElement>(".tooltip");
expect(tooltip).not.toBeNull();
expect(tooltip!.style.left).toBe("10px");
expect(tooltip!.style.top).toBe("30px");
});
});
8 changes: 4 additions & 4 deletions ui/src/explorer/Explorer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ describe("explorer", () => {
});
});

it("explorer should display loading state initially", () => {
it("should display loading state initially", () => {
render(<Explorer />);
expect(screen.getByText("Loading WebAssembly module...")).toBeInTheDocument();
});

it("explorer should display file selector when no file is selected", async () => {
it("should display file selector when no file is selected", async () => {
render(<Explorer />);
await waitFor(() => screen.getByText("Select a go binary"));
});

it("explorer should display analyzing state when a file is selected", async () => {
it("should display analyzing state when a file is selected", async () => {
render(<Explorer />);

await waitFor(() => screen.getByText("Select a go binary"));
Expand All @@ -61,7 +61,7 @@ describe("explorer", () => {
await waitFor(() => screen.getByText("Analyzing binary..."));
});

it("explorer should display error when analysis fails", async () => {
it("should display error when analysis fails", async () => {
vi.stubGlobal("gsa_analyze", () => {
return null;
});
Expand Down
110 changes: 56 additions & 54 deletions ui/src/explorer/FileSelector.test.tsx
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
import { fireEvent, render } from "@testing-library/react";
import { expect, it, vi } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { FileSelector } from "./FileSelector.tsx";

it("fileSelector should render correctly", () => {
const mockHandler = vi.fn();
const { getByText } = render(<FileSelector handler={mockHandler} />);
expect(getByText("Select file")).toBeInTheDocument();
});

it("fileSelector should call handler when file size is within limit", () => {
const mockHandler = vi.fn();
const { getByLabelText } = render(<FileSelector handler={mockHandler} />);
const file = new File(["dummy content"], "dummy.txt", { type: "text/plain" });
fireEvent.change(getByLabelText("Select file"), { target: { files: [file] } });
expect(mockHandler).toHaveBeenCalledWith(file);
});

it("fileSelector should not call handler when no file is selected", () => {
const mockHandler = vi.fn();
const { getByLabelText } = render(<FileSelector handler={mockHandler} />);
fireEvent.change(getByLabelText("Select file"), { target: { files: [] } });
expect(mockHandler).not.toHaveBeenCalled();
});

it("fileSelector should not call handler when file size exceeds limit", () => {
const mockHandler = vi.fn();
const { getByLabelText } = render(<FileSelector handler={mockHandler} />);
const file = {
size: 1024 * 1024 * 31,
};

fireEvent.change(getByLabelText("Select file"), { target: { files: [file] } });
expect(mockHandler).not.toHaveBeenCalled();
});

it("fileSelector should call handler when file size exceeds limit and user chooses to continue", () => {
const mockHandler = vi.fn();
const { getByLabelText, getByText } = render(<FileSelector handler={mockHandler} />);

const file = {
size: 1024 * 1024 * 31,
};

fireEvent.change(getByLabelText("Select file"), { target: { files: [file] } });
fireEvent.click(getByText("Continue"));
expect(mockHandler).toHaveBeenCalledWith(file);
});

it("fileSelector should not call handler when file size exceeds limit and user chooses to cancel", () => {
const mockHandler = vi.fn();
const { getByLabelText, getByText } = render(<FileSelector handler={mockHandler} />);
const file = new File(["0".repeat(1024 * 1024 * 31)], "dummy.txt", { type: "text/plain" });

fireEvent.change(getByLabelText("Select file"), { target: { files: [file] } });
fireEvent.click(getByText("Cancel"));
expect(mockHandler).not.toHaveBeenCalled();
describe("fileSelector", () => {
it("should render correctly", () => {
const mockHandler = vi.fn();
const { getByText } = render(<FileSelector handler={mockHandler} />);
expect(getByText("Select file")).toBeInTheDocument();
});

it("should call handler when file size is within limit", () => {
const mockHandler = vi.fn();
const { getByLabelText } = render(<FileSelector handler={mockHandler} />);
const file = new File(["dummy content"], "dummy.txt", { type: "text/plain" });
fireEvent.change(getByLabelText("Select file"), { target: { files: [file] } });
expect(mockHandler).toHaveBeenCalledWith(file);
});

it("should not call handler when no file is selected", () => {
const mockHandler = vi.fn();
const { getByLabelText } = render(<FileSelector handler={mockHandler} />);
fireEvent.change(getByLabelText("Select file"), { target: { files: [] } });
expect(mockHandler).not.toHaveBeenCalled();
});

it("should not call handler when file size exceeds limit", () => {
const mockHandler = vi.fn();
const { getByLabelText } = render(<FileSelector handler={mockHandler} />);
const file = {
size: 1024 * 1024 * 31,
};

fireEvent.change(getByLabelText("Select file"), { target: { files: [file] } });
expect(mockHandler).not.toHaveBeenCalled();
});

it("should call handler when file size exceeds limit and user chooses to continue", () => {
const mockHandler = vi.fn();
const { getByLabelText, getByText } = render(<FileSelector handler={mockHandler} />);

const file = {
size: 1024 * 1024 * 31,
};

fireEvent.change(getByLabelText("Select file"), { target: { files: [file] } });
fireEvent.click(getByText("Continue"));
expect(mockHandler).toHaveBeenCalledWith(file);
});

it("should not call handler when file size exceeds limit and user chooses to cancel", () => {
const mockHandler = vi.fn();
const { getByLabelText, getByText } = render(<FileSelector handler={mockHandler} />);
const file = new File(["0".repeat(1024 * 1024 * 31)], "dummy.txt", { type: "text/plain" });

fireEvent.change(getByLabelText("Select file"), { target: { files: [file] } });
fireEvent.click(getByText("Cancel"));
expect(mockHandler).not.toHaveBeenCalled();
});
});
Loading

0 comments on commit 2a7315c

Please sign in to comment.