From 35bba616b7f4be8aeb7822c2dd36136cd7f0eb8d Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Thu, 17 Aug 2023 15:37:08 +0200 Subject: [PATCH 1/2] catalog: EntitySwitch render children when entity is not found Signed-off-by: Vincenzo Scamporlino --- .../EntitySwitch/EntitySwitch.test.tsx | 46 ++++++++++++++++++- .../components/EntitySwitch/EntitySwitch.tsx | 15 ++++-- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/plugins/catalog/src/components/EntitySwitch/EntitySwitch.test.tsx b/plugins/catalog/src/components/EntitySwitch/EntitySwitch.test.tsx index c93922e938..d9998b0ca5 100644 --- a/plugins/catalog/src/components/EntitySwitch/EntitySwitch.test.tsx +++ b/plugins/catalog/src/components/EntitySwitch/EntitySwitch.test.tsx @@ -59,6 +59,50 @@ describe('EntitySwitch', () => { expect(screen.queryByText('C')).not.toBeInTheDocument(); }); + it('should render the default case if entity is not found', () => { + const content = ( + + + + + + ); + + render( + + + {content} + + , + ); + + expect(screen.queryByText('A')).not.toBeInTheDocument(); + expect(screen.queryByText('B')).not.toBeInTheDocument(); + expect(screen.queryByText('C')).toBeInTheDocument(); + }); + + it(`shouldn't render any children if entity is loading and no entity exists in the context`, () => { + const content = ( + + + + + + ); + + render( + + + {content} + + , + ); + + expect(screen.queryByText('A')).not.toBeInTheDocument(); + expect(screen.queryByText('B')).not.toBeInTheDocument(); + expect(screen.queryByText('C')).not.toBeInTheDocument(); + }); + it('should render the fallback if no cases are matching', () => { const content = ( @@ -170,7 +214,7 @@ describe('EntitySwitch', () => { expect(screen.queryByText('A')).not.toBeInTheDocument(); expect(screen.queryByText('B')).not.toBeInTheDocument(); - expect(screen.queryByText('C')).not.toBeInTheDocument(); + expect(screen.queryByText('C')).toBeInTheDocument(); }); it('should switch child when filters switch', () => { diff --git a/plugins/catalog/src/components/EntitySwitch/EntitySwitch.tsx b/plugins/catalog/src/components/EntitySwitch/EntitySwitch.tsx index b12495052e..9eab743308 100644 --- a/plugins/catalog/src/components/EntitySwitch/EntitySwitch.tsx +++ b/plugins/catalog/src/components/EntitySwitch/EntitySwitch.tsx @@ -64,7 +64,7 @@ export interface EntitySwitchProps { /** @public */ export const EntitySwitch = (props: EntitySwitchProps) => { - const { entity } = useAsyncEntity(); + const { entity, loading } = useAsyncEntity(); const apis = useApiHolder(); const results = useElementFilter( @@ -77,14 +77,21 @@ export const EntitySwitch = (props: EntitySwitchProps) => { }) .getElements() .flatMap((element: ReactElement) => { - // If the entity is missing or there is an error, render nothing - if (!entity) { + if (loading && !entity) { return []; } const { if: condition, children: elementsChildren } = element.props as EntitySwitchCase; + if (!entity) { + return [ + { + if: condition === undefined, + children: elementsChildren, + }, + ]; + } return [ { if: condition?.(entity, { apis }), @@ -92,7 +99,7 @@ export const EntitySwitch = (props: EntitySwitchProps) => { }, ]; }), - [apis, entity], + [apis, entity, loading], ); const hasAsyncCases = results.some( From 163a41035e429282bed6fa645cb213d2102133d3 Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Thu, 17 Aug 2023 21:26:30 +0200 Subject: [PATCH 2/2] add changeset Signed-off-by: Vincenzo Scamporlino --- .changeset/dull-tables-joke.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/dull-tables-joke.md diff --git a/.changeset/dull-tables-joke.md b/.changeset/dull-tables-joke.md new file mode 100644 index 0000000000..e987377023 --- /dev/null +++ b/.changeset/dull-tables-joke.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog': patch +--- + +Fixed an issue where `EntitySwitch` was preventing the display of entity errors.