Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Liza K committed Apr 12, 2020
1 parent 2b8610d commit 3ffe1d6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/

// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { coreMock } from '../../../../../../../src/core/public/mocks';
import { dataPluginMock } from '../../../../public/mocks';
import { coreMock } from '../../../../../../core/public/mocks';
import { dataPluginMock } from '../../../mocks';
import {
setFieldFormats,
setIndexPatterns,
Expand All @@ -29,7 +29,7 @@ import {
setQueryService,
setSearchService,
setUiSettings,
} from '../../../../public/services';
} from '../../../services';

/**
* Testing helper which calls all of the service setters used in the
Expand All @@ -49,4 +49,9 @@ export function mockDataServices() {
setQueryService(data.query);
setSearchService(data.search);
setUiSettings(core.uiSettings);

return {
core,
data,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ const searchMockResponse: any = Promise.resolve([]);
searchMockResponse.abort = jest.fn();
const searchMock = jest.fn().mockReturnValue(searchMockResponse);

const newSearchMockResponse: any = Promise.resolve([]);
newSearchMockResponse.abort = jest.fn();
const newSearchMock = jest.fn().mockReturnValue({
toPromise: () => searchMockResponse,
});

describe('defaultSearchStrategy', function() {
describe('search', function() {
let searchArgs: MockedKeys<Omit<SearchStrategySearchParams, 'config'>>;
Expand All @@ -58,7 +52,6 @@ describe('defaultSearchStrategy', function() {

const searchService = searchStartMock;
searchService.aggs.calculateAutoTimeExpression = jest.fn().mockReturnValue('1d');
searchService.search = newSearchMock;
searchService.__LEGACY.esClient.search = searchMock;
searchService.__LEGACY.esClient.msearch = msearchMock;

Expand Down Expand Up @@ -112,18 +105,5 @@ describe('defaultSearchStrategy', function() {
search({ ...searchArgs, config }).abort();
expect(msearchMockResponse.abort).toHaveBeenCalled();
});

test('should call new search service', () => {
const config = getConfigStub();
search({ ...searchArgs, config });
expect(newSearchMock).toHaveBeenCalledTimes(1);
});

test('should properly abort with new search service', async () => {
const abortSpy = jest.spyOn(AbortController.prototype, 'abort');
const config = getConfigStub({});
search({ ...searchArgs, config }).abort();
expect(abortSpy).toHaveBeenCalled();
});
});
});
57 changes: 55 additions & 2 deletions src/plugins/data/public/search/search_source/search_source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,28 @@
import { SearchSource } from './search_source';
import { IndexPattern, SortDirection } from '../..';
import { mockDataServices } from '../aggs/test_helpers';
import { setSearchService } from '../../services';
import { searchStartMock } from '../mocks';
import { fetchSoon } from '../legacy';
import { CoreStart } from 'kibana/public';

jest.mock('../fetch', () => ({
// Setup search service mock
searchStartMock.search = jest.fn(() => {
return {
toPromise: () => {
return new Promise(resolve => {
setTimeout(() => {
resolve({
rawResponse: '',
});
}, 100);
});
},
};
}) as any;
setSearchService(searchStartMock);

jest.mock('../legacy', () => ({
fetchSoon: jest.fn().mockResolvedValue({}),
}));

Expand All @@ -44,8 +64,11 @@ const indexPattern2 = ({
} as unknown) as IndexPattern;

describe('SearchSource', function() {
let uiSettingsMock: jest.Mocked<CoreStart['uiSettings']>;
beforeEach(() => {
mockDataServices();
const { core } = mockDataServices();
uiSettingsMock = core.uiSettings;
jest.clearAllMocks();
});

describe('#setField()', function() {
Expand Down Expand Up @@ -151,6 +174,36 @@ describe('SearchSource', function() {
});
});

describe('#legacy fetch()', () => {
beforeEach(() => {
uiSettingsMock.get.mockImplementation(() => {
return true; // batchSearches = true
});
});

afterEach(() => {
uiSettingsMock.get.mockImplementation(() => {
return false; // batchSearches = false
});
});

it('should call msearch', async () => {
const searchSource = new SearchSource({ index: indexPattern });
const options = {};
await searchSource.fetch(options);
expect(fetchSoon).toBeCalledTimes(1);
});
});

describe('#search service fetch()', () => {
it('should call msearch', async () => {
const searchSource = new SearchSource({ index: indexPattern });
const options = {};
await searchSource.fetch(options);
expect(searchStartMock.search).toBeCalledTimes(1);
});
});

describe('#serialize', function() {
it('should reference index patterns', () => {
const indexPattern123 = { id: '123' } as IndexPattern;
Expand Down

0 comments on commit 3ffe1d6

Please sign in to comment.