catalog: fix EntitySwitch to render default case
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user