@@ -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<string, EntityRefPresentationSnapshot>(
|
||||
await Promise.all(
|
||||
entities?.map(async entity => {
|
||||
const presentation = await entityPresentationApi.forEntity(entity)
|
||||
.promise;
|
||||
return [stringifyEntityRef(entity), presentation] as [
|
||||
string,
|
||||
EntityRefPresentationSnapshot,
|
||||
];
|
||||
}) || [],
|
||||
),
|
||||
);
|
||||
});
|
||||
if (loading) return <Progress />;
|
||||
if (!entities) return null;
|
||||
return (
|
||||
<ItemCardGrid data-testid="docs-explore">
|
||||
@@ -78,10 +56,7 @@ export const DocsCardGrid = (props: DocsCardGridProps) => {
|
||||
<Card key={index}>
|
||||
<CardMedia>
|
||||
<ItemCardHeader
|
||||
title={
|
||||
entityRefToPresentation?.get(stringifyEntityRef(entity))
|
||||
?.primaryTitle
|
||||
}
|
||||
title={entity.metadata.title ?? entity.metadata.name}
|
||||
/>
|
||||
</CardMedia>
|
||||
<CardContent>{entity.metadata.description}</CardContent>
|
||||
|
||||
@@ -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<React.PropsWithChildren<{}>>;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
Wrapper = ({ children }: { children?: React.ReactNode }) => (
|
||||
<TestApiProvider
|
||||
apis={[
|
||||
[
|
||||
entityPresentationApiRef,
|
||||
DefaultEntityPresentationApi.create({ catalogApi }),
|
||||
],
|
||||
]}
|
||||
>
|
||||
{children}
|
||||
</TestApiProvider>
|
||||
);
|
||||
});
|
||||
|
||||
it('should render multiple entities', async () => {
|
||||
await renderInTestApp(
|
||||
<InfoCardGrid
|
||||
entities={[
|
||||
{
|
||||
apiVersion: 'version',
|
||||
kind: 'TestKind',
|
||||
metadata: {
|
||||
name: 'testName',
|
||||
title: 'TestTitle',
|
||||
<Wrapper>
|
||||
<InfoCardGrid
|
||||
entities={[
|
||||
{
|
||||
apiVersion: 'version',
|
||||
kind: 'TestKind',
|
||||
metadata: {
|
||||
name: 'testName',
|
||||
title: 'TestTitle',
|
||||
},
|
||||
spec: {
|
||||
owner: 'techdocs@example.com',
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
owner: 'techdocs@example.com',
|
||||
{
|
||||
apiVersion: 'version',
|
||||
kind: 'TestKind2',
|
||||
metadata: {
|
||||
name: 'testName2',
|
||||
title: 'TestTitle2',
|
||||
},
|
||||
spec: {
|
||||
owner: 'techdocs2@example.com',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
apiVersion: 'version',
|
||||
kind: 'TestKind2',
|
||||
metadata: {
|
||||
name: 'testName2',
|
||||
title: 'TestTitle2',
|
||||
},
|
||||
spec: {
|
||||
owner: 'techdocs2@example.com',
|
||||
},
|
||||
},
|
||||
]}
|
||||
/>,
|
||||
]}
|
||||
/>
|
||||
</Wrapper>,
|
||||
{
|
||||
mountedRoutes: {
|
||||
'/docs/:namespace/:kind/:name/*': rootDocsRouteRef,
|
||||
@@ -66,32 +87,34 @@ describe('Entity Info Card Grid', () => {
|
||||
|
||||
it('should render links correctly', async () => {
|
||||
await renderInTestApp(
|
||||
<InfoCardGrid
|
||||
entities={[
|
||||
{
|
||||
apiVersion: 'version',
|
||||
kind: 'TestKind',
|
||||
metadata: {
|
||||
name: 'testName',
|
||||
title: 'TestTitle',
|
||||
<Wrapper>
|
||||
<InfoCardGrid
|
||||
entities={[
|
||||
{
|
||||
apiVersion: 'version',
|
||||
kind: 'TestKind',
|
||||
metadata: {
|
||||
name: 'testName',
|
||||
title: 'TestTitle',
|
||||
},
|
||||
spec: {
|
||||
owner: 'techdocs@example.com',
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
owner: 'techdocs@example.com',
|
||||
{
|
||||
apiVersion: 'version',
|
||||
kind: 'TestKind2',
|
||||
metadata: {
|
||||
name: 'testName2',
|
||||
title: 'TestTitle2',
|
||||
},
|
||||
spec: {
|
||||
owner: 'techdocs2@example.com',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
apiVersion: 'version',
|
||||
kind: 'TestKind2',
|
||||
metadata: {
|
||||
name: 'testName2',
|
||||
title: 'TestTitle2',
|
||||
},
|
||||
spec: {
|
||||
owner: 'techdocs2@example.com',
|
||||
},
|
||||
},
|
||||
]}
|
||||
/>,
|
||||
]}
|
||||
/>
|
||||
</Wrapper>,
|
||||
{
|
||||
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(
|
||||
<InfoCardGrid
|
||||
entities={[
|
||||
{
|
||||
apiVersion: 'version',
|
||||
kind: 'TestKind',
|
||||
metadata: {
|
||||
name: 'testName',
|
||||
title: 'TestTitle',
|
||||
<Wrapper>
|
||||
<InfoCardGrid
|
||||
entities={[
|
||||
{
|
||||
apiVersion: 'version',
|
||||
kind: 'TestKind',
|
||||
metadata: {
|
||||
name: 'testName',
|
||||
title: 'TestTitle',
|
||||
},
|
||||
spec: {
|
||||
owner: 'techdocs@example.com',
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
owner: 'techdocs@example.com',
|
||||
},
|
||||
},
|
||||
]}
|
||||
/>,
|
||||
]}
|
||||
/>
|
||||
</Wrapper>,
|
||||
{
|
||||
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(
|
||||
<InfoCardGrid
|
||||
entities={[
|
||||
{
|
||||
apiVersion: 'version',
|
||||
kind: 'TestKind',
|
||||
metadata: {
|
||||
name: 'testName',
|
||||
<Wrapper>
|
||||
<InfoCardGrid
|
||||
entities={[
|
||||
{
|
||||
apiVersion: 'version',
|
||||
kind: 'TestKind',
|
||||
metadata: {
|
||||
name: 'testName',
|
||||
},
|
||||
spec: {
|
||||
owner: 'techdocs@example.com',
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
owner: 'techdocs@example.com',
|
||||
},
|
||||
},
|
||||
]}
|
||||
/>,
|
||||
]}
|
||||
/>
|
||||
</Wrapper>,
|
||||
{
|
||||
mountedRoutes: {
|
||||
'/docs/:namespace/:kind/:name/*': rootDocsRouteRef,
|
||||
|
||||
Reference in New Issue
Block a user