Skip to content

Commit

Permalink
Fix flacky UserPage test (#2914)
Browse files Browse the repository at this point in the history
* Add waitFor to fix flaky tests
  • Loading branch information
EMaksy authored Aug 21, 2024
1 parent ca62626 commit 69264d2
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions assets/js/pages/Users/UsersPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import MockAdapter from 'axios-mock-adapter';
import { toast } from 'react-hot-toast';

import { networkClient } from '@lib/network';
import { screen, act } from '@testing-library/react';
import { screen, act, waitFor } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import { adminUser, userFactory } from '@lib/test-utils/factories/users';
import { renderWithRouter } from '@lib/test-utils';
Expand Down Expand Up @@ -59,7 +59,9 @@ describe('UsersPage', () => {
await act(async () => {
renderWithRouter(<UsersPage />);
});
expect(toast.error).toHaveBeenCalledWith(fetchErrorMessage);
await waitFor(() => {
expect(toast.error).toHaveBeenCalledWith(fetchErrorMessage);
});
});

it('should render toast with success message when deleting was successfully', async () => {
Expand All @@ -86,7 +88,9 @@ describe('UsersPage', () => {

expect(axiosMock.history.delete.length).toBe(1);
expect(axiosMock.history.delete[0].url).toBe(`/users/${user.id}`);
expect(toast.success).toHaveBeenCalledWith(deleteMessage);
await waitFor(() => {
expect(toast.success).toHaveBeenCalledWith(deleteMessage);
});
});

it('should render toast with error message when deleting failed', async () => {
Expand All @@ -107,6 +111,8 @@ describe('UsersPage', () => {

expect(axiosMock.history.delete.length).toBe(1);
expect(axiosMock.history.delete[0].url).toBe(`/users/${user.id}`);
expect(toast.error).toHaveBeenCalledWith(userNotFoundMessage);
await waitFor(() => {
expect(toast.error).toHaveBeenCalledWith(userNotFoundMessage);
});
});
});

0 comments on commit 69264d2

Please sign in to comment.