React tests: Wrap code that causes state updates in act() (#2372)

This commit is contained in:
Shati Patel
2023-04-25 15:01:53 +01:00
committed by GitHub
parent 2d68c1c735
commit 298176d8f1
2 changed files with 37 additions and 22 deletions

View File

@@ -1,5 +1,10 @@
import * as React from "react";
import { render as reactRender, screen, waitFor } from "@testing-library/react";
import {
act,
render as reactRender,
screen,
waitFor,
} from "@testing-library/react";
import {
VariantAnalysisRepoStatus,
VariantAnalysisScannedRepositoryDownloadStatus,
@@ -319,11 +324,13 @@ describe(RepoRow.name, () => {
status: VariantAnalysisRepoStatus.TimedOut,
});
await userEvent.click(
screen.getByRole("button", {
expanded: false,
}),
);
await act(async () => {
await userEvent.click(
screen.getByRole("button", {
expanded: false,
}),
);
});
screen.getByRole("button", {
expanded: true,
@@ -342,11 +349,13 @@ describe(RepoRow.name, () => {
interpretedResults: [],
});
await userEvent.click(
screen.getByRole("button", {
expanded: false,
}),
);
await act(async () => {
await userEvent.click(
screen.getByRole("button", {
expanded: false,
}),
);
});
expect(
screen.getByRole("button", {
@@ -365,11 +374,13 @@ describe(RepoRow.name, () => {
},
});
await userEvent.click(
screen.getByRole("button", {
expanded: false,
}),
);
await act(async () => {
await userEvent.click(
screen.getByRole("button", {
expanded: false,
}),
);
});
expect((window as any).vsCodeApi.postMessage).toHaveBeenCalledWith({
t: "requestRepositoryResults",

View File

@@ -1,5 +1,5 @@
import * as React from "react";
import { render as reactRender, screen } from "@testing-library/react";
import { act, render as reactRender, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import {
VariantAnalysisRepoStatus,
@@ -155,11 +155,15 @@ describe(VariantAnalysisAnalyzedRepos.name, () => {
expect(
screen.queryByText("This is an empty block."),
).not.toBeInTheDocument();
await userEvent.click(
screen.getByRole("button", {
name: /octodemo\/hello-world-2/,
}),
);
await act(async () => {
await userEvent.click(
screen.getByRole("button", {
name: /octodemo\/hello-world-2/,
}),
);
});
expect(screen.getByText("This is an empty block.")).toBeInTheDocument();
});