Replace mockXcmetricsApi with jest manual mock

Signed-off-by: Niklas Granander <ngranander@spotify.com>
This commit is contained in:
Niklas Granander
2021-07-28 12:29:54 +02:00
parent bb2fb16e6f
commit c75c6a3a19
7 changed files with 108 additions and 100 deletions
@@ -0,0 +1,73 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Build, XcmetricsApi } from '../types';
export const mockBuild = {
userid: 'userid1',
warningCount: 1,
duration: 1,
startTimestamp: '2021-01-01T00:00:00Z',
isCi: true,
startTimestampMicroseconds: 0,
category: '',
endTimestampMicroseconds: 10000,
day: '2021-01-01',
compilationEndTimestamp: '2021-01-01T00:00:01Z',
tag: '',
projectName: 'Project',
compilationEndTimestampMicroseconds: 1,
errorCount: 1,
id: 'buildId',
buildStatus: 'succeeded',
compilationDuration: 1,
schema: 'Schema',
compiledCount: 1,
endTimestamp: '2021-01-01T00:00:01Z',
userid256: 'userId256',
machineName: 'Example_Machine',
wasSuspended: true,
} as Build;
export const mockBuildCount = { day: '2021-07-10', builds: 10, errors: 1 };
export const mockBuildTime = {
day: '2021-07-10',
durationP50: 1.1,
durationP95: 2.1,
totalDuration: 3.1,
};
export const mockBuildStatus = { id: 'build_id', status: 'succeeded' };
export const XcmetricsClient: XcmetricsApi = {
getBuild: (id: string) => {
return Promise.resolve({ ...mockBuild, id });
},
getBuilds: () => {
return Promise.resolve([
{ ...mockBuild, id: '1' },
{ ...mockBuild, id: '2', userid: 'userid2' },
]);
},
getBuildCounts: () => {
return Promise.resolve([mockBuildCount, mockBuildCount]);
},
getBuildStatuses: (limit: number) => {
return Promise.resolve([mockBuild].slice(0, limit));
},
getBuildTimes: (days: number) => {
return Promise.resolve([mockBuildTime, mockBuildTime].slice(0, days));
},
};
@@ -17,9 +17,11 @@ import React from 'react';
import { renderInTestApp } from '@backstage/test-utils';
import { xcmetricsApiRef } from '../../api';
import { ApiProvider, ApiRegistry } from '@backstage/core-app-api';
import { mockUserId, createMockXcmetricsApi } from '../../test-utils';
import { OverviewComponent } from './OverviewComponent';
jest.mock('../../api/XcmetricsClient');
const client = require('../../api/XcmetricsClient');
jest.mock('../OverviewTrendsComponent', () => ({
OverviewTrendsComponent: () => 'OverviewTrendsComponent',
}));
@@ -32,18 +34,18 @@ describe('OverviewComponent', () => {
it('should render', async () => {
const rendered = await renderInTestApp(
<ApiProvider
apis={ApiRegistry.with(xcmetricsApiRef, createMockXcmetricsApi())}
apis={ApiRegistry.with(xcmetricsApiRef, client.XcmetricsClient)}
>
<OverviewComponent />
</ApiProvider>,
);
expect(rendered.getByText('XCMetrics Dashboard')).toBeInTheDocument();
expect(rendered.getByText(mockUserId)).toBeInTheDocument();
expect(rendered.getByText(client.mockBuild.userid)).toBeInTheDocument();
});
it('should render an empty state when no builds exist', async () => {
const api = createMockXcmetricsApi();
const api = client.XcmetricsClient;
api.getBuilds = jest.fn().mockResolvedValue([]);
const rendered = await renderInTestApp(
@@ -56,7 +58,7 @@ describe('OverviewComponent', () => {
});
it('should show an error when API not responding', async () => {
const api = createMockXcmetricsApi();
const api = client.XcmetricsClient;
const errorMessage = 'MockErrorMessage';
api.getBuilds = jest.fn().mockRejectedValue({ message: errorMessage });
@@ -18,14 +18,16 @@ import { OverviewTrendsComponent } from './OverviewTrendsComponent';
import { renderInTestApp } from '@backstage/test-utils';
import { xcmetricsApiRef } from '../../api';
import { ApiProvider, ApiRegistry } from '@backstage/core-app-api';
import { createMockXcmetricsApi } from '../../test-utils';
import userEvent from '@testing-library/user-event';
jest.mock('../../api/XcmetricsClient');
const client = require('../../api/XcmetricsClient');
describe('OverviewTrendsComponent', () => {
it('should render', async () => {
const rendered = await renderInTestApp(
<ApiProvider
apis={ApiRegistry.with(xcmetricsApiRef, createMockXcmetricsApi())}
apis={ApiRegistry.with(xcmetricsApiRef, client.XcmetricsClient)}
>
<OverviewTrendsComponent />
</ApiProvider>,
@@ -36,7 +38,7 @@ describe('OverviewTrendsComponent', () => {
});
it('should render empty state', async () => {
const api = createMockXcmetricsApi();
const api = client.XcmetricsClient;
api.getBuildCounts = jest.fn().mockResolvedValue([]);
const rendered = await renderInTestApp(
@@ -50,7 +52,7 @@ describe('OverviewTrendsComponent', () => {
it('should change number of days when select is changed', async () => {
const rendered = await renderInTestApp(
<ApiProvider
apis={ApiRegistry.with(xcmetricsApiRef, createMockXcmetricsApi())}
apis={ApiRegistry.with(xcmetricsApiRef, client.XcmetricsClient)}
>
<OverviewTrendsComponent />
</ApiProvider>,
@@ -62,7 +64,7 @@ describe('OverviewTrendsComponent', () => {
});
it('should show errors when API not responding', async () => {
const api = createMockXcmetricsApi();
const api = client.XcmetricsClient;
const buildCountError = 'MockBuildCountErrorMessage';
const buildTimesError = 'MockBuildTimesErrorMessage';
@@ -19,30 +19,36 @@ import { ApiProvider, ApiRegistry } from '@backstage/core-app-api';
import userEvent from '@testing-library/user-event';
import { StatusCellComponent } from './StatusCellComponent';
import { xcmetricsApiRef } from '../../api';
import {
mockBuildId,
mockStatus,
createMockXcmetricsApi,
} from '../../test-utils';
import { formatStatus } from '../../utils';
import { formatDuration, formatStatus } from '../../utils';
jest.mock('../../api/XcmetricsClient');
const client = require('../../api/XcmetricsClient');
describe('StatusCellComponent', () => {
it('should render', async () => {
const rendered = await renderInTestApp(
<ApiProvider
apis={ApiRegistry.with(xcmetricsApiRef, createMockXcmetricsApi())}
apis={ApiRegistry.with(xcmetricsApiRef, client.XcmetricsClient)}
>
<StatusCellComponent
buildStatus={{ id: mockBuildId, buildStatus: mockStatus }}
buildStatus={{
id: client.mockBuild.id,
buildStatus: client.mockBuild.buildStatus,
}}
size={10}
spacing={10}
/>
</ApiProvider>,
);
userEvent.hover(rendered.getByTestId(mockBuildId));
userEvent.hover(rendered.getByTestId(client.mockBuild.id));
expect(
await rendered.findByText(formatStatus(mockStatus)),
await rendered.findByText(formatStatus(client.mockBuild.buildStatus)),
).toBeInTheDocument();
expect(
await rendered.findByText(
formatDuration(client.mockBuild.duration).trim(),
),
).toBeInTheDocument();
});
});
@@ -18,19 +18,21 @@ import { renderInTestApp } from '@backstage/test-utils';
import { ApiProvider, ApiRegistry } from '@backstage/core-app-api';
import { StatusMatrixComponent } from './StatusMatrixComponent';
import { xcmetricsApiRef } from '../../api';
import { mockBuildId, createMockXcmetricsApi } from '../../test-utils';
jest.mock('../../api/XcmetricsClient');
const client = require('../../api/XcmetricsClient');
describe('StatusMatrixComponent', () => {
it('should render', async () => {
const rendered = await renderInTestApp(
<ApiProvider
apis={ApiRegistry.with(xcmetricsApiRef, createMockXcmetricsApi())}
apis={ApiRegistry.with(xcmetricsApiRef, client.XcmetricsClient)}
>
<StatusMatrixComponent />
</ApiProvider>,
);
const cell = rendered.getByTestId(mockBuildId);
const cell = rendered.getByTestId(client.mockBuild.id);
expect(cell).toBeInTheDocument();
});
});
-16
View File
@@ -1,16 +0,0 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './mockXcmetricsApi';
@@ -1,61 +0,0 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { XcmetricsApi } from '../api';
export const mockUserId = 'user_id';
export const mockBuildId = 'build_id';
export const mockStatus = 'succeeded';
export const createMockXcmetricsApi = (): jest.Mocked<XcmetricsApi> => ({
getBuildStatuses: jest
.fn()
.mockResolvedValue([{ id: mockBuildId, status: mockStatus }]),
getBuild: jest.fn().mockResolvedValue({
id: mockBuildId,
buildStatus: 'succeeded',
duration: 10.0,
startTimestamp: '1626365026',
}),
getBuilds: jest.fn().mockResolvedValue([
{
userid: mockUserId,
warningCount: 1,
duration: 123.45,
isCi: false,
projectName: 'App',
buildStatus: mockStatus,
schema: 'AppSchema',
},
]),
getBuildCounts: jest.fn().mockResolvedValue([
{ day: '2021-07-10', builds: 10, errors: 1 },
{ day: '2021-07-09', builds: 11, errors: 2 },
]),
getBuildTimes: jest.fn().mockResolvedValue([
{
day: '2021-07-10',
durationP50: 1.1,
durationP95: 2.1,
totalDuration: 3.1,
},
{
day: '2021-07-09',
durationP50: 1.2,
durationP95: 2.2,
totalDuration: 3.2,
},
]),
});