Added a test case which covers the case when the array of entities was provided to entityRefs

Signed-off-by: bnechyporenko <bnechyporenko@bol.com>
This commit is contained in:
bnechyporenko
2022-09-29 15:32:26 +02:00
parent 4a7dd7865e
commit 359efe7db9
2 changed files with 100 additions and 10 deletions
@@ -132,4 +132,87 @@ describe('<FetchedEntityRefLinks />', () => {
'/catalog/default/api/implementation',
);
});
it('should handle heterogeneous array of values to render the custom display text', async () => {
const entityRefs = [
...[
{
kind: 'Component',
namespace: 'default',
name: 'tool',
},
{
kind: 'API',
namespace: 'default',
name: 'implementation',
},
].map(ref => ({
apiVersion: 'backstage.io/v1alpha1',
kind: ref.kind,
metadata: {
name: ref.name,
namespace: ref.namespace,
},
spec: {
profile: {
displayName: ref.name.toLocaleUpperCase('en-US'),
},
type: 'organization',
},
})),
{
kind: 'Component',
namespace: 'default',
name: 'interface',
},
];
const catalogApi: Partial<CatalogApi> = {
getEntities: () =>
Promise.resolve({
items: [
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'interface',
namespace: 'default',
},
spec: {
profile: {
displayName: 'INTERFACE',
},
type: 'organization',
},
},
],
}),
};
const rendered = await renderInTestApp(
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
<FetchedEntityRefLinks entityRefs={entityRefs} getTitle={getTitle} />
</TestApiProvider>,
{
mountedRoutes: {
'/catalog/:namespace/:kind/:name/*': entityRouteRef,
},
},
);
expect(rendered.getByText('TOOL')).toHaveAttribute(
'href',
'/catalog/default/component/tool',
);
expect(rendered.getByText('IMPLEMENTATION')).toHaveAttribute(
'href',
'/catalog/default/api/implementation',
);
expect(rendered.getByText('INTERFACE')).toHaveAttribute(
'href',
'/catalog/default/component/interface',
);
});
});
@@ -60,17 +60,24 @@ export function FetchedEntityRefLinks<
return 'metadata' in current ? acc : [...acc, parseEntityRef(current)];
}, new Array<CompoundEntityRef>());
const pureEntities = entityRefs.filter(
ref => 'metadata' in ref,
) as Array<Entity>;
return refs.length > 0
? (
await catalogApi.getEntities({
filter: refs.map(ref => ({
kind: ref.kind,
'metadata.namespace': ref.namespace,
'metadata.name': ref.name,
})),
})
).items
: (entityRefs as Array<Entity>);
? [
...(
await catalogApi.getEntities({
filter: refs.map(ref => ({
kind: ref.kind,
'metadata.namespace': ref.namespace,
'metadata.name': ref.name,
})),
})
).items,
...pureEntities,
]
: pureEntities;
}, [entityRefs]);
if (loading) {