From 257692e42b4580a9bff4e49b6df236f6d11e4c05 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Thu, 11 Jun 2020 11:55:50 +0200 Subject: [PATCH 1/2] add the `provider` field to GlobalSearchResult --- rfcs/text/0011_global_search.md | 4 ++++ .../global_search/common/process_result.test.ts | 17 ++++++++++++++--- .../global_search/common/process_result.ts | 2 ++ x-pack/plugins/global_search/common/types.ts | 4 ++++ .../public/services/search_service.test.ts | 3 +++ .../public/services/search_service.ts | 8 +++++--- .../server/services/search_service.test.ts | 2 ++ .../server/services/search_service.ts | 8 +++++--- 8 files changed, 39 insertions(+), 9 deletions(-) diff --git a/rfcs/text/0011_global_search.md b/rfcs/text/0011_global_search.md index 3b2120283d06a..a148f2888f611 100644 --- a/rfcs/text/0011_global_search.md +++ b/rfcs/text/0011_global_search.md @@ -206,6 +206,10 @@ type GlobalSearchResult = Omit & { * This can be either an absolute url, or a relative path including the basePath */ url: string; + /** + * The id of the provider this result originated from. + */ + provider: string; }; diff --git a/x-pack/plugins/global_search/common/process_result.test.ts b/x-pack/plugins/global_search/common/process_result.test.ts index 723f21a24f552..29c88b37eeaed 100644 --- a/x-pack/plugins/global_search/common/process_result.test.ts +++ b/x-pack/plugins/global_search/common/process_result.test.ts @@ -21,6 +21,8 @@ const createResult = (parts: Partial): GlobalSearchP ...parts, }); +const provider = { id: 'provider' }; + describe('processProviderResult', () => { let basePath: jest.Mocked; @@ -43,9 +45,10 @@ describe('processProviderResult', () => { meta: { hello: 'dolly' }, }); - expect(processProviderResult(r1, basePath)).toEqual({ + expect(processProviderResult(r1, provider, basePath)).toEqual({ ...r1, url: expect.any(String), + provider: expect.any(String), }); }); @@ -58,16 +61,24 @@ describe('processProviderResult', () => { expect(convertResultUrlMock).not.toHaveBeenCalled(); - const g1 = processProviderResult(r1, basePath); + const g1 = processProviderResult(r1, provider, basePath); expect(g1.url).toEqual('/url-A'); expect(convertResultUrlMock).toHaveBeenCalledTimes(1); expect(convertResultUrlMock).toHaveBeenCalledWith(r1.url, basePath); - const g2 = processProviderResult(r2, basePath); + const g2 = processProviderResult(r2, provider, basePath); expect(g2.url).toEqual('/url-B'); expect(convertResultUrlMock).toHaveBeenCalledTimes(2); expect(convertResultUrlMock).toHaveBeenCalledWith(r2.url, basePath); }); + + it('adds the `provider` field to the result', () => { + const g1 = processProviderResult(createResult({}), { id: 'providerA' }, basePath); + const g2 = processProviderResult(createResult({}), { id: 'providerB' }, basePath); + + expect(g1.provider).toEqual('providerA'); + expect(g2.provider).toEqual('providerB'); + }); }); diff --git a/x-pack/plugins/global_search/common/process_result.ts b/x-pack/plugins/global_search/common/process_result.ts index fed6dc14f066b..fd7e51551fefa 100644 --- a/x-pack/plugins/global_search/common/process_result.ts +++ b/x-pack/plugins/global_search/common/process_result.ts @@ -13,10 +13,12 @@ import { convertResultUrl, IBasePath } from './utils'; */ export const processProviderResult = ( result: GlobalSearchProviderResult, + provider: { id: string }, basePath: IBasePath ): GlobalSearchResult => { return { ...result, + provider: provider.id, url: convertResultUrl(result.url, basePath), }; }; diff --git a/x-pack/plugins/global_search/common/types.ts b/x-pack/plugins/global_search/common/types.ts index 26940806a4ecd..4ea24e7f8b917 100644 --- a/x-pack/plugins/global_search/common/types.ts +++ b/x-pack/plugins/global_search/common/types.ts @@ -74,6 +74,10 @@ export type GlobalSearchResult = Omit & { * This can be either an absolute url, or a relative path including the basePath */ url: string; + /** + * The id of the provider this result originated from. + */ + provider: string; }; /** diff --git a/x-pack/plugins/global_search/public/services/search_service.test.ts b/x-pack/plugins/global_search/public/services/search_service.test.ts index 350547a928fe4..1efde46618e08 100644 --- a/x-pack/plugins/global_search/public/services/search_service.test.ts +++ b/x-pack/plugins/global_search/public/services/search_service.test.ts @@ -73,6 +73,7 @@ describe('SearchService', () => { type: 'test', url: '/foo/bar', score: 100, + provider: 'provider', ...parts, id, }); @@ -397,10 +398,12 @@ describe('SearchService', () => { expect(batch.results).toHaveLength(2); expect(batch.results[0]).toEqual({ ...resultA, + provider: 'A', url: '/base-path/foo/bar', }); expect(batch.results[1]).toEqual({ ...resultB, + provider: 'A', url: '/foo', }); }); diff --git a/x-pack/plugins/global_search/public/services/search_service.ts b/x-pack/plugins/global_search/public/services/search_service.ts index 68970b75ad975..dff060956a31c 100644 --- a/x-pack/plugins/global_search/public/services/search_service.ts +++ b/x-pack/plugins/global_search/public/services/search_service.ts @@ -139,8 +139,10 @@ export class SearchService { aborted$, }; - const processResult = (result: GlobalSearchProviderResult) => - processProviderResult(result, this.http!.basePath); + const processResult = ( + result: GlobalSearchProviderResult, + provider: GlobalSearchResultProvider + ) => processProviderResult(result, provider, this.http!.basePath); const serverResults$ = fetchServerResults(this.http!, term, { preference, @@ -151,7 +153,7 @@ export class SearchService { provider.find(term, providerOptions).pipe( takeInArray(this.maxProviderResults), takeUntil(aborted$), - map((results) => results.map((r) => processResult(r))) + map((results) => results.map((r) => processResult(r, provider))) ) ); diff --git a/x-pack/plugins/global_search/server/services/search_service.test.ts b/x-pack/plugins/global_search/server/services/search_service.test.ts index fd705b4286680..7602dabbf3d20 100644 --- a/x-pack/plugins/global_search/server/services/search_service.test.ts +++ b/x-pack/plugins/global_search/server/services/search_service.test.ts @@ -283,10 +283,12 @@ describe('SearchService', () => { expect(batch.results).toHaveLength(2); expect(batch.results[0]).toEqual({ ...resultA, + provider: 'A', url: '/base-path/foo/bar', }); expect(batch.results[1]).toEqual({ ...resultB, + provider: 'A', url: '/foo', }); }); diff --git a/x-pack/plugins/global_search/server/services/search_service.ts b/x-pack/plugins/global_search/server/services/search_service.ts index 12eada2a1385e..39e876d7b61d2 100644 --- a/x-pack/plugins/global_search/server/services/search_service.ts +++ b/x-pack/plugins/global_search/server/services/search_service.ts @@ -142,14 +142,16 @@ export class SearchService { aborted$, }; - const processResult = (result: GlobalSearchProviderResult) => - processProviderResult(result, this.basePath!); + const processResult = ( + result: GlobalSearchProviderResult, + provider: GlobalSearchResultProvider + ) => processProviderResult(result, provider, this.basePath!); const providersResults$ = [...this.providers.values()].map((provider) => provider.find(term, providerOptions, context).pipe( takeInArray(this.maxProviderResults), takeUntil(aborted$), - map((results) => results.map((r) => processResult(r))) + map((results) => results.map((r) => processResult(r, provider))) ) ); From 3fb8b625713bced70827d012f8b1af5b5d34affe Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Thu, 11 Jun 2020 15:26:14 +0200 Subject: [PATCH 2/2] fix test types --- .../global_search/public/services/fetch_server_results.test.ts | 1 + .../global_search/server/routes/integration_tests/find.test.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/x-pack/plugins/global_search/public/services/fetch_server_results.test.ts b/x-pack/plugins/global_search/public/services/fetch_server_results.test.ts index f62acd08633ff..2b3966931bb1e 100644 --- a/x-pack/plugins/global_search/public/services/fetch_server_results.test.ts +++ b/x-pack/plugins/global_search/public/services/fetch_server_results.test.ts @@ -19,6 +19,7 @@ const createResult = (id: string, parts: Partial = {}): Glob title: id, type: 'type', url: `/path/to/${id}`, + provider: 'provider', score: 100, ...parts, }); diff --git a/x-pack/plugins/global_search/server/routes/integration_tests/find.test.ts b/x-pack/plugins/global_search/server/routes/integration_tests/find.test.ts index 878e4ac896b96..e3ae369a74966 100644 --- a/x-pack/plugins/global_search/server/routes/integration_tests/find.test.ts +++ b/x-pack/plugins/global_search/server/routes/integration_tests/find.test.ts @@ -21,6 +21,7 @@ const createResult = (id: string): GlobalSearchResult => ({ title: id, type: 'test', url: `/app/test/${id}`, + provider: 'provider', score: 42, });