diff --git a/plugins/xcmetrics/src/api/__mocks__/XcmetricsClient.ts b/plugins/xcmetrics/src/api/__mocks__/XcmetricsClient.ts new file mode 100644 index 0000000000..5d53f41860 --- /dev/null +++ b/plugins/xcmetrics/src/api/__mocks__/XcmetricsClient.ts @@ -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)); + }, +}; diff --git a/plugins/xcmetrics/src/components/OverviewComponent/OverviewComponent.test.tsx b/plugins/xcmetrics/src/components/OverviewComponent/OverviewComponent.test.tsx index e5689869c1..fb5ef42cd8 100644 --- a/plugins/xcmetrics/src/components/OverviewComponent/OverviewComponent.test.tsx +++ b/plugins/xcmetrics/src/components/OverviewComponent/OverviewComponent.test.tsx @@ -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( , ); 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 }); diff --git a/plugins/xcmetrics/src/components/OverviewTrendsComponent/OverviewTrendsComponent.test.tsx b/plugins/xcmetrics/src/components/OverviewTrendsComponent/OverviewTrendsComponent.test.tsx index ddde3952b3..cb33be1451 100644 --- a/plugins/xcmetrics/src/components/OverviewTrendsComponent/OverviewTrendsComponent.test.tsx +++ b/plugins/xcmetrics/src/components/OverviewTrendsComponent/OverviewTrendsComponent.test.tsx @@ -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( , @@ -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( , @@ -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'; diff --git a/plugins/xcmetrics/src/components/StatusCellComponent/StatusCellComponent.test.tsx b/plugins/xcmetrics/src/components/StatusCellComponent/StatusCellComponent.test.tsx index 363486eb51..6ee832ceb8 100644 --- a/plugins/xcmetrics/src/components/StatusCellComponent/StatusCellComponent.test.tsx +++ b/plugins/xcmetrics/src/components/StatusCellComponent/StatusCellComponent.test.tsx @@ -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( , ); - 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(); }); }); diff --git a/plugins/xcmetrics/src/components/StatusMatrixComponent/StatusMatrixComponent.test.tsx b/plugins/xcmetrics/src/components/StatusMatrixComponent/StatusMatrixComponent.test.tsx index 8951f36b8f..aac029a919 100644 --- a/plugins/xcmetrics/src/components/StatusMatrixComponent/StatusMatrixComponent.test.tsx +++ b/plugins/xcmetrics/src/components/StatusMatrixComponent/StatusMatrixComponent.test.tsx @@ -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( , ); - const cell = rendered.getByTestId(mockBuildId); + const cell = rendered.getByTestId(client.mockBuild.id); expect(cell).toBeInTheDocument(); }); }); diff --git a/plugins/xcmetrics/src/test-utils/index.ts b/plugins/xcmetrics/src/test-utils/index.ts deleted file mode 100644 index 55e29260d1..0000000000 --- a/plugins/xcmetrics/src/test-utils/index.ts +++ /dev/null @@ -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'; diff --git a/plugins/xcmetrics/src/test-utils/mockXcmetricsApi.ts b/plugins/xcmetrics/src/test-utils/mockXcmetricsApi.ts deleted file mode 100644 index 08e75f2653..0000000000 --- a/plugins/xcmetrics/src/test-utils/mockXcmetricsApi.ts +++ /dev/null @@ -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 => ({ - 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, - }, - ]), -});