Skip to content

Commit

Permalink
Merge pull request #12 from lukasolson/np-search-api
Browse files Browse the repository at this point in the history
Adding unit tests
  • Loading branch information
stacey-gammon authored Oct 3, 2019
2 parents 00a22d6 + ba50e95 commit 83eab32
Show file tree
Hide file tree
Showing 18 changed files with 351 additions and 42 deletions.
52 changes: 52 additions & 0 deletions src/plugins/search/public/es_search/es_search_strategy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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/public/mocks';
import { esSearchStrategyProvider } from './es_search_strategy';
import { CoreSetup } from 'kibana/public';
import { ES_SEARCH_STRATEGY } from '../../common/es_search';

describe('ES search strategy', () => {
let mockCoreSetup: MockedKeys<CoreSetup>;
const mockSearch = jest.fn();

beforeEach(() => {
mockCoreSetup = coreMock.createSetup();
mockSearch.mockClear();
});

it('returns a strategy with `search` that calls the sync search `search`', () => {
const request = { params: {} };
const options = {};

const esSearch = esSearchStrategyProvider(
{
core: mockCoreSetup,
},
mockSearch
);
esSearch.search(request, options);

expect(mockSearch.mock.calls[0][0]).toEqual({
...request,
serverStrategy: ES_SEARCH_STRATEGY,
});
expect(mockSearch.mock.calls[0][1]).toBe(options);
});
});
2 changes: 1 addition & 1 deletion src/plugins/search/public/es_search/es_search_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ES_SEARCH_STRATEGY, IEsSearchResponse } from '../../common';
import { SYNC_SEARCH_STRATEGY } from '../sync_search_strategy';
import { TSearchStrategyProvider, ISearchStrategy, ISearchGeneric, ISearchContext } from '..';

