fix(HomePageSearchBar): don't require SearchContext

Signed-off-by: Phil Kuang <pkuang@factset.com>
This commit is contained in:
Phil Kuang
2022-04-21 23:53:02 -04:00
parent 9fbcc4b3b8
commit 11a46863de
8 changed files with 56 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-search': patch
---
Fix issue with `HomePageSearchBar` requiring `SearchContext`
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-search-react': patch
---
Export `useSearchContextCheck` hook to check if the search context is available
+3
View File
@@ -74,4 +74,7 @@ export type SearchContextValue = {
// @public
export const useSearch: () => SearchContextValue;
// @public
export const useSearchContextCheck: () => boolean;
```
@@ -18,7 +18,11 @@ import { useApi } from '@backstage/core-plugin-api';
import { render, screen, waitFor } from '@testing-library/react';
import { act, renderHook } from '@testing-library/react-hooks';
import React from 'react';
import { SearchContextProvider, useSearch } from './SearchContext';
import {
SearchContextProvider,
useSearch,
useSearchContextCheck,
} from './SearchContext';
jest.mock('@backstage/core-plugin-api', () => ({
...jest.requireActual('@backstage/core-plugin-api'),
@@ -71,6 +75,26 @@ describe('SearchContext', () => {
);
});
it('Checks whether context is set', async () => {
const hook = renderHook(() => useSearchContextCheck());
expect(hook.result.current).toEqual(false);
const { result, waitForNextUpdate } = renderHook(
() => useSearchContextCheck(),
{
wrapper,
initialProps: {
initialState,
},
},
);
await waitForNextUpdate();
expect(result.current).toEqual(true);
});
it('Uses initial state values', async () => {
const { result, waitForNextUpdate } = renderHook(() => useSearch(), {
wrapper,
@@ -79,6 +79,16 @@ export const useSearch = () => {
return value;
};
/**
* @public
*
* React hook which checks for an existing search context
*/
export const useSearchContextCheck = () => {
const context = useContext(SearchContext);
return context !== undefined;
};
/**
* The initial state of `SearchContextProvider`.
*
+5 -1
View File
@@ -14,7 +14,11 @@
* limitations under the License.
*/
export { SearchContextProvider, useSearch } from './SearchContext';
export {
SearchContextProvider,
useSearch,
useSearchContextCheck,
} from './SearchContext';
export type {
SearchContextProviderProps,
SearchContextState,
+1
View File
@@ -25,6 +25,7 @@ export type { SearchApi } from './api';
export {
SearchContextProvider,
useSearch,
useSearchContextCheck,
SearchContextProviderForStorybook,
SearchApiProviderForStorybook,
} from './context';
@@ -35,6 +35,7 @@ import ClearButton from '@material-ui/icons/Clear';
import {
SearchContextProvider,
useSearch,
useSearchContextCheck,
} from '@backstage/plugin-search-react';
import { TrackSearch } from '../SearchTracker';
@@ -72,7 +73,7 @@ export const SearchBarBase = ({
}: SearchBarBaseProps) => {
const configApi = useApi(configApiRef);
const [value, setValue] = useState<string>(defaultValue as string);
const hasSearchContext = useSearch();
const hasSearchContext = useSearchContextCheck();
useEffect(() => {
setValue(prevValue =>