From e80d34a250004966e9512b51694ec3e32b8bf9d8 Mon Sep 17 00:00:00 2001 From: meetul Date: Mon, 12 Feb 2024 00:02:59 +0530 Subject: [PATCH] add wait to tests --- .../OrgActionItemCategories.test.tsx | 36 ++++++++++++++++++- src/screens/OrgSettings/OrgSettings.test.tsx | 15 +++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/components/OrgActionItemCategories/OrgActionItemCategories.test.tsx b/src/components/OrgActionItemCategories/OrgActionItemCategories.test.tsx index d1daff0aea..f1ab510369 100644 --- a/src/components/OrgActionItemCategories/OrgActionItemCategories.test.tsx +++ b/src/components/OrgActionItemCategories/OrgActionItemCategories.test.tsx @@ -1,5 +1,11 @@ import React from 'react'; -import { render, screen, fireEvent, waitFor } from '@testing-library/react'; +import { + render, + screen, + fireEvent, + waitFor, + act, +} from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import 'jest-localstorage-mock'; import { MockedProvider } from '@apollo/client/testing'; @@ -27,6 +33,14 @@ jest.mock('react-toastify', () => ({ }, })); +async function wait(ms = 100): Promise { + await act(() => { + return new Promise((resolve) => { + setTimeout(resolve, ms); + }); + }); +} + const link = new StaticMockLink(MOCKS, true); const link2 = new StaticMockLink(MOCKS_ERROR_QUERY, true); const link3 = new StaticMockLink(MOCKS_ERROR_MUTATIONS, true); @@ -52,6 +66,8 @@ describe('Testing Action Item Categories Component', () => { ); + await wait(); + await waitFor(() => { expect(getByText(translations.createButton)).toBeInTheDocument(); }); @@ -71,6 +87,8 @@ describe('Testing Action Item Categories Component', () => { ); + await wait(); + await waitFor(() => { expect(queryByText(translations.createButton)).not.toBeInTheDocument(); }); @@ -90,6 +108,8 @@ describe('Testing Action Item Categories Component', () => { ); + await wait(); + await waitFor(() => { userEvent.click(screen.getByTestId('actionItemCategoryModalOpenBtn')); userEvent.click(screen.getByTestId('actionItemCategoryModalCloseBtn')); @@ -117,6 +137,8 @@ describe('Testing Action Item Categories Component', () => { ); + await wait(); + await waitFor(() => { userEvent.click(screen.getByTestId('actionItemCategoryModalOpenBtn')); userEvent.type( @@ -146,6 +168,8 @@ describe('Testing Action Item Categories Component', () => { ); + await wait(); + await waitFor(() => { userEvent.click(screen.getByTestId('actionItemCategoryModalOpenBtn')); userEvent.type( @@ -175,6 +199,8 @@ describe('Testing Action Item Categories Component', () => { ); + await wait(); + await waitFor(() => { userEvent.click( screen.getAllByTestId('actionItemCategoryUpdateModalOpenBtn')[0] @@ -210,6 +236,8 @@ describe('Testing Action Item Categories Component', () => { ); + await wait(); + await waitFor(() => { userEvent.click( screen.getAllByTestId('actionItemCategoryUpdateModalOpenBtn')[0] @@ -245,6 +273,8 @@ describe('Testing Action Item Categories Component', () => { ); + await wait(); + await waitFor(() => { userEvent.click( screen.getAllByTestId('actionItemCategoryUpdateModalOpenBtn')[0] @@ -280,6 +310,8 @@ describe('Testing Action Item Categories Component', () => { ); + await wait(); + await waitFor(() => { userEvent.click(screen.getAllByTestId('disabilityStatusButton')[0]); }); @@ -311,6 +343,8 @@ describe('Testing Action Item Categories Component', () => { ); + await wait(); + await waitFor(() => { userEvent.click(screen.getAllByTestId('disabilityStatusButton')[0]); }); diff --git a/src/screens/OrgSettings/OrgSettings.test.tsx b/src/screens/OrgSettings/OrgSettings.test.tsx index 144bc16709..245e7dfe17 100644 --- a/src/screens/OrgSettings/OrgSettings.test.tsx +++ b/src/screens/OrgSettings/OrgSettings.test.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { MockedProvider } from '@apollo/react-testing'; -import { render, screen, waitFor } from '@testing-library/react'; +import { act, render, screen, waitFor } from '@testing-library/react'; import 'jest-location-mock'; import { I18nextProvider } from 'react-i18next'; import { Provider } from 'react-redux'; @@ -84,6 +84,14 @@ const MOCKS = [ const link = new StaticMockLink(MOCKS, true); +async function wait(ms = 100): Promise { + await act(() => { + return new Promise((resolve) => { + setTimeout(resolve, ms); + }); + }); +} + const translations = JSON.parse( JSON.stringify(i18nForTest.getDataByLanguage('en')?.translation.orgSettings) ); @@ -116,6 +124,9 @@ describe('Organisation Settings Page', () => { ); + + await wait(); + expect(screen.getAllByText(/Delete Organization/i)).toHaveLength(3); expect( screen.getByText( @@ -143,6 +154,8 @@ describe('Organisation Settings Page', () => { ); + await wait(); + await waitFor(() => { userEvent.click(screen.getByTestId('actionItemCategoriesSettings')); expect(