From 4b3537ee236b58519b315f6f063db9907d0c0bc0 Mon Sep 17 00:00:00 2001 From: Tim Hansen Date: Thu, 19 Aug 2021 20:36:06 -0600 Subject: [PATCH] Add test for CatalogKindHeader Signed-off-by: Tim Hansen --- .../catalog-react/src/hooks/useEntityKinds.ts | 3 +- .../catalog-react/src/testUtils/providers.tsx | 6 +- .../CatalogPage/CatalogKindHeader.test.tsx | 112 ++++++++++++++++++ .../CatalogPage/CatalogKindHeader.tsx | 4 +- 4 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 plugins/catalog/src/components/CatalogPage/CatalogKindHeader.test.tsx diff --git a/plugins/catalog-react/src/hooks/useEntityKinds.ts b/plugins/catalog-react/src/hooks/useEntityKinds.ts index 1aaf40981a..21bdad2900 100644 --- a/plugins/catalog-react/src/hooks/useEntityKinds.ts +++ b/plugins/catalog-react/src/hooks/useEntityKinds.ts @@ -18,8 +18,9 @@ import { useEffect, useState } from 'react'; import { useApi } from '@backstage/core-plugin-api'; import { catalogApiRef } from '../api'; +// Retrieve a list of unique entity kinds present in the catalog export function useEntityKinds() { - const [kinds, setKinds] = useState(['component']); + const [kinds, setKinds] = useState(['Component']); const catalogApi = useApi(catalogApiRef); useEffect(() => { diff --git a/plugins/catalog-react/src/testUtils/providers.tsx b/plugins/catalog-react/src/testUtils/providers.tsx index 1f9e4f40d6..2172f6c5a9 100644 --- a/plugins/catalog-react/src/testUtils/providers.tsx +++ b/plugins/catalog-react/src/testUtils/providers.tsx @@ -25,12 +25,12 @@ export const MockEntityListContextProvider = ({ children, value, }: PropsWithChildren<{ - value: Partial; + value?: Partial; }>) => { // Provides a default implementation that stores filter state, for testing components that // reflect filter state. const [filters, setFilters] = useState( - value.filters ?? {}, + value?.filters ?? {}, ); const updateFilters = useCallback( ( @@ -60,7 +60,7 @@ export const MockEntityListContextProvider = ({ // Extract value.filters to avoid overwriting it; some tests exercise filter updates. The value // provided is used as the initial seed in useState above. - const { filters: _, ...otherContextFields } = value; + const { filters: _, ...otherContextFields } = value ?? {}; return ( Promise.resolve({ items: entities })), + } as unknown as CatalogApi, + ], +]); + +describe('', () => { + it('renders available kinds', async () => { + const rendered = await renderWithEffects( + + + + + , + ); + + const input = rendered.getByText('Components'); + fireEvent.mouseDown(input); + + entities.map(entity => { + expect( + rendered.getByRole('option', { name: `${entity.kind}s` }), + ).toBeInTheDocument(); + }); + }); + + it('updates the kind filter', async () => { + const updateFilters = jest.fn(); + const rendered = await renderWithEffects( + + + + + , + ); + + const input = rendered.getByText('Components'); + fireEvent.mouseDown(input); + + const option = rendered.getByRole('option', { name: 'Templates' }); + fireEvent.click(option); + + expect(updateFilters).toHaveBeenCalledWith({ + kind: new EntityKindFilter('Template'), + }); + }); +}); diff --git a/plugins/catalog/src/components/CatalogPage/CatalogKindHeader.tsx b/plugins/catalog/src/components/CatalogPage/CatalogKindHeader.tsx index 985f8fed6a..16051a0951 100644 --- a/plugins/catalog/src/components/CatalogPage/CatalogKindHeader.tsx +++ b/plugins/catalog/src/components/CatalogPage/CatalogKindHeader.tsx @@ -67,7 +67,9 @@ export const CatalogKindHeader = ({ classes={classes} > {allKinds.map(kind => ( - {capitalize(kind)}s + + {`${capitalize(kind)}s`} + ))} );