Skip to content

Commit

Permalink
Unit test case for useQueryRangeResourceData
Browse files Browse the repository at this point in the history
  • Loading branch information
uidoyen committed Apr 8, 2024
1 parent fee2688 commit 003d36d
Showing 1 changed file with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { TimeframeTitle } from '~/pages/modelServing/screens/types';
import useQueryRangeResourceData from '~/api/prometheus/useQueryRangeResourceData';
import { TimeframeStep, TimeframeTimeRange } from '~/pages/modelServing/screens/const';
import { testHook } from '~/__tests__/unit/testUtils/hooks';
import * as usePrometheusQueryRangeModule from '~/api/prometheus/usePrometheusQueryRange';
import { mockPrometheusQueryResponse } from '~/__mocks__/mockPrometheusQueryResponse';

describe('useQueryRangeResourceData', () => {
const active = true;
const query = 'testQuery';
const end = 123456;
const timeframe: TimeframeTitle = TimeframeTitle.ONE_HOUR;
const responsePredicate = jest.fn();
const namespace = 'testNamespace';

beforeEach(() => {
jest.clearAllMocks();
});

it('should call usePrometheusQueryRange with correct arguments and restructure the returned data', async () => {
const spy = jest.spyOn(usePrometheusQueryRangeModule, 'default');
const mockedResponse = { data: { result: mockPrometheusQueryResponse({}) } };

spy.mockReturnValue([
mockedResponse.data.result.data.result,
true,
undefined,
expect.any(Function),
false,
]);

const renderResult = testHook(useQueryRangeResourceData)(
active,
query,
end,
timeframe,
responsePredicate,
namespace,
);

expect(renderResult).hookToStrictEqual({
data: mockedResponse.data.result.data.result,
loaded: true,
error: undefined,
refresh: expect.any(Function),
pending: false,
});

expect(renderResult).hookToHaveUpdateCount(1);

expect(spy).toHaveBeenCalledWith(
active,
'/api/prometheus/serving',
query,
TimeframeTimeRange[timeframe],
end,
TimeframeStep[timeframe],
responsePredicate,
namespace,
);

// Restore the spy
spy.mockRestore();
});
});

0 comments on commit 003d36d

Please sign in to comment.