From 301a40f00ff363efdf8550575ab74b1cabc6882f Mon Sep 17 00:00:00 2001 From: nikolar Date: Tue, 14 Jan 2025 18:54:16 -0800 Subject: [PATCH] fix tests Signed-off-by: nikolar --- plugins/techdocs/package.json | 1 + .../home/components/Grids/DocsCardGrid.tsx | 29 +-- .../components/Grids/InfoCardGrid.test.tsx | 179 ++++++++++-------- yarn.lock | 1 + 4 files changed, 107 insertions(+), 103 deletions(-) diff --git a/plugins/techdocs/package.json b/plugins/techdocs/package.json index b9cc59fd10..50aeedd02d 100644 --- a/plugins/techdocs/package.json +++ b/plugins/techdocs/package.json @@ -92,6 +92,7 @@ "@backstage/cli": "workspace:^", "@backstage/core-app-api": "workspace:^", "@backstage/dev-utils": "workspace:^", + "@backstage/plugin-catalog": "workspace:^", "@backstage/plugin-techdocs-module-addons-contrib": "workspace:^", "@backstage/test-utils": "workspace:^", "@testing-library/dom": "^10.0.0", diff --git a/plugins/techdocs/src/home/components/Grids/DocsCardGrid.tsx b/plugins/techdocs/src/home/components/Grids/DocsCardGrid.tsx index 2d5d5fa7c0..d2947b5f64 100644 --- a/plugins/techdocs/src/home/components/Grids/DocsCardGrid.tsx +++ b/plugins/techdocs/src/home/components/Grids/DocsCardGrid.tsx @@ -16,18 +16,12 @@ import { rootDocsRouteRef } from '../../../routes'; import { toLowerMaybe } from '../../../helpers'; -import useAsync from 'react-use/esm/useAsync'; -import { - EntityRefPresentationSnapshot, - entityPresentationApiRef, -} from '@backstage/plugin-catalog-react'; -import { Entity, stringifyEntityRef } from '@backstage/catalog-model'; +import { Entity } from '@backstage/catalog-model'; import { useApi, useRouteRef, configApiRef } from '@backstage/core-plugin-api'; import { LinkButton, ItemCardGrid, ItemCardHeader, - Progress, } from '@backstage/core-components'; import Card from '@material-ui/core/Card'; import CardActions from '@material-ui/core/CardActions'; @@ -53,22 +47,6 @@ export const DocsCardGrid = (props: DocsCardGridProps) => { const { entities } = props; const getRouteToReaderPageFor = useRouteRef(rootDocsRouteRef); const config = useApi(configApiRef); - const entityPresentationApi = useApi(entityPresentationApiRef); - const { value: entityRefToPresentation, loading } = useAsync(async () => { - return new Map( - await Promise.all( - entities?.map(async entity => { - const presentation = await entityPresentationApi.forEntity(entity) - .promise; - return [stringifyEntityRef(entity), presentation] as [ - string, - EntityRefPresentationSnapshot, - ]; - }) || [], - ), - ); - }); - if (loading) return ; if (!entities) return null; return ( @@ -78,10 +56,7 @@ export const DocsCardGrid = (props: DocsCardGridProps) => { {entity.metadata.description} diff --git a/plugins/techdocs/src/home/components/Grids/InfoCardGrid.test.tsx b/plugins/techdocs/src/home/components/Grids/InfoCardGrid.test.tsx index a2d95950f9..e1e3ce1511 100644 --- a/plugins/techdocs/src/home/components/Grids/InfoCardGrid.test.tsx +++ b/plugins/techdocs/src/home/components/Grids/InfoCardGrid.test.tsx @@ -14,45 +14,66 @@ * limitations under the License. */ -import { renderInTestApp } from '@backstage/test-utils'; +import { TestApiProvider, renderInTestApp } from '@backstage/test-utils'; import { screen } from '@testing-library/react'; +import { entityPresentationApiRef } from '@backstage/plugin-catalog-react'; +import { DefaultEntityPresentationApi } from '@backstage/plugin-catalog'; +import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils'; + import React from 'react'; import { rootDocsRouteRef } from '../../../routes'; import { InfoCardGrid } from './InfoCardGrid'; describe('Entity Info Card Grid', () => { + const catalogApi = catalogApiMock(); + let Wrapper: React.ComponentType>; + beforeEach(() => { jest.resetAllMocks(); + Wrapper = ({ children }: { children?: React.ReactNode }) => ( + + {children} + + ); }); it('should render multiple entities', async () => { await renderInTestApp( - + , + ]} + /> + , { mountedRoutes: { '/docs/:namespace/:kind/:name/*': rootDocsRouteRef, @@ -66,32 +87,34 @@ describe('Entity Info Card Grid', () => { it('should render links correctly', async () => { await renderInTestApp( - + , + ]} + /> + , { mountedRoutes: { '/docs/:namespace/:kind/:name/*': rootDocsRouteRef, @@ -112,21 +135,23 @@ describe('Entity Info Card Grid', () => { it('should render entity title if available', async () => { await renderInTestApp( - + , + ]} + /> + , { mountedRoutes: { '/docs/:namespace/:kind/:name/*': rootDocsRouteRef, @@ -139,20 +164,22 @@ describe('Entity Info Card Grid', () => { it('should render entity name if title is not available', async () => { await renderInTestApp( - + , + ]} + /> + , { mountedRoutes: { '/docs/:namespace/:kind/:name/*': rootDocsRouteRef, diff --git a/yarn.lock b/yarn.lock index 16dbd0c921..b38c9eb061 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8694,6 +8694,7 @@ __metadata: "@backstage/integration": "workspace:^" "@backstage/integration-react": "workspace:^" "@backstage/plugin-auth-react": "workspace:^" + "@backstage/plugin-catalog": "workspace:^" "@backstage/plugin-catalog-react": "workspace:^" "@backstage/plugin-search-common": "workspace:^" "@backstage/plugin-search-react": "workspace:^"