searchApi._alphaPerformSearch -> searchApi.query

Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
Eric Peterson
2021-06-04 18:23:07 +02:00
parent 66507c5830
commit 19588d7d96
9 changed files with 33 additions and 68 deletions
-2
View File
@@ -30,8 +30,6 @@
},
"dependencies": {
"@backstage/core": "^0.7.11",
"@backstage/catalog-model": "^0.8.0",
"@backstage/plugin-catalog-react": "^0.2.0",
"@backstage/search-common": "^0.1.1",
"@backstage/config": "^0.1.5",
"@backstage/theme": "^0.2.8",
+2 -5
View File
@@ -14,8 +14,6 @@
* limitations under the License.
*/
import { CatalogApi } from '@backstage/plugin-catalog-react';
import { SearchClient } from './apis';
describe('apis', () => {
@@ -29,7 +27,6 @@ describe('apis', () => {
const baseUrl = 'https://base-url.com/';
const getBaseUrl = jest.fn().mockResolvedValue(baseUrl);
const client = new SearchClient({
catalogApi: {} as CatalogApi,
discoveryApi: { getBaseUrl },
});
@@ -42,7 +39,7 @@ describe('apis', () => {
});
it('Fetch is called with expected URL (including stringified Q params)', async () => {
await client._alphaPerformSearch(query);
await client.query(query);
expect(getBaseUrl).toHaveBeenLastCalledWith('search/query');
expect(fetch).toHaveBeenLastCalledWith(`${baseUrl}?term=&pageCursor=`);
});
@@ -50,6 +47,6 @@ describe('apis', () => {
it('Resolves JSON from fetch response', async () => {
const result = { loading: false, error: '', value: {} };
json.mockReturnValueOnce(result);
expect(await client._alphaPerformSearch(query)).toStrictEqual(result);
expect(await client.query(query)).toStrictEqual(result);
});
});
+3 -32
View File
@@ -15,9 +15,6 @@
*/
import { createApiRef, DiscoveryApi } from '@backstage/core';
import { Entity, ENTITY_DEFAULT_NAMESPACE } from '@backstage/catalog-model';
import { CatalogApi } from '@backstage/plugin-catalog-react';
import { SearchQuery, SearchResultSet } from '@backstage/search-common';
import qs from 'qs';
@@ -38,43 +35,17 @@ export type Result = {
export type SearchResults = Array<Result>;
export interface SearchApi {
getSearchResult(): Promise<SearchResults>;
_alphaPerformSearch(query: SearchQuery): Promise<SearchResultSet>;
query(query: SearchQuery): Promise<SearchResultSet>;
}
export class SearchClient implements SearchApi {
private readonly catalogApi: CatalogApi;
private readonly discoveryApi: DiscoveryApi;
constructor(options: { catalogApi: CatalogApi; discoveryApi: DiscoveryApi }) {
this.catalogApi = options.catalogApi;
constructor(options: { discoveryApi: DiscoveryApi }) {
this.discoveryApi = options.discoveryApi;
}
private async entities() {
const entities = await this.catalogApi.getEntities();
return entities.items.map((entity: Entity) => ({
name: entity.metadata.name,
description: entity.metadata.description,
owner:
typeof entity.spec?.owner === 'string' ? entity.spec?.owner : undefined,
kind: entity.kind,
lifecycle:
typeof entity.spec?.lifecycle === 'string'
? entity.spec?.lifecycle
: undefined,
url: `/catalog/${
entity.metadata.namespace?.toLowerCase() || ENTITY_DEFAULT_NAMESPACE
}/${entity.kind.toLowerCase()}/${entity.metadata.name}`,
}));
}
getSearchResult(): Promise<SearchResults> {
return this.entities();
}
// TODO: Productionalize as we implement search milestones.
async _alphaPerformSearch(query: SearchQuery): Promise<SearchResultSet> {
async query(query: SearchQuery): Promise<SearchResultSet> {
const queryString = qs.stringify(query);
const url = `${await this.discoveryApi.getBaseUrl(
'search/query',
@@ -38,8 +38,8 @@ describe('SearchBar', () => {
const name = 'Search term';
const term = 'term';
const _alphaPerformSearch = jest.fn().mockResolvedValue({});
(useApi as jest.Mock).mockReturnValue({ _alphaPerformSearch });
const query = jest.fn().mockResolvedValue({});
(useApi as jest.Mock).mockReturnValue({ query });
afterAll(() => {
jest.resetAllMocks();
@@ -86,7 +86,7 @@ describe('SearchBar', () => {
expect(textbox).toHaveValue(value);
});
expect(_alphaPerformSearch).toHaveBeenLastCalledWith(
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({ term: value }),
);
});
@@ -108,7 +108,7 @@ describe('SearchBar', () => {
expect(screen.getByRole('textbox', { name })).toHaveValue('');
});
expect(_alphaPerformSearch).toHaveBeenLastCalledWith(
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({ term: '' }),
);
});
@@ -134,7 +134,7 @@ describe('SearchBar', () => {
userEvent.type(textbox, value);
expect(_alphaPerformSearch).not.toHaveBeenLastCalledWith(
expect(query).not.toHaveBeenLastCalledWith(
expect.objectContaining({ term: value }),
);
@@ -146,7 +146,7 @@ describe('SearchBar', () => {
expect(textbox).toHaveValue(value);
});
expect(_alphaPerformSearch).toHaveBeenLastCalledWith(
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({ term: value }),
);
});
@@ -28,7 +28,7 @@ jest.mock('@backstage/core', () => ({
}));
describe('SearchContext', () => {
const _alphaPerformSearch = jest.fn();
const query = jest.fn();
const wrapper = ({ children, initialState }: any) => (
<SearchContextProvider initialState={initialState}>
@@ -44,8 +44,8 @@ describe('SearchContext', () => {
};
beforeEach(() => {
_alphaPerformSearch.mockResolvedValue({});
(useApi as jest.Mock).mockReturnValue({ _alphaPerformSearch });
query.mockResolvedValue({});
(useApi as jest.Mock).mockReturnValue({ query: query });
});
afterAll(() => {
@@ -138,7 +138,7 @@ describe('SearchContext', () => {
await waitForNextUpdate();
expect(_alphaPerformSearch).toHaveBeenLastCalledWith({
expect(query).toHaveBeenLastCalledWith({
...initialState,
term,
});
@@ -162,7 +162,7 @@ describe('SearchContext', () => {
await waitForNextUpdate();
expect(_alphaPerformSearch).toHaveBeenLastCalledWith({
expect(query).toHaveBeenLastCalledWith({
...initialState,
filters,
});
@@ -186,7 +186,7 @@ describe('SearchContext', () => {
await waitForNextUpdate();
expect(_alphaPerformSearch).toHaveBeenLastCalledWith({
expect(query).toHaveBeenLastCalledWith({
...initialState,
pageCursor,
});
@@ -210,7 +210,7 @@ describe('SearchContext', () => {
await waitForNextUpdate();
expect(_alphaPerformSearch).toHaveBeenLastCalledWith({
expect(query).toHaveBeenLastCalledWith({
...initialState,
types,
});
@@ -65,7 +65,7 @@ export const SearchContextProvider = ({
const result = useAsync(
() =>
searchApi._alphaPerformSearch({
searchApi.query({
term,
filters,
pageCursor,
@@ -39,8 +39,8 @@ describe('SearchFilter', () => {
const values = ['value1', 'value2'];
const filters = { unrelated: 'unrelated' };
const _alphaPerformSearch = jest.fn().mockResolvedValue({});
(useApi as jest.Mock).mockReturnValue({ _alphaPerformSearch });
const query = jest.fn().mockResolvedValue({});
(useApi as jest.Mock).mockReturnValue({ query: query });
afterAll(() => {
jest.resetAllMocks();
@@ -135,7 +135,7 @@ describe('SearchFilter', () => {
// Check the box.
userEvent.click(checkBox);
await waitFor(() => {
expect(_alphaPerformSearch).toHaveBeenLastCalledWith(
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({ filters: { field: [values[0]] } }),
);
});
@@ -143,7 +143,7 @@ describe('SearchFilter', () => {
// Uncheck the box.
userEvent.click(checkBox);
await waitFor(() => {
expect(_alphaPerformSearch).toHaveBeenLastCalledWith(
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({ filters: {} }),
);
});
@@ -165,7 +165,7 @@ describe('SearchFilter', () => {
// Check the box.
userEvent.click(checkBox);
await waitFor(() => {
expect(_alphaPerformSearch).toHaveBeenLastCalledWith(
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({
filters: { ...filters, field: [values[0]] },
}),
@@ -175,7 +175,7 @@ describe('SearchFilter', () => {
// Uncheck the box.
userEvent.click(checkBox);
await waitFor(() => {
expect(_alphaPerformSearch).toHaveBeenLastCalledWith(
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({ filters }),
);
});
@@ -299,7 +299,7 @@ describe('SearchFilter', () => {
userEvent.click(screen.getByRole('option', { name: values[0] }));
await waitFor(() => {
expect(_alphaPerformSearch).toHaveBeenLastCalledWith(
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({
filters: { [name]: values[0] },
}),
@@ -315,7 +315,7 @@ describe('SearchFilter', () => {
userEvent.click(screen.getByRole('option', { name: 'All' }));
await waitFor(() => {
expect(_alphaPerformSearch).toHaveBeenLastCalledWith(
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({
filters: {},
}),
@@ -350,7 +350,7 @@ describe('SearchFilter', () => {
userEvent.click(screen.getByRole('option', { name: values[0] }));
await waitFor(() => {
expect(_alphaPerformSearch).toHaveBeenLastCalledWith(
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({
filters: { ...filters, [name]: values[0] },
}),
@@ -366,7 +366,7 @@ describe('SearchFilter', () => {
userEvent.click(screen.getByRole('option', { name: 'All' }));
await waitFor(() => {
expect(_alphaPerformSearch).toHaveBeenLastCalledWith(
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({ filters }),
);
});
+1 -1
View File
@@ -14,7 +14,7 @@
* limitations under the License.
*/
// TODO: export searchApiRef from ./apis once interface is stable and settled.
export { searchApiRef } from './apis';
export {
searchPlugin,
searchPlugin as plugin,
+3 -4
View File
@@ -22,7 +22,6 @@ import {
createComponentExtension,
} from '@backstage/core';
import { SearchClient, searchApiRef } from './apis';
import { catalogApiRef } from '@backstage/plugin-catalog-react';
export const rootRouteRef = createRouteRef({
path: '/search',
@@ -39,9 +38,9 @@ export const searchPlugin = createPlugin({
apis: [
createApiFactory({
api: searchApiRef,
deps: { catalogApi: catalogApiRef, discoveryApi: discoveryApiRef },
factory: ({ catalogApi, discoveryApi }) => {
return new SearchClient({ catalogApi, discoveryApi });
deps: { discoveryApi: discoveryApiRef },
factory: ({ discoveryApi }) => {
return new SearchClient({ discoveryApi });
},
}),
],