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
@@ -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';