diff --git a/plugins/search/src/apis.test.ts b/plugins/search/src/apis.test.ts new file mode 100644 index 0000000000..33714bc688 --- /dev/null +++ b/plugins/search/src/apis.test.ts @@ -0,0 +1,55 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 { CatalogApi } from '@backstage/plugin-catalog-react'; + +import { SearchClient } from './apis'; + +describe('apis', () => { + const query = { + term: '', + filters: {}, + types: [], + pageCursor: '', + }; + + const baseUrl = 'https://base-url.com/'; + const getBaseUrl = jest.fn().mockResolvedValue(baseUrl); + const client = new SearchClient({ + catalogApi: {} as CatalogApi, + discoveryApi: { getBaseUrl }, + }); + + const json = jest.fn(); + const originalFetch = window.fetch; + window.fetch = jest.fn().mockResolvedValue({ json }); + + afterAll(() => { + window.fetch = originalFetch; + }); + + it('Fetch is called with expected URL (including stringified Q params)', async () => { + await client._alphaPerformSearch(query); + expect(getBaseUrl).toHaveBeenLastCalledWith('search/query'); + expect(fetch).toHaveBeenLastCalledWith(`${baseUrl}?term=&pageCursor=`); + }); + + it('Resolves JSON from fetch response', async () => { + const result = { loading: false, error: '', value: {} }; + json.mockReturnValueOnce(result); + expect(await client._alphaPerformSearch(query)).toStrictEqual(result); + }); +});