Fix tests

Signed-off-by: Tim Hansen <timbonicus@gmail.com>
This commit is contained in:
Tim Hansen
2021-05-21 20:48:46 -06:00
parent 09230dd5d8
commit bec8aab60e
5 changed files with 44 additions and 49 deletions
@@ -25,7 +25,7 @@ import {
ConfigApi,
configApiRef,
} from '@backstage/core-api';
import { EntityTagFilter } from '../../types';
import { EntityTagFilter, FilterEnvironment } from '../../types';
const apis = ApiRegistry.from([
[
@@ -41,25 +41,20 @@ const apis = ApiRegistry.from([
],
]);
jest.mock('../../hooks', () => ({
useOwnUser: jest.fn().mockReturnValue({
value: {
apiVersion: '1',
kind: 'User',
metadata: {
namespace: 'default',
name: 'testUser',
},
const filterEnv: FilterEnvironment = {
user: {
apiVersion: 'backstage.io/v1alpha1',
kind: 'User',
metadata: {
namespace: 'default',
name: 'testUser',
},
}),
useStarredEntities: jest.fn().mockReturnValue({
isStarredEntity: jest.fn(
(entity: Entity) => entity.metadata.name === 'component-3',
),
}),
useEntityListProvider: jest.requireActual('../../hooks')
.useEntityListProvider,
}));
spec: {
memberOf: [],
},
},
isStarredEntity: (entity: Entity) => entity.metadata.name === 'component-3',
};
describe('<UserListPicker />', () => {
const backendEntities: Entity[] = [
@@ -143,7 +138,7 @@ describe('<UserListPicker />', () => {
it('includes counts alongside each filter', () => {
const { getAllByRole } = render(
<ApiProvider apis={apis}>
<MockEntityListContextProvider value={{ backendEntities }}>
<MockEntityListContextProvider value={{ backendEntities, filterEnv }}>
<UserListPicker />
</MockEntityListContextProvider>
</ApiProvider>,
@@ -165,6 +160,7 @@ describe('<UserListPicker />', () => {
value={{
backendEntities,
filters: { tags: new EntityTagFilter(['tag1']) },
filterEnv,
}}
>
<UserListPicker />
+1
View File
@@ -24,5 +24,6 @@ export {
entityRouteRef,
rootRoute,
} from './routes';
export * from './testUtils';
export * from './types';
export * from './utils';
@@ -0,0 +1,16 @@
/*
* Copyright 2021 Spotify AB
*
* 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 { MockEntityListContextProvider } from './providers';
@@ -15,7 +15,6 @@
*/
import React, { PropsWithChildren } from 'react';
import { useOwnUser, useStarredEntities } from '../hooks';
import {
EntityListContext,
EntityListContextProps,
@@ -25,16 +24,14 @@ export const MockEntityListContextProvider = ({
children,
value,
}: PropsWithChildren<{ value: Partial<EntityListContextProps> }>) => {
const { value: user } = useOwnUser();
const { isStarredEntity } = useStarredEntities();
const defaultContext: EntityListContextProps = {
entities: [],
backendEntities: [],
updateFilters: jest.fn(),
filters: {},
filterEnv: {
user,
isStarredEntity,
user: undefined,
isStarredEntity: () => false,
},
loading: false,
};
@@ -24,7 +24,7 @@ import { renderInTestApp } from '@backstage/test-utils';
import * as React from 'react';
import { CatalogTable } from './CatalogTable';
import {
EntityListContext,
MockEntityListContextProvider,
UserListFilter,
} from '@backstage/plugin-catalog-react';
@@ -46,14 +46,6 @@ const entities: Entity[] = [
},
];
const emptyEntityListContext = {
entities: [],
backendEntities: [],
filters: [],
loading: false,
updateFilters: () => {},
};
describe('CatalogTable component', () => {
beforeEach(() => {
window.open = jest.fn();
@@ -65,11 +57,9 @@ describe('CatalogTable component', () => {
it('should render error message', async () => {
const rendered = await renderInTestApp(
<EntityListContext.Provider
value={{ ...emptyEntityListContext, error: new Error('error') }}
>
<MockEntityListContextProvider value={{ error: new Error('error') }}>
<CatalogTable />
</EntityListContext.Provider>,
</MockEntityListContextProvider>,
);
const errorMessage = await rendered.findByText(
/Could not fetch catalog entities./,
@@ -79,15 +69,14 @@ describe('CatalogTable component', () => {
it('should display entity names when loading has finished and no error occurred', async () => {
const rendered = await renderInTestApp(
<EntityListContext.Provider
<MockEntityListContextProvider
value={{
...emptyEntityListContext,
entities,
filters: { user: new UserListFilter('owned') },
}}
>
<CatalogTable />
</EntityListContext.Provider>,
</MockEntityListContextProvider>,
);
expect(rendered.getByText(/Owned \(3\)/)).toBeInTheDocument();
expect(rendered.getByText(/component1/)).toBeInTheDocument();
@@ -106,11 +95,9 @@ describe('CatalogTable component', () => {
};
const { getByTitle } = await renderInTestApp(
<EntityListContext.Provider
value={{ ...emptyEntityListContext, entities: [entity] }}
>
<MockEntityListContextProvider value={{ entities: [entity] }}>
<CatalogTable />
</EntityListContext.Provider>,
</MockEntityListContextProvider>,
);
const editButton = getByTitle('Edit');
@@ -133,11 +120,9 @@ describe('CatalogTable component', () => {
};
const { getByTitle } = await renderInTestApp(
<EntityListContext.Provider
value={{ ...emptyEntityListContext, entities: [entity] }}
>
<MockEntityListContextProvider value={{ entities: [entity] }}>
<CatalogTable />
</EntityListContext.Provider>,
</MockEntityListContextProvider>,
);
const viewButton = getByTitle('View');