From a1711da81b4558e7a94db5495289f1499fa8ed26 Mon Sep 17 00:00:00 2001 From: Patrik Ivarsson Date: Wed, 31 Jan 2024 19:13:35 +0100 Subject: [PATCH] add tests verifying catalog backend is called with correct filters Signed-off-by: Patrik Ivarsson --- .../EntityAutocompletePicker.test.tsx | 81 ++++++++++++++++++- 1 file changed, 78 insertions(+), 3 deletions(-) diff --git a/plugins/catalog-react/src/components/EntityAutocompletePicker/EntityAutocompletePicker.test.tsx b/plugins/catalog-react/src/components/EntityAutocompletePicker/EntityAutocompletePicker.test.tsx index 32e66dc578..94af52f374 100644 --- a/plugins/catalog-react/src/components/EntityAutocompletePicker/EntityAutocompletePicker.test.tsx +++ b/plugins/catalog-react/src/components/EntityAutocompletePicker/EntityAutocompletePicker.test.tsx @@ -24,6 +24,7 @@ import { CatalogApi } from '@backstage/catalog-client'; import { DefaultEntityFilters } from '../../hooks'; import { Entity } from '@backstage/catalog-model'; import { EntityFilter } from '../../types'; +import { EntityKindFilter, EntityTypeFilter } from '../../filters'; interface EntityFilters extends DefaultEntityFilters { options?: EntityOptionFilter; @@ -46,13 +47,17 @@ class EntityOptionFilter implements EntityFilter { } describe('', () => { - const mockCatalogApi = { - getEntityFacets: async () => ({ + const mockCatalogApi: Partial> = { + getEntityFacets: jest.fn().mockResolvedValue({ facets: { 'spec.options': options.map((value, idx) => ({ value, count: idx })), }, }), - } as unknown as CatalogApi; + }; + + beforeEach(() => { + jest.clearAllMocks(); + }); it('renders all options', async () => { render( @@ -71,6 +76,12 @@ describe('', () => { expect(screen.getByText('Options')).toBeInTheDocument(), ); + // should have called catalog backend without any filters applied + expect(mockCatalogApi.getEntityFacets).toHaveBeenCalledWith({ + facets: ['spec.options'], + filter: {}, + }); + fireEvent.click(screen.getByTestId('options-picker-expand')); options.forEach(option => { expect(screen.getByText(option)).toBeInTheDocument(); @@ -267,4 +278,68 @@ describe('', () => { options: new EntityOptionFilter(['option2']), }); }); + + it('filters available values by kind as default', async () => { + render( + + + + label="Options" + path="spec.options" + name="options" + Filter={EntityOptionFilter} + /> + + , + ); + + await waitFor(() => + expect(mockCatalogApi.getEntityFacets).toHaveBeenCalledWith({ + facets: ['spec.options'], + filter: { + kind: 'Component', + }, + }), + ); + }); + + it('can be supplied with filters for available values', async () => { + render( + + + + label="Options" + path="spec.options" + name="options" + Filter={EntityOptionFilter} + filtersForAvailableValues={['kind', 'type']} + /> + + , + ); + + await waitFor(() => + expect(mockCatalogApi.getEntityFacets).toHaveBeenCalledWith({ + facets: ['spec.options'], + filter: { + kind: 'Component', + 'spec.type': ['service'], + }, + }), + ); + }); });