Skip to content

Commit

Permalink
Fix useSwarmUi test with mock timers
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbaldwin44 committed Dec 12, 2023
1 parent ef3e3db commit 7703df4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions locust/webui/src/components/LineChart/LineChart.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, test, expect } from 'vitest';

import LineChart from 'components/LineChart/LineChart';
import { getStatsResponseTransformed } from 'test/mocks/statsRequest.mock';
import { statsResponseTransformed } from 'test/mocks/statsRequest.mock';
import { renderWithProvider } from 'test/testUtils';
import { ICharts } from 'types/ui.types';

Expand All @@ -18,7 +18,7 @@ describe('LineChart', () => {
test('renders LineChart with charts and lines', () => {
const { container } = renderWithProvider(
<LineChart
charts={getStatsResponseTransformed().charts}
charts={statsResponseTransformed.charts}
colors={mockChart.colors}
lines={mockChart.lines}
title={mockChart.title}
Expand Down
17 changes: 13 additions & 4 deletions locust/webui/src/hooks/tests/useSwarmUi.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { swarmActions } from 'redux/slice/swarm.slice';
import { TEST_BASE_API } from 'test/constants';
import {
ratiosResponseMock,
getStatsResponseTransformed,
statsResponseTransformed,
statsResponseMock,
exceptionsResponseMock,
mockDate,
} from 'test/mocks/statsRequest.mock';
import { swarmStateMock } from 'test/mocks/swarmState.mock';
import { renderWithProvider } from 'test/testUtils';
Expand All @@ -35,10 +36,18 @@ describe('useSwarmUi', () => {
afterAll(() => server.close());

test('should fetch request stats, ratios, and exceptions and update UI accordingly', async () => {
const { store } = renderWithProvider(<MockHook />);
act(async () => {
vi.useFakeTimers();

await waitFor(() => {
expect(store.getState().ui).toEqual(getStatsResponseTransformed());
vi.setSystemTime(mockDate);

const { store } = renderWithProvider(<MockHook />);

await vi.runAllTimersAsync();

expect(store.getState().ui).toEqual(statsResponseTransformed);

vi.useRealTimers();
});
});

Expand Down
8 changes: 5 additions & 3 deletions locust/webui/src/test/mocks/statsRequest.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ export const exceptionsResponseMock = {
],
};

export const getStatsResponseTransformed = () => ({
export const mockDate = new Date(1970, 1);

export const statsResponseTransformed = {
totalRps: 1932.5,
failRatio: 100,
stats: [
Expand Down Expand Up @@ -151,7 +153,7 @@ export const getStatsResponseTransformed = () => ({
currentFailPerSec: [1932.5],
userCount: [1],
totalAvgResponseTime: [0.41],
time: [new Date().toLocaleTimeString()],
time: [mockDate.toLocaleTimeString()],
},
ratios: {
perClass: {
Expand All @@ -169,4 +171,4 @@ export const getStatsResponseTransformed = () => ({
},
userCount: 1,
workers: undefined,
});
};

0 comments on commit 7703df4

Please sign in to comment.