introduce a new mock implementation of the search api, delete SearchContextProviderForStorybook and SearchApiProviderForStorybook

Signed-off-by: Emma Indal <emma.indahl@gmail.com>
This commit is contained in:
Emma Indal
2022-04-29 11:20:44 +02:00
parent 183948fa85
commit 30cf3bd830
5 changed files with 23 additions and 107 deletions
+9 -23
View File
@@ -5,31 +5,27 @@
```ts
import { ApiRef } from '@backstage/core-plugin-api';
import { AsyncState } from 'react-use/lib/useAsync';
import { ComponentProps } from 'react';
import { JsonObject } from '@backstage/types';
import { PropsWithChildren } from 'react';
import { default as React_2 } from 'react';
import { SearchQuery } from '@backstage/plugin-search-common';
import { SearchResultSet } from '@backstage/plugin-search-common';
// @public
export class MockSearchApi implements SearchApi {
constructor(mockedResults?: SearchResultSet | undefined);
// (undocumented)
mockedResults?: SearchResultSet | undefined;
// (undocumented)
query(): Promise<SearchResultSet>;
}
// @public (undocumented)
export interface SearchApi {
// (undocumented)
query(query: SearchQuery): Promise<SearchResultSet>;
}
// @public
export function SearchApiProviderForStorybook(
props: SearchApiProviderForStorybookProps,
): JSX.Element;
// @public
export type SearchApiProviderForStorybookProps = ComponentProps<
typeof SearchContextProvider
> & {
mockedResults?: SearchResultSet;
};
// @public (undocumented)
export const searchApiRef: ApiRef<SearchApi>;
@@ -38,16 +34,6 @@ export const SearchContextProvider: (
props: SearchContextProviderProps,
) => JSX.Element;
// @public
export const SearchContextProviderForStorybook: (
props: SearchContextProviderForStorybookProps,
) => JSX.Element;
// @public
export type SearchContextProviderForStorybookProps = PropsWithChildren<{
mockedResults?: SearchResultSet;
}>;
// @public
export type SearchContextProviderProps = PropsWithChildren<{
initialState?: SearchContextState;
+13
View File
@@ -30,3 +30,16 @@ export const searchApiRef = createApiRef<SearchApi>({
export interface SearchApi {
query(query: SearchQuery): Promise<SearchResultSet>;
}
/**
* @public
*
* Search Api Mock that can be used in tests and storybooks
*/
export class MockSearchApi implements SearchApi {
constructor(public mockedResults?: SearchResultSet) {}
query(): Promise<SearchResultSet> {
return Promise.resolve(this.mockedResults || { results: [] });
}
}
@@ -1,71 +0,0 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ApiProvider } from '@backstage/core-app-api';
import { SearchResultSet } from '@backstage/plugin-search-common';
import { TestApiRegistry } from '@backstage/test-utils';
import React, { ComponentProps, PropsWithChildren } from 'react';
import { searchApiRef } from '../api';
import { SearchContextProvider } from './SearchContext';
/**
* Props for {@link SearchApiProviderForStorybook}
* @public
*/
export type SearchApiProviderForStorybookProps = ComponentProps<
typeof SearchContextProvider
> & {
mockedResults?: SearchResultSet;
};
/**
* Props for {@link SearchContextProviderForStorybook}
* @public
*/
export type SearchContextProviderForStorybookProps = PropsWithChildren<{
mockedResults?: SearchResultSet;
}>;
/**
* Utility api provider only for use in Storybook stories.
*
* @public
*/
export function SearchApiProviderForStorybook(
props: SearchApiProviderForStorybookProps,
) {
const { mockedResults, children } = props;
const query: any = () => Promise.resolve(mockedResults || {});
const apiRegistry = TestApiRegistry.from([searchApiRef, { query }]);
return <ApiProvider apis={apiRegistry} children={children} />;
}
/**
* Utility context provider only for use in Storybook stories. You should use
* the real `<SearchContextProvider>` exported by `@backstage/plugin-search-react` in
* your app instead of this! In some cases (like the search page) it may
* already be provided on your behalf.
*
* @public
*/
export const SearchContextProviderForStorybook = (
props: SearchContextProviderForStorybookProps,
) => {
return (
<SearchApiProviderForStorybook {...props}>
<SearchContextProvider children={props.children} />
</SearchApiProviderForStorybook>
);
};
@@ -24,11 +24,3 @@ export type {
SearchContextState,
SearchContextValue,
} from './SearchContext';
export {
SearchContextProviderForStorybook,
SearchApiProviderForStorybook,
} from './SearchContextForStorybook.stories';
export type {
SearchContextProviderForStorybookProps,
SearchApiProviderForStorybookProps,
} from './SearchContextForStorybook.stories';
+1 -5
View File
@@ -20,19 +20,15 @@
* @packageDocumentation
*/
export { searchApiRef } from './api';
export { searchApiRef, MockSearchApi } from './api';
export type { SearchApi } from './api';
export {
SearchContextProvider,
useSearch,
useSearchContextCheck,
SearchContextProviderForStorybook,
SearchApiProviderForStorybook,
} from './context';
export type {
SearchContextProviderProps,
SearchContextState,
SearchContextValue,
SearchContextProviderForStorybookProps,
SearchApiProviderForStorybookProps,
} from './context';