Skip to content

Commit

Permalink
add the provider field to GlobalSearchResult
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Jun 11, 2020
1 parent f593455 commit 257692e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 9 deletions.
4 changes: 4 additions & 0 deletions rfcs/text/0011_global_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ type GlobalSearchResult = Omit<GlobalSearchProviderResult, 'url'> & {
* 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;
};


Expand Down
17 changes: 14 additions & 3 deletions x-pack/plugins/global_search/common/process_result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const createResult = (parts: Partial<GlobalSearchProviderResult>): GlobalSearchP
...parts,
});

const provider = { id: 'provider' };

describe('processProviderResult', () => {
let basePath: jest.Mocked<IBasePath>;

Expand All @@ -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),
});
});

Expand All @@ -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');
});
});
2 changes: 2 additions & 0 deletions x-pack/plugins/global_search/common/process_result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
};
4 changes: 4 additions & 0 deletions x-pack/plugins/global_search/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export type GlobalSearchResult = Omit<GlobalSearchProviderResult, 'url'> & {
* 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;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('SearchService', () => {
type: 'test',
url: '/foo/bar',
score: 100,
provider: 'provider',
...parts,
id,
});
Expand Down Expand Up @@ -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',
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)))
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
)
);

Expand Down

0 comments on commit 257692e

Please sign in to comment.