From 19588d7d96fb48cdb8da39c59e97108fce4d82cb Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Fri, 4 Jun 2021 18:23:07 +0200 Subject: [PATCH] searchApi._alphaPerformSearch -> searchApi.query Signed-off-by: Eric Peterson --- plugins/search/package.json | 2 -- plugins/search/src/apis.test.ts | 7 ++-- plugins/search/src/apis.ts | 35 ++----------------- .../components/SearchBar/SearchBar.test.tsx | 12 +++---- .../SearchContext/SearchContext.test.tsx | 14 ++++---- .../SearchContext/SearchContext.tsx | 2 +- .../SearchFilter/SearchFilter.test.tsx | 20 +++++------ plugins/search/src/index.ts | 2 +- plugins/search/src/plugin.ts | 7 ++-- 9 files changed, 33 insertions(+), 68 deletions(-) diff --git a/plugins/search/package.json b/plugins/search/package.json index a74091e497..e434c6d830 100644 --- a/plugins/search/package.json +++ b/plugins/search/package.json @@ -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", diff --git a/plugins/search/src/apis.test.ts b/plugins/search/src/apis.test.ts index 32ec0f0d0e..64a5207d6d 100644 --- a/plugins/search/src/apis.test.ts +++ b/plugins/search/src/apis.test.ts @@ -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); }); }); diff --git a/plugins/search/src/apis.ts b/plugins/search/src/apis.ts index b15be349cf..4f0ab4a7f7 100644 --- a/plugins/search/src/apis.ts +++ b/plugins/search/src/apis.ts @@ -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; export interface SearchApi { - getSearchResult(): Promise; - _alphaPerformSearch(query: SearchQuery): Promise; + query(query: SearchQuery): Promise; } 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 { - return this.entities(); - } - - // TODO: Productionalize as we implement search milestones. - async _alphaPerformSearch(query: SearchQuery): Promise { + async query(query: SearchQuery): Promise { const queryString = qs.stringify(query); const url = `${await this.discoveryApi.getBaseUrl( 'search/query', diff --git a/plugins/search/src/components/SearchBar/SearchBar.test.tsx b/plugins/search/src/components/SearchBar/SearchBar.test.tsx index c4b4ea7f93..ca85275a5c 100644 --- a/plugins/search/src/components/SearchBar/SearchBar.test.tsx +++ b/plugins/search/src/components/SearchBar/SearchBar.test.tsx @@ -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 }), ); }); diff --git a/plugins/search/src/components/SearchContext/SearchContext.test.tsx b/plugins/search/src/components/SearchContext/SearchContext.test.tsx index 1b79e62698..e4394abe70 100644 --- a/plugins/search/src/components/SearchContext/SearchContext.test.tsx +++ b/plugins/search/src/components/SearchContext/SearchContext.test.tsx @@ -28,7 +28,7 @@ jest.mock('@backstage/core', () => ({ })); describe('SearchContext', () => { - const _alphaPerformSearch = jest.fn(); + const query = jest.fn(); const wrapper = ({ children, initialState }: any) => ( @@ -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, }); diff --git a/plugins/search/src/components/SearchContext/SearchContext.tsx b/plugins/search/src/components/SearchContext/SearchContext.tsx index 55ccd096a1..36a5ccde37 100644 --- a/plugins/search/src/components/SearchContext/SearchContext.tsx +++ b/plugins/search/src/components/SearchContext/SearchContext.tsx @@ -65,7 +65,7 @@ export const SearchContextProvider = ({ const result = useAsync( () => - searchApi._alphaPerformSearch({ + searchApi.query({ term, filters, pageCursor, diff --git a/plugins/search/src/components/SearchFilter/SearchFilter.test.tsx b/plugins/search/src/components/SearchFilter/SearchFilter.test.tsx index 4e721d1eb8..2742197d32 100644 --- a/plugins/search/src/components/SearchFilter/SearchFilter.test.tsx +++ b/plugins/search/src/components/SearchFilter/SearchFilter.test.tsx @@ -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 }), ); }); diff --git a/plugins/search/src/index.ts b/plugins/search/src/index.ts index 9cebd1bdd7..75868d12fd 100644 --- a/plugins/search/src/index.ts +++ b/plugins/search/src/index.ts @@ -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, diff --git a/plugins/search/src/plugin.ts b/plugins/search/src/plugin.ts index fb187d4840..45987a3749 100644 --- a/plugins/search/src/plugin.ts +++ b/plugins/search/src/plugin.ts @@ -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 }); }, }), ],