Added tests

Signed-off-by: Robert Bunning <rbunning@webstaurantstore.com>
This commit is contained in:
Robert Bunning
2022-12-20 14:58:57 -05:00
parent e4469d0ec1
commit fd19425ebe
5 changed files with 482 additions and 0 deletions
+4
View File
@@ -46,9 +46,13 @@
"react-router-dom": "6.0.0-beta.0 || ^6.3.0"
},
"devDependencies": {
"@backstage/catalog-client": "workspace:^",
"@backstage/cli": "workspace:^",
"@backstage/core-app-api": "workspace:^",
"@backstage/dev-utils": "workspace:^",
"@backstage/integration": "workspace:^",
"@backstage/plugin-catalog": "workspace:^",
"@backstage/plugin-permission-react": "workspace:^",
"@backstage/test-utils": "workspace:^",
"@testing-library/jest-dom": "^5.10.1",
"@testing-library/react": "^12.1.3",
@@ -0,0 +1,129 @@
/*
* Copyright 2022 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 React from 'react';
import { EntityLayout } from '@backstage/plugin-catalog';
import { Entity, ANNOTATION_SOURCE_LOCATION } from '@backstage/catalog-model';
import { ApiProvider } from '@backstage/core-app-api';
import { CatalogApi } from '@backstage/catalog-client';
import {
EntityProvider,
catalogApiRef,
starredEntitiesApiRef,
MockStarredEntitiesApi,
} from '@backstage/plugin-catalog-react';
import { scmIntegrationsApiRef } from '@backstage/integration-react';
import { permissionApiRef } from '@backstage/plugin-permission-react';
import {
renderInTestApp,
TestApiRegistry,
MockPermissionApi,
} from '@backstage/test-utils';
import { AdrReader } from './AdrReader';
import { ScmIntegrationRegistry } from '@backstage/integration';
import { ANNOTATION_ADR_LOCATION } from '@backstage/plugin-adr-common';
import {
octokitAdrFileFetcher,
urlReaderAdrFileFetcher,
} from '../../hooks/adrFileFetcher';
const mockApis = TestApiRegistry.from(
[catalogApiRef, {} as CatalogApi],
[starredEntitiesApiRef, new MockStarredEntitiesApi()],
[permissionApiRef, new MockPermissionApi()],
[
scmIntegrationsApiRef,
{
resolveUrl: options => `${options.url}`,
} as ScmIntegrationRegistry,
],
);
const mockEntity: Entity = {
kind: 'TestEntity',
metadata: {
name: 'Testing Entity 1',
annotations: {
[ANNOTATION_ADR_LOCATION]: 'testAdrFolder',
[ANNOTATION_SOURCE_LOCATION]: 'source:location',
},
},
apiVersion: '',
};
afterEach(() => {
jest.resetAllMocks();
});
describe('AdrReader', () => {
it('Falls back to octokitAdrFileFetcher when adrFileFetcher is not specified', async () => {
const spyInstance = jest
.spyOn(octokitAdrFileFetcher, 'useReadAdrFileAtUrl')
.mockImplementation(() => {
return { data: '' };
});
await renderInTestApp(
<ApiProvider apis={mockApis}>
<EntityProvider entity={mockEntity}>
<EntityLayout>
<EntityLayout.Route path="/" title="tabbed-test-title">
<AdrReader adr="testadr.md" />
</EntityLayout.Route>
</EntityLayout>
</EntityProvider>
</ApiProvider>,
);
expect(spyInstance).toHaveBeenCalled();
});
it('Uses an alternative AdrFileFetcher when provided', async () => {
const octokitSpyInstance = jest
.spyOn(octokitAdrFileFetcher, 'useReadAdrFileAtUrl')
.mockImplementation(() => {
return {
data: '',
};
});
const urlReadersSpyInstance = jest
.spyOn(urlReaderAdrFileFetcher, 'useReadAdrFileAtUrl')
.mockImplementation(() => {
return {
data: '',
};
});
await renderInTestApp(
<ApiProvider apis={mockApis}>
<EntityProvider entity={mockEntity}>
<EntityLayout>
<EntityLayout.Route path="/" title="tabbed-test-title">
<AdrReader
adr="testadr.md"
adrFileFetcher={urlReaderAdrFileFetcher}
/>
</EntityLayout.Route>
</EntityLayout>
</EntityProvider>
</ApiProvider>,
);
expect(octokitSpyInstance).not.toHaveBeenCalled();
expect(urlReadersSpyInstance).toHaveBeenCalled();
});
});
@@ -0,0 +1,139 @@
/*
* Copyright 2022 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 React from 'react';
import { EntityLayout } from '@backstage/plugin-catalog';
import { Entity, ANNOTATION_SOURCE_LOCATION } from '@backstage/catalog-model';
import { ApiProvider } from '@backstage/core-app-api';
import { CatalogApi } from '@backstage/catalog-client';
import {
EntityProvider,
catalogApiRef,
starredEntitiesApiRef,
MockStarredEntitiesApi,
} from '@backstage/plugin-catalog-react';
import { scmIntegrationsApiRef } from '@backstage/integration-react';
import { permissionApiRef } from '@backstage/plugin-permission-react';
import {
renderInTestApp,
TestApiRegistry,
MockPermissionApi,
} from '@backstage/test-utils';
import { ScmIntegrationRegistry } from '@backstage/integration';
import { ANNOTATION_ADR_LOCATION } from '@backstage/plugin-adr-common';
import {
octokitAdrFileFetcher,
urlReaderAdrFileFetcher,
} from '../../hooks/adrFileFetcher';
import { EntityAdrContent } from './EntityAdrContent';
import { rootRouteRef } from '../../routes';
const mockApis = TestApiRegistry.from(
[catalogApiRef, {} as CatalogApi],
[starredEntitiesApiRef, new MockStarredEntitiesApi()],
[permissionApiRef, new MockPermissionApi()],
[
scmIntegrationsApiRef,
{
resolveUrl: options => `${options.url}`,
} as ScmIntegrationRegistry,
],
);
const mockEntity: Entity = {
kind: 'TestEntity',
metadata: {
name: 'Testing Entity 1',
annotations: {
[ANNOTATION_ADR_LOCATION]: 'testAdrFolder',
[ANNOTATION_SOURCE_LOCATION]: 'source:location',
},
},
apiVersion: '',
};
afterEach(() => {
jest.resetAllMocks();
});
describe('EntityAdrContent', () => {
it('Falls back to octokitAdrFileFetcher when adrFileFetcher is not specified', async () => {
const getAdrFilesSpyInstance = jest
.spyOn(octokitAdrFileFetcher, 'useGetAdrFilesAtUrl')
.mockImplementation(() => {
return {
data: [],
};
});
await renderInTestApp(
<ApiProvider apis={mockApis}>
<EntityProvider entity={mockEntity}>
<EntityLayout>
<EntityLayout.Route path="/" title="tabbed-test-title">
<EntityAdrContent />
</EntityLayout.Route>
</EntityLayout>
</EntityProvider>
</ApiProvider>,
{
mountedRoutes: {
'/adr': rootRouteRef,
},
},
);
expect(getAdrFilesSpyInstance).toHaveBeenCalled();
});
it('Uses an alternative AdrFileFetcher when provided', async () => {
const octokitGetAdrFilesSpyInstance = jest
.spyOn(octokitAdrFileFetcher, 'useGetAdrFilesAtUrl')
.mockImplementation(() => {
return {
data: [],
};
});
const urlReadersGetAdrFilesSpyInstance = jest
.spyOn(urlReaderAdrFileFetcher, 'useGetAdrFilesAtUrl')
.mockImplementation(() => {
return {
data: [],
};
});
await renderInTestApp(
<ApiProvider apis={mockApis}>
<EntityProvider entity={mockEntity}>
<EntityLayout>
<EntityLayout.Route path="/" title="tabbed-test-title">
<EntityAdrContent adrFileFetcher={urlReaderAdrFileFetcher} />
</EntityLayout.Route>
</EntityLayout>
</EntityProvider>
</ApiProvider>,
{
mountedRoutes: {
'/adr': rootRouteRef,
},
},
);
expect(octokitGetAdrFilesSpyInstance).not.toHaveBeenCalled();
expect(urlReadersGetAdrFilesSpyInstance).toHaveBeenCalled();
});
});