From 737a5fdf918475f04c5d98d5163ee63c6b08a18e Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Thu, 24 Feb 2022 19:49:13 +0100 Subject: [PATCH 1/3] Update entity filter on entityName change. Signed-off-by: Eric Peterson --- .../reader/components/TechDocsSearch.test.tsx | 60 ++++++++++++++++++- .../src/reader/components/TechDocsSearch.tsx | 12 ++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/plugins/techdocs/src/reader/components/TechDocsSearch.test.tsx b/plugins/techdocs/src/reader/components/TechDocsSearch.test.tsx index 5b3d714a04..b4233734aa 100644 --- a/plugins/techdocs/src/reader/components/TechDocsSearch.test.tsx +++ b/plugins/techdocs/src/reader/components/TechDocsSearch.test.tsx @@ -23,7 +23,7 @@ import { waitFor, within, } from '@testing-library/react'; -import React from 'react'; +import React, { useState } from 'react'; import { TechDocsSearch } from './TechDocsSearch'; const entityId = { @@ -70,6 +70,7 @@ describe('', () => { expect(rendered.getByTestId('techdocs-search-bar')).toBeInTheDocument(); }); }); + it('should trigger query when autocomplete input changed', async () => { const query = () => singleResult; const querySpy = jest.fn(query); @@ -119,4 +120,61 @@ describe('', () => { ); }); }); + + it('should update filter values when a new entityName is provided', async () => { + const query = () => singleResult; + const querySpy = jest.fn(query); + const searchApi = { query: querySpy }; + const apiRegistry = TestApiRegistry.from([searchApiRef, searchApi]); + const newEntityId = { + name: 'test-diff', + namespace: 'testspace-diff', + kind: 'TestableDiff', + }; + + const WrappedSearchBar = () => { + const [entityName, setEntityName] = useState(entityId); + return wrapInTestApp( + + + + , + ); + }; + + await act(async () => { + const rendered = render(); + + await singleResult; + expect(querySpy).toBeCalledWith({ + filters: { + kind: 'Testable', + name: 'test', + namespace: 'testspace', + }, + pageCursor: '', + term: '', + types: ['techdocs'], + }); + + const button = rendered.getByText('Update Entity'); + button.click(); + + await singleResult; + await waitFor(() => + expect(querySpy).toBeCalledWith({ + filters: { + kind: 'TestableDiff', + name: 'test-diff', + namespace: 'testspace-diff', + }, + pageCursor: '', + term: '', + types: ['techdocs'], + }), + ); + }); + }); }); diff --git a/plugins/techdocs/src/reader/components/TechDocsSearch.tsx b/plugins/techdocs/src/reader/components/TechDocsSearch.tsx index 75cd3fcd6a..e45a26675b 100644 --- a/plugins/techdocs/src/reader/components/TechDocsSearch.tsx +++ b/plugins/techdocs/src/reader/components/TechDocsSearch.tsx @@ -64,6 +64,7 @@ const TechDocsSearchBar = ({ const { term, setTerm, + setFilters, result: { loading, value: searchVal }, } = useSearch(); const classes = useStyles(); @@ -87,6 +88,17 @@ const TechDocsSearchBar = ({ useDebounce(() => setTerm(value), debounceTime, [value]); + // Update the filter context when the entityId changes, e.g. when the search + // bar continues to be rendered, navigating between different TechDocs sites. + useEffect(() => { + setFilters(prevFilters => { + return { + ...prevFilters, + ...entityId, + }; + }); + }, [entityId, setFilters]); + const handleQuery = (e: ChangeEvent) => { if (!open) { setOpen(true); From 0ca964ee0e2ba7a754e0b9010e9589d59ba195bc Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Thu, 24 Feb 2022 19:51:45 +0100 Subject: [PATCH 2/3] Changeset. Signed-off-by: Eric Peterson --- .changeset/techdocs-war-on-war.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/techdocs-war-on-war.md diff --git a/.changeset/techdocs-war-on-war.md b/.changeset/techdocs-war-on-war.md new file mode 100644 index 0000000000..28722feed3 --- /dev/null +++ b/.changeset/techdocs-war-on-war.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-techdocs': patch +--- + +Fixed a bug that could cause searches in the in-context TechDocs search bar to show results from a different TechDocs site. From 2491754cfc7a17a14393b7b67140b8cdd146331b Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Fri, 25 Feb 2022 10:10:42 +0100 Subject: [PATCH 3/3] Clearer filter set, safer hook dependencies. Signed-off-by: Eric Peterson --- plugins/techdocs/src/reader/components/TechDocsSearch.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/techdocs/src/reader/components/TechDocsSearch.tsx b/plugins/techdocs/src/reader/components/TechDocsSearch.tsx index e45a26675b..6c0be16f4f 100644 --- a/plugins/techdocs/src/reader/components/TechDocsSearch.tsx +++ b/plugins/techdocs/src/reader/components/TechDocsSearch.tsx @@ -90,14 +90,17 @@ const TechDocsSearchBar = ({ // Update the filter context when the entityId changes, e.g. when the search // bar continues to be rendered, navigating between different TechDocs sites. + const { kind, name, namespace } = entityId; useEffect(() => { setFilters(prevFilters => { return { ...prevFilters, - ...entityId, + kind, + namespace, + name, }; }); - }, [entityId, setFilters]); + }, [kind, namespace, name, setFilters]); const handleQuery = (e: ChangeEvent) => { if (!open) {