Skip to content

Commit

Permalink
Fix hanging test
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Baldwin committed Jan 19, 2024
1 parent f5e235d commit 7150aae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
- run: python -m pip install tox
- run: python -m tox -e ${{ matrix.tox }}

lint_typecheck_webui:
lint_typecheck_test_webui:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { act, fireEvent, waitFor } from '@testing-library/react';
import { act, fireEvent } from '@testing-library/react';
import { http } from 'msw';
import { setupServer } from 'msw/node';
import { afterEach, afterAll, beforeAll, describe, test, expect, vi } from 'vitest';
Expand Down Expand Up @@ -47,7 +47,7 @@ const defaultProps = {
setSwarm: updateSwarm,
};

describe('SwarmForm', () => {
describe('SwarmUserClassPicker', () => {
beforeAll(() => server.listen());
afterEach(() => {
server.resetHandlers();
Expand Down Expand Up @@ -95,6 +95,8 @@ describe('SwarmForm', () => {
},
);

vi.useFakeTimers();

act(() => {
// Open modal
fireEvent.click(getAllByRole('button')[0]);
Expand All @@ -104,12 +106,15 @@ describe('SwarmForm', () => {
fireEvent.click(getByRole('button', { name: 'Save' }));
});

await waitFor(async () => {
const submittedData = updateUserSettings.mock.calls[0][0];
if (submittedData) {
expect(submittedData).toEqual({ ...mockUsers.Example, userClassName: 'Example' });
}
await act(async () => {
await vi.runAllTimersAsync();
});

expect(updateUserSettings).toHaveBeenCalled();

const submittedData = updateUserSettings.mock.calls[0][0];

expect(submittedData).toEqual({ ...mockUsers.Example, userClassName: 'Example' });
});

test('should allow for configuring tasks, host, fixed_count, and weight to be modified', async () => {
Expand All @@ -130,6 +135,8 @@ describe('SwarmForm', () => {
},
);

vi.useFakeTimers();

act(() => {
// Open modal
fireEvent.click(getAllByRole('button')[0]);
Expand All @@ -152,12 +159,15 @@ describe('SwarmForm', () => {
fireEvent.click(getByRole('button', { name: 'Save' }));
});

await waitFor(async () => {
const submittedData = updateUserSettings.mock.calls[0][0];
if (submittedData) {
expect(submittedData).toEqual({ ...updatedUser, userClassName: 'Example' });
expect(store.getState().swarm.users.Example).toEqual(updatedUser);
}
await act(async () => {
await vi.runAllTimersAsync();
});

expect(updateUserSettings).toHaveBeenCalled();

const submittedData = updateUserSettings.mock.calls[0][0];

expect(submittedData).toEqual({ ...updatedUser, userClassName: 'Example' });
expect(store.getState().swarm.users.Example).toEqual(updatedUser);
});
});
6 changes: 5 additions & 1 deletion locust/webui/src/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cleanup } from '@testing-library/react';
import { afterEach, vi } from 'vitest';
import { afterAll, afterEach, vi } from 'vitest';

import { TEST_BASE_API } from 'test/constants';
import { swarmStateMock } from 'test/mocks/swarmState.mock';
Expand All @@ -25,3 +25,7 @@ vi.mock('echarts', async () => {

return { ...actual, init: (...args: any[]) => actual.init(...args, { width: 100, height: 100 }) };
});

afterAll(() => {
vi.clearAllMocks();
});

0 comments on commit 7150aae

Please sign in to comment.