refactor: move configApi to SearchContext
Signed-off-by: Enrico Alvarenga <enricomalvarenga@gmail.com>
This commit is contained in:
committed by
Camila Belo
parent
2ddf648a01
commit
40de4a7210
@@ -14,28 +14,32 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { useApi } from '@backstage/core-plugin-api';
|
||||
import { configApiRef } from '@backstage/core-plugin-api';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import { act, renderHook } from '@testing-library/react-hooks';
|
||||
import { MockConfigApi, TestApiProvider } from '@backstage/test-utils';
|
||||
import React from 'react';
|
||||
import {
|
||||
SearchContextProvider,
|
||||
useSearch,
|
||||
useSearchContextCheck,
|
||||
} from './SearchContext';
|
||||
|
||||
jest.mock('@backstage/core-plugin-api', () => ({
|
||||
...jest.requireActual('@backstage/core-plugin-api'),
|
||||
useApi: jest.fn(),
|
||||
}));
|
||||
import { searchApiRef } from '../api';
|
||||
|
||||
describe('SearchContext', () => {
|
||||
const query = jest.fn();
|
||||
|
||||
const wrapper = ({ children, initialState }: any) => (
|
||||
<SearchContextProvider initialState={initialState}>
|
||||
{children}
|
||||
</SearchContextProvider>
|
||||
const wrapper = ({ children, initialState, config = {} }: any) => (
|
||||
<TestApiProvider
|
||||
apis={[
|
||||
[configApiRef, new MockConfigApi(config)],
|
||||
[searchApiRef, { query }],
|
||||
]}
|
||||
>
|
||||
<SearchContextProvider initialState={initialState}>
|
||||
{children}
|
||||
</SearchContextProvider>
|
||||
</TestApiProvider>
|
||||
);
|
||||
|
||||
const initialState = {
|
||||
@@ -46,7 +50,6 @@ describe('SearchContext', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
query.mockResolvedValue({});
|
||||
(useApi as jest.Mock).mockReturnValue({ query: query });
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
@@ -56,11 +59,7 @@ describe('SearchContext', () => {
|
||||
it('Passes children', async () => {
|
||||
const text = 'text';
|
||||
|
||||
render(
|
||||
<SearchContextProvider initialState={initialState}>
|
||||
{text}
|
||||
</SearchContextProvider>,
|
||||
);
|
||||
render(wrapper({ children: text, initialState }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(text)).toBeInTheDocument();
|
||||
@@ -95,17 +94,61 @@ describe('SearchContext', () => {
|
||||
expect(result.current).toEqual(true);
|
||||
});
|
||||
|
||||
it('Uses initial state values', async () => {
|
||||
const { result, waitForNextUpdate } = renderHook(() => useSearch(), {
|
||||
wrapper,
|
||||
initialProps: {
|
||||
initialState,
|
||||
},
|
||||
describe('Uses initial state values', () => {
|
||||
it('Uses default initial state values', async () => {
|
||||
const { result, waitForNextUpdate } = renderHook(() => useSearch(), {
|
||||
wrapper,
|
||||
});
|
||||
|
||||
await waitForNextUpdate();
|
||||
|
||||
expect(result.current).toEqual(
|
||||
expect.objectContaining({
|
||||
term: '',
|
||||
types: [],
|
||||
filters: {},
|
||||
pageLimit: undefined,
|
||||
pageCursor: undefined,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
await waitForNextUpdate();
|
||||
it('Uses provided initial state values', async () => {
|
||||
const { result, waitForNextUpdate } = renderHook(() => useSearch(), {
|
||||
wrapper,
|
||||
initialProps: {
|
||||
initialState,
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.current).toEqual(expect.objectContaining(initialState));
|
||||
await waitForNextUpdate();
|
||||
|
||||
expect(result.current).toEqual(expect.objectContaining(initialState));
|
||||
});
|
||||
|
||||
it('Uses page limit provided via config api', async () => {
|
||||
const { result, waitForNextUpdate } = renderHook(() => useSearch(), {
|
||||
wrapper,
|
||||
initialProps: {
|
||||
initialState,
|
||||
config: {
|
||||
app: {
|
||||
search: {
|
||||
query: {
|
||||
pageLimit: 100,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await waitForNextUpdate();
|
||||
|
||||
expect(result.current).toEqual(
|
||||
expect.objectContaining({ ...initialState, pageLimit: 100 }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Resets cursor', () => {
|
||||
|
||||
@@ -30,7 +30,11 @@ import {
|
||||
createVersionedValueMap,
|
||||
} from '@backstage/version-bridge';
|
||||
import { JsonObject } from '@backstage/types';
|
||||
import { AnalyticsContext, useApi } from '@backstage/core-plugin-api';
|
||||
import {
|
||||
AnalyticsContext,
|
||||
useApi,
|
||||
configApiRef,
|
||||
} from '@backstage/core-plugin-api';
|
||||
import { SearchResultSet } from '@backstage/plugin-search-common';
|
||||
|
||||
import { searchApiRef } from '../api';
|
||||
@@ -95,25 +99,16 @@ export const useSearchContextCheck = () => {
|
||||
};
|
||||
|
||||
/**
|
||||
* @public
|
||||
* The initial state of `SearchContextProvider`.
|
||||
*
|
||||
* Constructs an object representing the initial state default
|
||||
* configuration for the SearchPage component
|
||||
*/
|
||||
export const getSearchContextInitialStateDefaults = (): SearchContextState => ({
|
||||
export const searchInitialState = {
|
||||
term: '',
|
||||
types: [],
|
||||
filters: {},
|
||||
pageLimit: undefined,
|
||||
pageCursor: undefined,
|
||||
});
|
||||
|
||||
/**
|
||||
* The initial state of `SearchContextProvider`.
|
||||
*
|
||||
*/
|
||||
const searchInitialState: SearchContextState =
|
||||
getSearchContextInitialStateDefaults();
|
||||
};
|
||||
|
||||
const useSearchContextValue = (
|
||||
initialValue: SearchContextState = searchInitialState,
|
||||
@@ -248,10 +243,20 @@ export const SearchContextProvider = (props: SearchContextProviderProps) => {
|
||||
const { initialState, inheritParentContextIfAvailable, children } = props;
|
||||
const hasParentContext = useSearchContextCheck();
|
||||
|
||||
const configApi = useApi(configApiRef);
|
||||
|
||||
const searchContextInitialState = {
|
||||
...searchInitialState,
|
||||
...(initialState || {}),
|
||||
pageLimit:
|
||||
configApi.getOptionalNumber('app.search.query.pageLimit') ||
|
||||
initialState?.pageLimit,
|
||||
};
|
||||
|
||||
return hasParentContext && inheritParentContextIfAvailable ? (
|
||||
<>{children}</>
|
||||
) : (
|
||||
<LocalSearchContext initialState={initialState}>
|
||||
<LocalSearchContext initialState={searchContextInitialState}>
|
||||
{children}
|
||||
</LocalSearchContext>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user