-
Notifications
You must be signed in to change notification settings - Fork 4
test(frontend): add tests for RetriggerWorkflow #1017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NatLeung96
wants to merge
1
commit into
main
Choose a base branch
from
nl/add-test-for-retriggerworkflow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
frontend/relay-workflows-lib/tests/query-components/RetriggerWorkflow.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(), | ||
| }; | ||
| }); | ||
|
|
||
| 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(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"]);There was a problem hiding this comment.
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.