Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import Tooltip from "@mui/material/Tooltip";
import RefreshIcon from "@mui/icons-material/Refresh";
import { graphql } from "relay-runtime";
import { useLazyLoadQuery } from "react-relay/hooks";
import { useLazyLoadQuery } from "react-relay";
import { NavLink } from "react-router-dom";
import { RetriggerWorkflowQuery as RetriggerWorkflowQueryType } from "./__generated__/RetriggerWorkflowQuery.graphql";
import { Visit, visitToText } from "@diamondlightsource/sci-react-ui";
Expand Down Expand Up @@ -57,7 +57,7 @@ const RetriggerWorkflowBase: React.FC<RetriggerWorkflowProps> = ({
);
};

const RetriggerWorkflow: React.FC<RetriggerWorkflowProps> = (props) => (
export const RetriggerWorkflow: React.FC<RetriggerWorkflowProps> = (props) => (
<WorkflowsErrorBoundary fallback={<NoTemplateIcon />}>
<RetriggerWorkflowBase {...props} />
</WorkflowsErrorBoundary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";
import { RelayEnvironmentProvider } from "react-relay";
import { getRelayEnvironment } from "dashboard/src/RelayEnvironment";
import { MemoryRouter } from "react-router-dom";
import { RetriggerWorkflowQuery$data } from "../../lib/query-components/__generated__/RetriggerWorkflowQuery.graphql";
import { RetriggerWorkflow } from "../../lib/query-components/RetriggerWorkflow";
import { mockLazyLoadQuery } from "../testUtils";

vi.mock("react-relay", async () => {
const actual = await vi.importActual("react-relay");
return {
...actual,
RelayEnvironmentProvider: actual.RelayEnvironmentProvider,
useLazyLoadQuery: vi.fn(),
};
});
Comment on lines +10 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could create a helper function for mocking to use in all tests where you pass the name of hooks to mock e.g.
mockRelay(["useLazyLoadQuery", "useFragment"]);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it's worth it. The issue is that vi.mock() cannot take higher level variables because it gets hoisted. You could get around that by using vi.doMock() instead but it's very messy as it requires reimporting things and test components would need to be stored in separate files.


describe("RetriggerWorkflow", () => {
afterEach(() => {
vi.clearAllMocks();
});
const renderComponent = async () => {
const environment = await getRelayEnvironment();
render(
<MemoryRouter initialEntries={["/workflows/abc1234-1"]}>
<RelayEnvironmentProvider environment={environment}>
<RetriggerWorkflow
instrumentSession={{
proposalCode: "abc",
proposalNumber: 1234,
number: 1,
}}
workflowName="mock-workflow"
/>
</RelayEnvironmentProvider>
</MemoryRouter>,
);
};

it("renders a link to previous workflow submission page", async () => {
mockLazyLoadQuery<RetriggerWorkflowQuery$data>({
workflow: {
templateRef: "mock-template",
},
});

await renderComponent();

const icon = await screen.findByTestId("RefreshIcon");
expect(icon).toBeInTheDocument();
const link = screen.getByRole("link", { name: "Rerun workflow" });
expect(link).toBeInTheDocument();
expect(link).toHaveAttribute(
"href",
"/templates/mock-template/abc1234-1-mock-workflow",
);
});

it("renders no template icon", async () => {
mockLazyLoadQuery<RetriggerWorkflowQuery$data>({
workflow: {
templateRef: null,
},
});

await renderComponent();

const icon = await screen.findByTestId("RefreshIcon");
expect(icon).toBeInTheDocument();
expect(screen.getByLabelText("No template found")).toBeInTheDocument();
});
});
7 changes: 7 additions & 0 deletions frontend/relay-workflows-lib/tests/testUtils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useLazyLoadQuery } from "react-relay";

export function mockLazyLoadQuery<T>(response: T): T {
vi.mocked(useLazyLoadQuery as () => T).mockReturnValueOnce(response);

return response;
}