Skip to content

fix: replace fireEvent with userEvent #5361

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

Merged
merged 2 commits into from
Dec 9, 2022
Merged
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
16 changes: 10 additions & 6 deletions site/src/pages/WorkspacePage/WorkspacePage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fireEvent, screen, waitFor, within } from "@testing-library/react"
import { screen, waitFor, within } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import EventSourceMock from "eventsourcemock"
import i18next from "i18next"
Expand Down Expand Up @@ -48,9 +48,11 @@ const renderWorkspacePage = async () => {
* workspaceStatus was calculated correctly.
*/
const testButton = async (label: string, actionMock: jest.SpyInstance) => {
const user = userEvent.setup()

await renderWorkspacePage()
const button = await screen.findByRole("button", { name: label })
fireEvent.click(button)
await user.click(button)
expect(actionMock).toBeCalled()
}

Expand All @@ -65,7 +67,7 @@ const testStatus = async (ws: Workspace, label: string) => {
)
await renderWorkspacePage()
const header = screen.getByTestId("header")
const status = await within(header).findByRole("status")
const status = within(header).getByRole("status")
expect(status).toHaveTextContent(label)
}

Expand All @@ -88,6 +90,7 @@ afterAll(() => {
describe("WorkspacePage", () => {
it("requests a delete job when the user presses Delete and confirms", async () => {
const user = userEvent.setup()

const deleteWorkspaceMock = jest
.spyOn(api, "deleteWorkspace")
.mockResolvedValueOnce(MockWorkspaceBuild)
Expand Down Expand Up @@ -160,7 +163,7 @@ describe("WorkspacePage", () => {
name: "cancel action",
})

fireEvent.click(cancelButton)
await userEvent.setup().click(cancelButton)

expect(cancelWorkspaceMock).toBeCalled()
})
Expand All @@ -180,7 +183,7 @@ describe("WorkspacePage", () => {
await renderWorkspacePage()
const buttonText = t("actionButton.update", { ns: "workspacePage" })
const button = await screen.findByText(buttonText, { exact: true })
fireEvent.click(button)
await userEvent.setup().click(button)

// getTemplate is called twice: once when the machine starts, and once after the user requests to update
expect(getTemplateMock).toBeCalledTimes(2)
Expand All @@ -202,7 +205,7 @@ describe("WorkspacePage", () => {
await renderWorkspacePage()
const buttonText = t("actionButton.update", { ns: "workspacePage" })
const button = await screen.findByText(buttonText, { exact: true })
fireEvent.click(button)
await userEvent.setup().click(button)

await waitFor(() =>
expect(api.startWorkspace).toBeCalledWith(
Expand All @@ -211,6 +214,7 @@ describe("WorkspacePage", () => {
),
)
})

it("shows the Stopping status when the workspace is stopping", async () => {
await testStatus(
MockStoppingWorkspace,
Expand Down