Skip to content

Commit

Permalink
add wait to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meetulr committed Feb 11, 2024
1 parent 97d2ea3 commit e80d34a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -27,6 +33,14 @@ jest.mock('react-toastify', () => ({
},
}));

async function wait(ms = 100): Promise<void> {
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);
Expand All @@ -52,6 +66,8 @@ describe('Testing Action Item Categories Component', () => {
</MockedProvider>
);

await wait();

await waitFor(() => {
expect(getByText(translations.createButton)).toBeInTheDocument();
});
Expand All @@ -71,6 +87,8 @@ describe('Testing Action Item Categories Component', () => {
</MockedProvider>
);

await wait();

await waitFor(() => {
expect(queryByText(translations.createButton)).not.toBeInTheDocument();
});
Expand All @@ -90,6 +108,8 @@ describe('Testing Action Item Categories Component', () => {
</MockedProvider>
);

await wait();

await waitFor(() => {
userEvent.click(screen.getByTestId('actionItemCategoryModalOpenBtn'));
userEvent.click(screen.getByTestId('actionItemCategoryModalCloseBtn'));
Expand Down Expand Up @@ -117,6 +137,8 @@ describe('Testing Action Item Categories Component', () => {
</MockedProvider>
);

await wait();

await waitFor(() => {
userEvent.click(screen.getByTestId('actionItemCategoryModalOpenBtn'));
userEvent.type(
Expand Down Expand Up @@ -146,6 +168,8 @@ describe('Testing Action Item Categories Component', () => {
</MockedProvider>
);

await wait();

await waitFor(() => {
userEvent.click(screen.getByTestId('actionItemCategoryModalOpenBtn'));
userEvent.type(
Expand Down Expand Up @@ -175,6 +199,8 @@ describe('Testing Action Item Categories Component', () => {
</MockedProvider>
);

await wait();

await waitFor(() => {
userEvent.click(
screen.getAllByTestId('actionItemCategoryUpdateModalOpenBtn')[0]
Expand Down Expand Up @@ -210,6 +236,8 @@ describe('Testing Action Item Categories Component', () => {
</MockedProvider>
);

await wait();

await waitFor(() => {
userEvent.click(
screen.getAllByTestId('actionItemCategoryUpdateModalOpenBtn')[0]
Expand Down Expand Up @@ -245,6 +273,8 @@ describe('Testing Action Item Categories Component', () => {
</MockedProvider>
);

await wait();

await waitFor(() => {
userEvent.click(
screen.getAllByTestId('actionItemCategoryUpdateModalOpenBtn')[0]
Expand Down Expand Up @@ -280,6 +310,8 @@ describe('Testing Action Item Categories Component', () => {
</MockedProvider>
);

await wait();

await waitFor(() => {
userEvent.click(screen.getAllByTestId('disabilityStatusButton')[0]);
});
Expand Down Expand Up @@ -311,6 +343,8 @@ describe('Testing Action Item Categories Component', () => {
</MockedProvider>
);

await wait();

await waitFor(() => {
userEvent.click(screen.getAllByTestId('disabilityStatusButton')[0]);
});
Expand Down
15 changes: 14 additions & 1 deletion src/screens/OrgSettings/OrgSettings.test.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -84,6 +84,14 @@ const MOCKS = [

const link = new StaticMockLink(MOCKS, true);

async function wait(ms = 100): Promise<void> {
await act(() => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
});
}

const translations = JSON.parse(
JSON.stringify(i18nForTest.getDataByLanguage('en')?.translation.orgSettings)
);
Expand Down Expand Up @@ -116,6 +124,9 @@ describe('Organisation Settings Page', () => {
</BrowserRouter>
</MockedProvider>
);

await wait();

expect(screen.getAllByText(/Delete Organization/i)).toHaveLength(3);
expect(
screen.getByText(
Expand Down Expand Up @@ -143,6 +154,8 @@ describe('Organisation Settings Page', () => {
</MockedProvider>
);

await wait();

await waitFor(() => {
userEvent.click(screen.getByTestId('actionItemCategoriesSettings'));
expect(
Expand Down

0 comments on commit e80d34a

Please sign in to comment.