From 286466a90ae4dc42b5e94b91124d2f09a6e464cc Mon Sep 17 00:00:00 2001 From: Tim Hansen Date: Thu, 19 Aug 2021 21:07:57 -0600 Subject: [PATCH] useEntityKinds test Signed-off-by: Tim Hansen --- .../src/hooks/useEntityKinds.test.tsx | 90 +++++++++++++++++++ .../CatalogPage/CatalogKindHeader.test.tsx | 2 +- 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 plugins/catalog-react/src/hooks/useEntityKinds.test.tsx diff --git a/plugins/catalog-react/src/hooks/useEntityKinds.test.tsx b/plugins/catalog-react/src/hooks/useEntityKinds.test.tsx new file mode 100644 index 0000000000..a1e1463174 --- /dev/null +++ b/plugins/catalog-react/src/hooks/useEntityKinds.test.tsx @@ -0,0 +1,90 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React, { PropsWithChildren } from 'react'; +import { ApiProvider, ApiRegistry } from '@backstage/core-app-api'; +import { CatalogApi } from '@backstage/catalog-client'; +import { Entity } from '@backstage/catalog-model'; +import { catalogApiRef } from '../api'; +import { renderHook } from '@testing-library/react-hooks'; +import { useEntityKinds } from './useEntityKinds'; + +const entities: Entity[] = [ + { + apiVersion: '1', + kind: 'Component', + metadata: { + name: 'component-1', + }, + }, + { + apiVersion: '1', + kind: 'Component', + metadata: { + name: 'component-2', + }, + }, + { + apiVersion: '1', + kind: 'Template', + metadata: { + name: 'template', + }, + }, + { + apiVersion: '1', + kind: 'System', + metadata: { + name: 'system', + }, + }, +]; + +const mockCatalogApi: Partial = { + getEntities: jest.fn().mockImplementation(async () => ({ items: entities })), +}; + +const wrapper = ({ children }: PropsWithChildren<{}>) => { + return ( + + {children} + + ); +}; + +describe('useEntityKinds', () => { + it('does not return duplicate kinds', async () => { + const { result, waitForValueToChange } = renderHook( + () => useEntityKinds(), + { + wrapper, + }, + ); + await waitForValueToChange(() => result.current); + expect(result.current.length).toBe(3); + }); + + it('sorts entity kinds', async () => { + const { result, waitForValueToChange } = renderHook( + () => useEntityKinds(), + { + wrapper, + }, + ); + await waitForValueToChange(() => result.current); + expect(result.current).toEqual(['Component', 'System', 'Template']); + }); +}); diff --git a/plugins/catalog/src/components/CatalogPage/CatalogKindHeader.test.tsx b/plugins/catalog/src/components/CatalogPage/CatalogKindHeader.test.tsx index 95c056cf6b..b39ad08359 100644 --- a/plugins/catalog/src/components/CatalogPage/CatalogKindHeader.test.tsx +++ b/plugins/catalog/src/components/CatalogPage/CatalogKindHeader.test.tsx @@ -65,7 +65,7 @@ const apis = ApiRegistry.from([ getEntities: jest .fn() .mockImplementation(() => Promise.resolve({ items: entities })), - } as unknown as CatalogApi, + } as Partial, ], ]);