From 298176d8f10eeedecae461687c2786505b3e5c14 Mon Sep 17 00:00:00 2001 From: Shati Patel <42641846+shati-patel@users.noreply.github.com> Date: Tue, 25 Apr 2023 15:01:53 +0100 Subject: [PATCH] React tests: Wrap code that causes state updates in `act()` (#2372) --- .../__tests__/RepoRow.spec.tsx | 43 ++++++++++++------- .../VariantAnalysisAnalyzedRepos.spec.tsx | 16 ++++--- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/extensions/ql-vscode/src/view/variant-analysis/__tests__/RepoRow.spec.tsx b/extensions/ql-vscode/src/view/variant-analysis/__tests__/RepoRow.spec.tsx index 8e740307d..a0f90cf58 100644 --- a/extensions/ql-vscode/src/view/variant-analysis/__tests__/RepoRow.spec.tsx +++ b/extensions/ql-vscode/src/view/variant-analysis/__tests__/RepoRow.spec.tsx @@ -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", diff --git a/extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysisAnalyzedRepos.spec.tsx b/extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysisAnalyzedRepos.spec.tsx index a1cc2ad6b..64cb3729a 100644 --- a/extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysisAnalyzedRepos.spec.tsx +++ b/extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysisAnalyzedRepos.spec.tsx @@ -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(); });