Merge pull request #10214 from backstage/rugvip/missing-entities

catalog: fix EntitySwitch to render default case
This commit is contained in:
Patrik Oldsberg
2022-03-16 09:40:18 +01:00
committed by GitHub
3 changed files with 36 additions and 4 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog': patch
---
Fix for `EntitySwitch` not properly falling back to render the default entity page when the entity is missing.
@@ -15,7 +15,10 @@
*/
import { Entity } from '@backstage/catalog-model';
import { EntityProvider } from '@backstage/plugin-catalog-react';
import {
AsyncEntityProvider,
EntityProvider,
} from '@backstage/plugin-catalog-react';
import { render } from '@testing-library/react';
import React from 'react';
import { isKind } from './conditions';
@@ -76,6 +79,18 @@ describe('EntitySwitch', () => {
expect(rendered.queryByText('A')).not.toBeInTheDocument();
expect(rendered.queryByText('B')).not.toBeInTheDocument();
expect(rendered.queryByText('C')).toBeInTheDocument();
rendered.rerender(
<Wrapper>
<AsyncEntityProvider entity={undefined} loading={false}>
{content}
</AsyncEntityProvider>
</Wrapper>,
);
expect(rendered.queryByText('A')).not.toBeInTheDocument();
expect(rendered.queryByText('B')).not.toBeInTheDocument();
expect(rendered.queryByText('C')).toBeInTheDocument();
});
it('should switch child when filters switch', () => {
@@ -63,7 +63,7 @@ export interface EntitySwitchProps {
/** @public */
export const EntitySwitch = (props: EntitySwitchProps) => {
const { entity } = useAsyncEntity();
const { entity, loading } = useAsyncEntity();
const apis = useApiHolder();
const results = useElementFilter(
props.children,
@@ -75,11 +75,23 @@ export const EntitySwitch = (props: EntitySwitchProps) => {
})
.getElements()
.flatMap<SwitchCaseResult>((element: ReactElement) => {
if (!entity) {
// Nothing is rendered while loading
if (loading) {
return [];
}
const { if: condition, children: elementsChildren } =
element.props as EntitySwitchCase;
// If the entity is missing or there is an error, render the default page
if (!entity) {
return [
{
if: condition === undefined,
children: elementsChildren,
},
];
}
return [
{
if: condition?.(entity, { apis }) ?? true,
@@ -87,7 +99,7 @@ export const EntitySwitch = (props: EntitySwitchProps) => {
},
];
}),
[apis, entity],
[apis, entity, loading],
);
const hasAsyncCases = results.some(
r => typeof r.if === 'object' && 'then' in r.if,