Skip to content

Commit

Permalink
[Search][BUG] Call wrong search strategy recursively in async search (#…
Browse files Browse the repository at this point in the history
…69116)

* Call sync search recursively

* Fix test

* Fix search mock to avoid resetting it

* delete empty line

* fix tests
  • Loading branch information
lizozom authored Jun 15, 2020
1 parent 437f9f6 commit efe6ba4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/plugins/data/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { Plugin, IndexPatternsContract } from '.';
import { fieldFormatsServiceMock } from './field_formats/mocks';
import { searchSetupMock, searchStartMock } from './search/mocks';
import { searchServiceMock } from './search/mocks';
import { queryServiceMock } from './query/mocks';
import { AutocompleteStart, AutocompleteSetup } from './autocomplete';

Expand All @@ -41,7 +41,7 @@ const createSetupContract = (): Setup => {
const querySetupMock = queryServiceMock.createSetupContract();
return {
autocomplete: automcompleteSetupMock,
search: searchSetupMock,
search: searchServiceMock.createSetupContract(),
fieldFormats: fieldFormatsServiceMock.createSetupContract(),
query: querySetupMock,
};
Expand All @@ -55,7 +55,7 @@ const createStartContract = (): Start => {
createFiltersFromRangeSelectAction: jest.fn(),
},
autocomplete: autocompleteStartMock,
search: searchStartMock,
search: searchServiceMock.createStartContract(),
fieldFormats: fieldFormatsServiceMock.createStartContract(),
query: queryStartMock,
ui: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { IUiSettingsClient } from 'kibana/public';
import { defaultSearchStrategy } from './default_search_strategy';
import { searchStartMock } from '../mocks';
import { searchServiceMock } from '../mocks';
import { SearchStrategySearchParams } from './types';
import { UI_SETTINGS } from '../../../common';

Expand Down Expand Up @@ -51,7 +51,7 @@ describe('defaultSearchStrategy', function () {
searchMockResponse.abort.mockClear();
searchMock.mockClear();

const searchService = searchStartMock;
const searchService = searchServiceMock.createStartContract();
searchService.aggs.calculateAutoTimeExpression = jest.fn().mockReturnValue('1d');
searchService.__LEGACY.esClient.search = searchMock;
searchService.__LEGACY.esClient.msearch = msearchMock;
Expand Down
41 changes: 25 additions & 16 deletions src/plugins/data/public/search/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,32 @@ import { searchSourceMock, createSearchSourceMock } from './search_source/mocks'

export * from './search_source/mocks';

const searchSetupMock: jest.Mocked<ISearchSetup> = {
aggs: searchAggsSetupMock(),
registerSearchStrategy: jest.fn(),
};
function createSetupContract(): jest.Mocked<ISearchSetup> {
return {
aggs: searchAggsSetupMock(),
registerSearchStrategy: jest.fn(),
};
}

const searchStartMock: jest.Mocked<ISearchStart> = {
aggs: searchAggsStartMock(),
setInterceptor: jest.fn(),
getSearchStrategy: jest.fn(),
search: jest.fn(),
searchSource: searchSourceMock,
__LEGACY: {
esClient: {
search: jest.fn(),
msearch: jest.fn(),
function createStartContract(): jest.Mocked<ISearchStart> {
return {
aggs: searchAggsStartMock(),
setInterceptor: jest.fn(),
getSearchStrategy: jest.fn(),
search: jest.fn(),
searchSource: searchSourceMock,
__LEGACY: {
esClient: {
search: jest.fn(),
msearch: jest.fn(),
},
},
},
};
}

export const searchServiceMock = {
createSetupContract,
createStartContract,
};

export { searchSetupMock, searchStartMock, createSearchSourceMock };
export { createSearchSourceMock };
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ import { coreMock } from '../../../core/public/mocks';
import { IAggConfig, search } from '../../data/public';
// TODO: remove linting disable
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { searchStartMock } from '../../data/public/search/mocks';
import { searchServiceMock } from '../../data/public/search/mocks';

const { createAggConfigs } = searchStartMock.aggs;
const { createAggConfigs } = searchServiceMock.createStartContract().aggs;

const { tabifyAggResponse } = search;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Async search strategy', () => {
mockCoreSetup = coreMock.createSetup();
mockDataStart = dataPluginMock.createStartContract();
(mockDataStart.search.getSearchStrategy as jest.Mock).mockReturnValue({ search: mockSearch });

mockCoreSetup.getStartServices.mockResolvedValue([
undefined as any,
{ data: mockDataStart },
Expand Down Expand Up @@ -92,6 +93,7 @@ describe('Async search strategy', () => {

await asyncSearch.search(mockRequest, mockOptions).toPromise();

expect(mockDataStart.search.getSearchStrategy).toBeCalledTimes(1);
expect(mockSearch).toBeCalledTimes(2);
expect(mockSearch.mock.calls[0][0]).toEqual(mockRequest);
expect(mockSearch.mock.calls[1][0]).toEqual({ id: 1, serverStrategy: 'foo' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function asyncSearchStrategyProvider(
return timer(pollInterval).pipe(
// Send future requests using just the ID from the response
mergeMap(() => {
return search({ id, serverStrategy }, options);
return syncSearch.search({ id, serverStrategy }, options);
})
);
}),
Expand Down

0 comments on commit efe6ba4

Please sign in to comment.