From bec8aab60ece40da62c0a3309f45370e4bad2bd3 Mon Sep 17 00:00:00 2001 From: Tim Hansen Date: Fri, 21 May 2021 20:48:46 -0600 Subject: [PATCH] Fix tests Signed-off-by: Tim Hansen --- .../UserListPicker/UserListPicker.test.tsx | 36 +++++++++---------- plugins/catalog-react/src/index.ts | 1 + plugins/catalog-react/src/testUtils/index.ts | 16 +++++++++ .../catalog-react/src/testUtils/providers.tsx | 7 ++-- .../CatalogTable/CatalogTable.test.tsx | 33 +++++------------ 5 files changed, 44 insertions(+), 49 deletions(-) create mode 100644 plugins/catalog-react/src/testUtils/index.ts diff --git a/plugins/catalog-react/src/components/UserListPicker/UserListPicker.test.tsx b/plugins/catalog-react/src/components/UserListPicker/UserListPicker.test.tsx index 9809dbfbda..a85ddd232b 100644 --- a/plugins/catalog-react/src/components/UserListPicker/UserListPicker.test.tsx +++ b/plugins/catalog-react/src/components/UserListPicker/UserListPicker.test.tsx @@ -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('', () => { const backendEntities: Entity[] = [ @@ -143,7 +138,7 @@ describe('', () => { it('includes counts alongside each filter', () => { const { getAllByRole } = render( - + , @@ -165,6 +160,7 @@ describe('', () => { value={{ backendEntities, filters: { tags: new EntityTagFilter(['tag1']) }, + filterEnv, }} > diff --git a/plugins/catalog-react/src/index.ts b/plugins/catalog-react/src/index.ts index b0bd35471d..8333925652 100644 --- a/plugins/catalog-react/src/index.ts +++ b/plugins/catalog-react/src/index.ts @@ -24,5 +24,6 @@ export { entityRouteRef, rootRoute, } from './routes'; +export * from './testUtils'; export * from './types'; export * from './utils'; diff --git a/plugins/catalog-react/src/testUtils/index.ts b/plugins/catalog-react/src/testUtils/index.ts new file mode 100644 index 0000000000..090e9190e4 --- /dev/null +++ b/plugins/catalog-react/src/testUtils/index.ts @@ -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'; diff --git a/plugins/catalog-react/src/testUtils/providers.tsx b/plugins/catalog-react/src/testUtils/providers.tsx index 4a808179f6..3cde513b81 100644 --- a/plugins/catalog-react/src/testUtils/providers.tsx +++ b/plugins/catalog-react/src/testUtils/providers.tsx @@ -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 }>) => { - 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, }; diff --git a/plugins/catalog/src/components/CatalogTable/CatalogTable.test.tsx b/plugins/catalog/src/components/CatalogTable/CatalogTable.test.tsx index 56d2db6bba..ce8447cf6d 100644 --- a/plugins/catalog/src/components/CatalogTable/CatalogTable.test.tsx +++ b/plugins/catalog/src/components/CatalogTable/CatalogTable.test.tsx @@ -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( - + - , + , ); 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( - - , + , ); expect(rendered.getByText(/Owned \(3\)/)).toBeInTheDocument(); expect(rendered.getByText(/component1/)).toBeInTheDocument(); @@ -106,11 +95,9 @@ describe('CatalogTable component', () => { }; const { getByTitle } = await renderInTestApp( - + - , + , ); const editButton = getByTitle('Edit'); @@ -133,11 +120,9 @@ describe('CatalogTable component', () => { }; const { getByTitle } = await renderInTestApp( - + - , + , ); const viewButton = getByTitle('View');