Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More unit tests #13

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/plugins/data/server/search/es_search/es_search_service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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 { coreMock } from '../../../../../core/server/mocks';
import { EsSearchService } from './es_search_service';
import { PluginInitializerContext } from '../../../../../core/server';
import { searchSetupMock } from '../mocks';

describe('ES search strategy service', () => {
let service: EsSearchService;

const mockCoreSetup = coreMock.createSetup();
const opaqueId = Symbol();
const context: PluginInitializerContext = {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of this, but I didn't see a way to get a mock for this (I might not have been looking in the right places though).

opaqueId,
config: {
createIfExists: jest.fn(),
create: jest.fn(),
},
env: {
mode: {
dev: false,
name: 'development',
prod: false,
},
},
logger: {
get: jest.fn(),
},
};

beforeEach(() => {
service = new EsSearchService(context);
});

describe('setup()', () => {
it('registers the ES search strategy', async () => {
service.setup(mockCoreSetup, {
search: searchSetupMock,
});
expect(searchSetupMock.registerSearchStrategyProvider).toBeCalled();
});
});
});
102 changes: 102 additions & 0 deletions src/plugins/data/server/search/es_search/es_search_strategy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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 { coreMock } from '../../../../../core/server/mocks';
import { esSearchStrategyProvider } from './es_search_strategy';

describe('ES search strategy', () => {
const mockCoreSetup = coreMock.createSetup();
const mockApiCaller = jest.fn().mockResolvedValue({
_shards: {
total: 10,
failed: 1,
skipped: 2,
successful: 7,
},
});
const mockSearch = jest.fn();

beforeEach(() => {
mockApiCaller.mockClear();
mockSearch.mockClear();
});

it('returns a strategy with `search`', () => {
const esSearch = esSearchStrategyProvider(
{
core: mockCoreSetup,
},
mockApiCaller,
mockSearch
);

expect(typeof esSearch.search).toBe('function');
});

it('logs the response if `debug` is set to `true`', () => {
const spy = jest.spyOn(console, 'log');
const esSearch = esSearchStrategyProvider(
{
core: mockCoreSetup,
},
mockApiCaller,
mockSearch
);

expect(spy).not.toBeCalled();

esSearch.search({ params: {}, debug: true });

expect(spy).toBeCalled();
});

it('calls the API caller with the params', () => {
const params = { index: 'logstash-*' };
const esSearch = esSearchStrategyProvider(
{
core: mockCoreSetup,
},
mockApiCaller,
mockSearch
);

esSearch.search({ params });

expect(mockApiCaller).toBeCalled();
expect(mockApiCaller.mock.calls[0][0]).toBe('search');
expect(mockApiCaller.mock.calls[0][1]).toEqual(params);
});

it('returns total, loaded, and raw response', async () => {
const params = { index: 'logstash-*' };
const esSearch = esSearchStrategyProvider(
{
core: mockCoreSetup,
},
mockApiCaller,
mockSearch
);

const response = await esSearch.search({ params });

expect(response).toHaveProperty('total');
expect(response).toHaveProperty('loaded');
expect(response).toHaveProperty('rawResponse');
});
});
4 changes: 3 additions & 1 deletion src/plugins/data/server/search/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@
export const searchSetupMock = {
registerSearchStrategyContext: jest.fn(),
registerSearchStrategyProvider: jest.fn(),
__LEGACY: jest.fn(),
__LEGACY: {
search: jest.fn(),
},
};
99 changes: 99 additions & 0 deletions src/plugins/data/server/search/routes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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 { httpServiceMock, httpServerMock } from '../../../../../src/core/server/mocks';
import { registerSearchRoute } from './routes';
import { IRouter, ScopedClusterClient } from 'kibana/server';

describe('Search service', () => {
let routerMock: jest.Mocked<IRouter>;

beforeEach(() => {
routerMock = httpServiceMock.createRouter();
});

it('registers a post route', async () => {
registerSearchRoute(routerMock);
expect(routerMock.post).toBeCalled();
});

it('handler calls context.search.search with the given request and strategy', async () => {
const mockSearch = jest.fn().mockResolvedValue('yay');
const mockContext = {
core: {
elasticsearch: {
dataClient: {} as ScopedClusterClient,
adminClient: {} as ScopedClusterClient,
Comment on lines +41 to +42
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not a fan of this, but also didn't see a way to get a mock for this.

},
},
search: {
search: mockSearch,
},
};
const mockBody = { params: {} };
const mockParams = { strategy: 'foo' };
const mockRequest = httpServerMock.createKibanaRequest({
body: mockBody,
params: mockParams,
});
const mockResponse = httpServerMock.createResponseFactory();

registerSearchRoute(routerMock);
const handler = routerMock.post.mock.calls[0][1];
await handler(mockContext, mockRequest, mockResponse);

expect(mockSearch).toBeCalled();
expect(mockSearch.mock.calls[0][0]).toStrictEqual(mockBody);
expect(mockSearch.mock.calls[0][1]).toBe(mockParams.strategy);
expect(mockResponse.ok).toBeCalled();
expect(mockResponse.ok.mock.calls[0][0]).toEqual({ body: 'yay' });
});

it('handler throws internal error if the search throws an error', async () => {
const mockSearch = jest.fn().mockRejectedValue('oh no');
const mockContext = {
core: {
elasticsearch: {
dataClient: {} as ScopedClusterClient,
adminClient: {} as ScopedClusterClient,
},
},
search: {
search: mockSearch,
},
};
const mockBody = { params: {} };
const mockParams = { strategy: 'foo' };
const mockRequest = httpServerMock.createKibanaRequest({
body: mockBody,
params: mockParams,
});
const mockResponse = httpServerMock.createResponseFactory();

registerSearchRoute(routerMock);
const handler = routerMock.post.mock.calls[0][1];
await handler(mockContext, mockRequest, mockResponse);

expect(mockSearch).toBeCalled();
expect(mockSearch.mock.calls[0][0]).toStrictEqual(mockBody);
expect(mockSearch.mock.calls[0][1]).toBe(mockParams.strategy);
expect(mockResponse.internalError).toBeCalled();
expect(mockResponse.internalError.mock.calls[0][0]).toEqual({ body: 'oh no' });
});
});