export const esClientSearchStrategyProvider: TSearchStrategyProvider<typeof ES_SEARCH_STRATEGY> = (
export const esSearchStrategyProvider: TSearchStrategyProvider<typeof ES_SEARCH_STRATEGY> = (
context: ISearchContext,
search: ISearchGeneric
): ISearchStrategy<typeof ES_SEARCH_STRATEGY> => {
Expand Down
43 changes: 43 additions & 0 deletions src/plugins/search/public/es_search/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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/public/mocks';
import { EsSearchService } from './plugin';
import { CoreSetup } from '../../../../core/public';
import { searchSetupMock } from '../mocks';

describe('ES search strategy service', () => {
let service: EsSearchService;
let mockCoreSetup: MockedKeys<CoreSetup>;
const opaqueId = Symbol();

beforeEach(() => {
service = new EsSearchService({ opaqueId });
mockCoreSetup = coreMock.createSetup();
});

describe('setup()', () => {
it('registers the ES search strategy', async () => {
service.setup(mockCoreSetup, {
search: searchSetupMock,
});
expect(searchSetupMock.registerSearchStrategyProvider).toBeCalled();
});
});
});
4 changes: 2 additions & 2 deletions src/plugins/search/public/es_search/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { Plugin, CoreSetup, PluginInitializerContext } from '../../../../core/public';
import { ES_SEARCH_STRATEGY } from '../../common/es_search';
import { esClientSearchStrategyProvider } from './es_search_strategy';
import { esSearchStrategyProvider } from './es_search_strategy';
import { ISearchSetup } from '../i_search_setup';

export interface IEsSearchSetupDependencies {
Expand All @@ -32,7 +32,7 @@ export class EsSearchService implements Plugin {
deps.search.registerSearchStrategyProvider(
this.initializerContext.opaqueId,
ES_SEARCH_STRATEGY,
esClientSearchStrategyProvider
esSearchStrategyProvider
);
}

Expand Down
4 changes: 0 additions & 4 deletions src/plugins/search/public/i_search_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,3 @@ export type TRegisterSearchStrategyProvider = <T extends TStrategyTypes>(
export type TSearchStrategiesMap = {
[K in TStrategyTypes]?: TSearchStrategyProviderEnhanced<K>;
};

export type TGetSearchStrategy = <K extends TStrategyTypes>(
strategyName: K
) => Promise<ISearchStrategy<K>>;
23 changes: 23 additions & 0 deletions src/plugins/search/public/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/

export const searchSetupMock = {
registerSearchStrategyContext: jest.fn(),
registerSearchStrategyProvider: jest.fn(),
};
19 changes: 0 additions & 19 deletions src/plugins/search/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import { coreMock } from '../../../core/public/mocks';

import { SearchPublicPlugin } from './plugin';
import { CoreSetup } from '../../../core/public';
import { SYNC_SEARCH_STRATEGY } from './sync_search_strategy';
import { ISearchAppMountContext } from './i_search_app_mount_context';

describe('Search service', () => {
let plugin: SearchPublicPlugin;
Expand All @@ -39,22 +37,5 @@ describe('Search service', () => {
expect(setup).toHaveProperty('registerSearchStrategyContext');
expect(setup).toHaveProperty('registerSearchStrategyProvider');
});

it('app mount', async () => {
plugin.setup(mockCoreSetup);

const mountContext = mockCoreSetup.application.registerMountContext.mock.calls[0][1]({
core: mockCoreSetup,
}) as ISearchAppMountContext;
// console.log(
// 'mockCoreSetup.application.registerMountContext.mock.calls[0][1]',
// mockCoreSetup.application.registerMountContext.mock.calls[0][1]()
// );
// console.log('mountContext', mountContext);

await mountContext.search({}, {}, SYNC_SEARCH_STRATEGY);

expect(mockCoreSetup.http.fetch.mock.calls[0]).toMatchInlineSnapshot(`undefined`);
});
});
});
56 changes: 56 additions & 0 deletions src/plugins/search/public/sync_search_strategy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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/public/mocks';
import { SYNC_SEARCH_STRATEGY, syncSearchStrategyProvider } from './sync_search_strategy';
import { CoreSetup } from '../../../core/public';

describe('Sync search strategy', () => {
let mockCoreSetup: MockedKeys<CoreSetup>;
const mockSearch = jest.fn();

beforeEach(() => {
mockCoreSetup = coreMock.createSetup();
});

it('returns a strategy with `search` that calls the backend API', () => {
mockCoreSetup.http.fetch.mockImplementationOnce(() => Promise.resolve());

const syncSearch = syncSearchStrategyProvider(
{
core: mockCoreSetup,
},
mockSearch
);
syncSearch.search(
{
serverStrategy: SYNC_SEARCH_STRATEGY,
},
{}
);
expect(mockCoreSetup.http.fetch.mock.calls[0][0]).toBe(`/api/search/${SYNC_SEARCH_STRATEGY}`);
expect(mockCoreSetup.http.fetch.mock.calls[0][1]).toEqual({
body: JSON.stringify({
serverStrategy: 'SYNC_SEARCH_STRATEGY',
}),
method: 'POST',
signal: undefined,
});
});
});
4 changes: 0 additions & 4 deletions src/plugins/search/public/sync_search_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ export interface ISyncSearchRequest extends IKibanaSearchRequest {
export const syncSearchStrategyProvider: TSearchStrategyProvider<typeof SYNC_SEARCH_STRATEGY> = (
context: ISearchContext
) => {
if (!context.core) {
throw new Error('core undefined!');
}

const search: ISearch<typeof SYNC_SEARCH_STRATEGY> = (
request: ISyncSearchRequest,
options: ISearchOptions
Expand Down
62 changes: 62 additions & 0 deletions src/plugins/search/server/create_api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 { createApi } from './create_api';

import { TSearchStrategiesMap } from './i_search_strategy';
import { IRouteHandlerSearchContext } from './i_route_handler_search_context';
import { DEFAULT_SEARCH_STRATEGY } from '../common';

// let mockCoreSetup: MockedKeys<CoreSetup>;

const mockDefaultSearch = jest.fn(() => Promise.resolve({ percentComplete: 0 }));
const mockDefaultSearchStrategyProvider = jest.fn(() =>
Promise.resolve({
search: mockDefaultSearch,
})
);
const mockStrategies: TSearchStrategiesMap = {
[DEFAULT_SEARCH_STRATEGY]: mockDefaultSearchStrategyProvider,
};

describe('createApi', () => {
let api: IRouteHandlerSearchContext;

beforeEach(() => {
api = createApi({
caller: jest.fn(),
searchStrategies: mockStrategies,
});
mockDefaultSearchStrategyProvider.mockClear();
});

it('should default to DEFAULT_SEARCH_STRATEGY if none is provided', async () => {
await api.search({
params: {},
});
expect(mockDefaultSearchStrategyProvider).toBeCalled();
expect(mockDefaultSearch).toBeCalled();
});

it('should throw if no provider is found for the given name', () => {
expect(api.search({}, 'noneByThisName')).rejects.toThrowErrorMatchingInlineSnapshot(
`"No strategy found for noneByThisName"`
);
});
});
3 changes: 0 additions & 3 deletions src/plugins/search/server/create_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ export function createApi({
}
// Give providers access to other search strategies by injecting this function
const strategy = await strategyProvider(caller, api.search);
if (!strategy) {
throw new Error(`No strategy named ${name}`);
}
return strategy.search(request);
},
};
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/search/server/es_search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
*/

import { PluginInitializerContext } from '../../../../core/server';
import { EsSearchServerPlugin } from './plugin';
import { EsSearchService } from './plugin';

export { ES_SEARCH_STRATEGY, IEsSearchRequest, IEsSearchResponse } from '../../common';

export function plugin(initializerContext: PluginInitializerContext) {
return new EsSearchServerPlugin(initializerContext);
return new EsSearchService(initializerContext);
}
2 changes: 1 addition & 1 deletion src/plugins/search/server/es_search/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface IEsSearchDependencies {
search: ISearchSetup;
}

export class EsSearchServerPlugin implements Plugin<void, void, IEsSearchDependencies> {
export class EsSearchService implements Plugin<void, void, IEsSearchDependencies> {
constructor(private initializerContext: PluginInitializerContext) {}

public setup(core: CoreSetup, deps: IEsSearchDependencies) {
Expand Down
4 changes: 0 additions & 4 deletions src/plugins/search/server/i_search_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,3 @@ export type TRegisterSearchStrategyProvider = <T extends TStrategyTypes>(
export type TSearchStrategiesMap = {
[K in TStrategyTypes]?: TSearchStrategyProviderEnhanced<K>;
};

export type TGettSearchStrategy = <K extends TStrategyTypes>(
strategyName: K
) => Promise<ISearchStrategy<K>>;
27 changes: 27 additions & 0 deletions src/plugins/search/server/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 { plugin } from '.';
import { coreMock } from '../../../core/server/mocks';

it('search service is instantiated', () => {
const context = coreMock.createPluginInitializerContext({});
const searchPlugin = plugin(context);
expect(searchPlugin).toBeDefined();
});
Loading

0 comments on commit 83eab32

Please sign in to comment.