From 326790d80d97978ffbcbcec4b7d76e798338ea0c Mon Sep 17 00:00:00 2001 From: Aleksandr Gumroian Date: Tue, 25 Feb 2025 14:04:07 +0100 Subject: [PATCH] Replace 'wait' with 'waitFor' --- jest.config.js | 5 +---- src/bootstrap/__tests__/NumberInput.test.js | 8 ++++---- .../__tests__/ResetWiFiSettings.test.js | 8 ++++---- .../WiFiSettings/__tests__/WiFiSettings.test.js | 9 +++++---- src/common/__tests__/ActionButtonWithModal.test.js | 14 ++++---------- .../__tests__/CustomizationContext.test.js | 8 ++++---- src/form/__tests__/hooks.test.js | 6 +++--- 7 files changed, 25 insertions(+), 33 deletions(-) diff --git a/jest.config.js b/jest.config.js index b06a9a0..745ca44 100644 --- a/jest.config.js +++ b/jest.config.js @@ -21,10 +21,7 @@ module.exports = { testPathIgnorePatterns: ["/node_modules/", "/__fixtures__/", "/dist/"], testEnvironment: "jsdom", verbose: false, - setupFilesAfterEnv: [ - "@testing-library/react/cleanup-after-each", - "/src/testUtils/setup", - ], + setupFilesAfterEnv: ["/src/testUtils/setup"], globals: { TZ: "utc", }, diff --git a/src/bootstrap/__tests__/NumberInput.test.js b/src/bootstrap/__tests__/NumberInput.test.js index c0d89df..adc75ac 100644 --- a/src/bootstrap/__tests__/NumberInput.test.js +++ b/src/bootstrap/__tests__/NumberInput.test.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2024 CZ.NIC z.s.p.o. (https://www.nic.cz/) + * Copyright (C) 2019-2025 CZ.NIC z.s.p.o. (https://www.nic.cz/) * * This is free software, licensed under the GNU General Public License v3. * See /LICENSE for more information. @@ -7,7 +7,7 @@ import React from "react"; -import { render, fireEvent, getByLabelText, wait } from "customTestRender"; +import { render, fireEvent, getByLabelText, waitFor } from "customTestRender"; import NumberInput from "../NumberInput"; @@ -34,7 +34,7 @@ describe("", () => { it("Increase number with button", async () => { const increaseButton = getByLabelText(componentContainer, /Increase/); fireEvent.mouseDown(increaseButton); - await wait(() => + await waitFor(() => expect(onChangeMock).toHaveBeenCalledWith({ target: { value: 2 } }) ); }); @@ -42,7 +42,7 @@ describe("", () => { it("Decrease number with button", async () => { const decreaseButton = getByLabelText(componentContainer, /Decrease/); fireEvent.mouseDown(decreaseButton); - await wait(() => + await waitFor(() => expect(onChangeMock).toHaveBeenCalledWith({ target: { value: 0 } }) ); }); diff --git a/src/common/WiFiSettings/__tests__/ResetWiFiSettings.test.js b/src/common/WiFiSettings/__tests__/ResetWiFiSettings.test.js index 5b388b6..a750a8f 100644 --- a/src/common/WiFiSettings/__tests__/ResetWiFiSettings.test.js +++ b/src/common/WiFiSettings/__tests__/ResetWiFiSettings.test.js @@ -1,12 +1,12 @@ /* - * Copyright (C) 2019-2021 CZ.NIC z.s.p.o. (http://www.nic.cz/) + * Copyright (C) 2019-2025 CZ.NIC z.s.p.o. (https://www.nic.cz/) * * This is free software, licensed under the GNU General Public License v3. * See /LICENSE for more information. */ import React from "react"; -import { render, fireEvent, wait } from "customTestRender"; +import { render, fireEvent, waitFor } from "customTestRender"; import mockAxios from "jest-mock-axios"; import WebSockets from "webSockets/WebSockets"; @@ -35,7 +35,7 @@ describe("", () => { expect.anything() ); mockAxios.mockResponse({ data: { foo: "bar" } }); - await wait(() => + await waitFor(() => expect(mockSetAlert).toBeCalledWith( "Wi-Fi settings are set to defaults.", ALERT_TYPES.SUCCESS @@ -46,7 +46,7 @@ describe("", () => { it("should display alert on open ports - failure", async () => { fireEvent.click(getAllByText("Reset Wi-Fi Settings")[1]); mockJSONError(); - await wait(() => + await waitFor(() => expect(mockSetAlert).toBeCalledWith( "An error occurred during resetting Wi-Fi settings." ) diff --git a/src/common/WiFiSettings/__tests__/WiFiSettings.test.js b/src/common/WiFiSettings/__tests__/WiFiSettings.test.js index b8d0241..f1b5b4f 100644 --- a/src/common/WiFiSettings/__tests__/WiFiSettings.test.js +++ b/src/common/WiFiSettings/__tests__/WiFiSettings.test.js @@ -1,15 +1,16 @@ /* - * Copyright (C) 2019-2021 CZ.NIC z.s.p.o. (http://www.nic.cz/) + * Copyright (C) 2019-2025 CZ.NIC z.s.p.o. (https://www.nic.cz/) * * This is free software, licensed under the GNU General Public License v3. * See /LICENSE for more information. */ import React from "react"; + import diffSnapshot from "snapshot-diff"; import mockAxios from "jest-mock-axios"; -import { fireEvent, render, wait } from "customTestRender"; +import { fireEvent, render, waitFor } from "customTestRender"; import WebSockets from "webSockets/WebSockets"; import { mockJSONError } from "testUtils/network"; @@ -45,7 +46,7 @@ describe("", () => { getByLabelText = renderRes.getByLabelText; getByText = renderRes.getByText; mockAxios.mockResponse({ data: wifiSettingsFixture() }); - await wait(() => renderRes.getByText("Wi-Fi 1")); + await waitFor(() => renderRes.getByText("Wi-Fi 1")); firstRender = renderRes.asFragment(); }); @@ -60,7 +61,7 @@ describe("", () => { ); const errorMessage = "An API error occurred."; mockJSONError(errorMessage); - await wait(() => { + await waitFor(() => { expect(getByText(errorMessage)).toBeTruthy(); }); }); diff --git a/src/common/__tests__/ActionButtonWithModal.test.js b/src/common/__tests__/ActionButtonWithModal.test.js index a17037c..de342d0 100644 --- a/src/common/__tests__/ActionButtonWithModal.test.js +++ b/src/common/__tests__/ActionButtonWithModal.test.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2024 CZ.NIC z.s.p.o. (https://www.nic.cz/) + * Copyright (C) 2019-2025 CZ.NIC z.s.p.o. (https://www.nic.cz/) * * This is free software, licensed under the GNU General Public License v3. * See /LICENSE for more information. @@ -9,13 +9,7 @@ import React from "react"; import Button from "bootstrap/Button"; -import { - fireEvent, - getByText, - queryByText, - render, - wait, -} from "customTestRender"; +import { fireEvent, getByText, render, waitFor } from "customTestRender"; import mockAxios from "jest-mock-axios"; import { mockJSONError } from "testUtils/network"; import { mockSetAlert } from "testUtils/alertContextMock"; @@ -73,7 +67,7 @@ describe("", () => { fireEvent.click(getByText(componentContainer, "Action")); fireEvent.click(getByText(componentContainer, "Confirm action")); mockJSONError(); - await wait(() => + await waitFor(() => expect(mockSetAlert).toBeCalledWith("Action request failed.") ); }); @@ -82,7 +76,7 @@ describe("", () => { fireEvent.click(getByText(componentContainer, "Action")); fireEvent.click(getByText(componentContainer, "Confirm action")); mockAxios.mockResponse({ status: 200 }); - await wait(() => + await waitFor(() => expect(mockSetAlert).toBeCalledWith( "Action request succeeded.", "success" diff --git a/src/context/customizationContext/__tests__/CustomizationContext.test.js b/src/context/customizationContext/__tests__/CustomizationContext.test.js index edba3ce..39f420f 100644 --- a/src/context/customizationContext/__tests__/CustomizationContext.test.js +++ b/src/context/customizationContext/__tests__/CustomizationContext.test.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2022 CZ.NIC z.s.p.o. (https://www.nic.cz/) + * Copyright (C) 2019-2025 CZ.NIC z.s.p.o. (https://www.nic.cz/) * * This is free software, licensed under the GNU General Public License v3. * See /LICENSE for more information. @@ -7,7 +7,7 @@ import React from "react"; -import { render, wait, getByText } from "customTestRender"; +import { render, waitFor, getByText } from "customTestRender"; import mockAxios from "jest-mock-axios"; import { @@ -38,7 +38,7 @@ describe("CustomizationContext", () => { it("should render component without customization", async () => { mockAxios.mockResponse({ data: {} }); - await wait(() => getByText(componentContainer, ORIGINAL)); + await waitFor(() => getByText(componentContainer, ORIGINAL)); expect(componentContainer).toMatchSnapshot(); }); @@ -46,7 +46,7 @@ describe("CustomizationContext", () => { it("should render customized component", async () => { mockAxios.mockResponse({ data: { customization: "shield" } }); - await wait(() => getByText(componentContainer, CUSTOM)); + await waitFor(() => getByText(componentContainer, CUSTOM)); expect(componentContainer).toMatchSnapshot(); }); diff --git a/src/form/__tests__/hooks.test.js b/src/form/__tests__/hooks.test.js index e57e466..c6a245f 100644 --- a/src/form/__tests__/hooks.test.js +++ b/src/form/__tests__/hooks.test.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/) + * Copyright (C) 2019-2025 CZ.NIC z.s.p.o. (https://www.nic.cz/) * * This is free software, licensed under the GNU General Public License v3. * See /LICENSE for more information. @@ -7,7 +7,7 @@ import React from "react"; -import { act, fireEvent, render, waitForElement } from "customTestRender"; +import { act, fireEvent, render, waitFor } from "customTestRender"; import mockAxios from "jest-mock-axios"; import WebSockets from "webSockets/WebSockets"; import ForisForm from "../components/ForisForm"; @@ -59,7 +59,7 @@ describe("useForm hook.", () => { ); mockAxios.mockResponse({ field: "fetchedData" }); - input = await waitForElement(() => getByTestId("test-input")); + input = await waitFor(() => getByTestId("test-input")); form = container.firstChild.firstChild; });