File size: 2,160 Bytes
6a7089a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import { act, render, screen, waitFor } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import InstanceLogsPanel from "./InstanceLogsPanel";
const { fetchInstanceLogs, subscribeToInstanceLogs } = vi.hoisted(() => ({
fetchInstanceLogs: vi.fn(),
subscribeToInstanceLogs: vi.fn(),
}));
vi.mock("../../services/api", () => ({
fetchInstanceLogs,
subscribeToInstanceLogs,
}));
describe("InstanceLogsPanel", () => {
beforeEach(() => {
vi.clearAllMocks();
subscribeToInstanceLogs.mockReturnValue(() => {});
});
afterEach(() => {
vi.restoreAllMocks();
});
it("loads logs on mount when an instance id is provided", async () => {
fetchInstanceLogs.mockResolvedValue("first line\nsecond line");
render(<InstanceLogsPanel instanceId="inst_123" />);
expect(screen.getByText("Loading logs...")).toBeInTheDocument();
await waitFor(() => {
expect(document.querySelector("pre")).toHaveTextContent(
"first line second line",
);
});
expect(fetchInstanceLogs).toHaveBeenCalledWith("inst_123");
expect(subscribeToInstanceLogs).toHaveBeenCalledWith("inst_123", {
onLogs: expect.any(Function),
});
});
it("updates rendered logs from the subscription stream", async () => {
fetchInstanceLogs.mockResolvedValue("");
let onLogs: ((logs: string) => void) | undefined;
subscribeToInstanceLogs.mockImplementation((_id, handlers) => {
onLogs = handlers.onLogs;
return () => {};
});
render(<InstanceLogsPanel instanceId="inst_123" />);
await waitFor(() => {
expect(subscribeToInstanceLogs).toHaveBeenCalledTimes(1);
});
await act(async () => {
onLogs?.("streamed logs");
});
expect(document.querySelector("pre")).toHaveTextContent("streamed logs");
});
it("shows the empty state when no instance is available", () => {
render(<InstanceLogsPanel />);
expect(screen.getByText("No instance logs available.")).toBeInTheDocument();
expect(fetchInstanceLogs).not.toHaveBeenCalled();
expect(subscribeToInstanceLogs).not.toHaveBeenCalled();
});
});
|