Skip to content

Commit

Permalink
test(Metrics): ✅ Request and response unit test empty case
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoval committed Nov 7, 2023
1 parent 707872b commit 4c1c54e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/pages/Topology/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const shape = {

const platformsMap: Record<string, 'kubernetes' | 'podman'> = {
kubernetes: kubernetesIcon,
pdoman: podmanIcon
podman: podmanIcon
};

export const TopologyController = {
Expand Down
27 changes: 27 additions & 0 deletions src/pages/shared/Metrics/__tests__/Request.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Suspense } from 'react';
import { render, screen, waitForElementToBeRemoved } from '@testing-library/react';
import { Server } from 'miragejs';

import * as PrometheusAPIModule from '@API/Prometheus.api';
import { ProcessResponse } from '@API/REST.interfaces';
import { waitForElementToBeRemovedTimeout } from '@config/config';
import { getTestsIds } from '@config/testIds';
Expand Down Expand Up @@ -49,4 +50,30 @@ describe('Request component', () => {

expect(screen.getByText(MetricsLabels.RequestsTitle)).toBeInTheDocument();
});

it('should not render the Request section', async () => {
jest
.spyOn(PrometheusAPIModule.PrometheusApi, 'fetchRequestRateByMethodInInTimeRange')
.mockImplementation(jest.fn().mockReturnValue({ data: null }));

render(
<Wrapper>
<Suspense fallback={<LoadingPage />}>
<Request
selectedFilters={{
sourceProcess: processResult.name
}}
openSections={true}
forceUpdate={1}
/>
</Suspense>
</Wrapper>
);

await waitForElementToBeRemoved(() => screen.queryByTestId(getTestsIds.loadingView()), {
timeout: waitForElementToBeRemovedTimeout
});

expect(screen.queryByText(MetricsLabels.RequestsTitle)).not.toBeInTheDocument();
});
});
79 changes: 79 additions & 0 deletions src/pages/shared/Metrics/__tests__/Response.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Suspense } from 'react';

import { render, screen, waitForElementToBeRemoved } from '@testing-library/react';
import { Server } from 'miragejs';

import * as PrometheusAPIModule from '@API/Prometheus.api';
import { ProcessResponse } from '@API/REST.interfaces';
import { waitForElementToBeRemovedTimeout } from '@config/config';
import { getTestsIds } from '@config/testIds';
import { Wrapper } from '@core/components/Wrapper';
import processesData from '@mocks/data/PROCESSES.json';
import { loadMockServer } from '@mocks/server';
import LoadingPage from '@pages/shared/Loading';

import Response from '../components/Response';
import { MetricsLabels } from '../Metrics.enum';

const processResult = processesData.results[0] as ProcessResponse;

describe('Response component', () => {
let server: Server;
beforeEach(() => {
server = loadMockServer() as Server;
server.logging = false;
});

afterEach(() => {
server.shutdown();
jest.clearAllMocks();
});

it('should render the Reponse section of the metric', async () => {
render(
<Wrapper>
<Suspense fallback={<LoadingPage />}>
<Response
selectedFilters={{
sourceProcess: processResult.name
}}
openSections={true}
forceUpdate={1}
/>
</Suspense>
</Wrapper>
);

await waitForElementToBeRemoved(() => screen.queryByTestId(getTestsIds.loadingView()), {
timeout: waitForElementToBeRemovedTimeout
});

expect(screen.getByText(MetricsLabels.ResposeTitle)).toBeInTheDocument();
});

it('should not render the Response section', async () => {
jest
.spyOn(PrometheusAPIModule.PrometheusApi, 'fetchResponseCountsByPartialCodeInTimeRange')
.mockImplementation(jest.fn().mockReturnValue({ data: null }));

render(
<Wrapper>
<Suspense fallback={<LoadingPage />}>
<Response
selectedFilters={{
sourceProcess: processResult.name
}}
openSections={true}
forceUpdate={1}
/>
</Suspense>
</Wrapper>
);

await waitForElementToBeRemoved(() => screen.queryByTestId(getTestsIds.loadingView()), {
timeout: waitForElementToBeRemovedTimeout
});

expect(screen.queryByText(MetricsLabels.ResposeTitle)).not.toBeInTheDocument();
});
});

0 comments on commit 4c1c54e

Please sign in to comment.