Skip to content

Commit

Permalink
Add client side service mocks (#32999)
Browse files Browse the repository at this point in the history
* Add client side service mocks

* remove ab obsolete test snapshot

* put back accidentally removed type definition

* define MethodKeysOf, PublicMethodsOf on project level

* export src/core/public service mocks

* export src/core/server service mocks
  • Loading branch information
mshustov authored Mar 18, 2019
1 parent 4b2c2bd commit 03afe53
Show file tree
Hide file tree
Showing 29 changed files with 682 additions and 347 deletions.
45 changes: 45 additions & 0 deletions src/core/public/base_path/base_path_service.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 { BasePathService, BasePathStart } from './base_path_service';

const createStartContractMock = () => {
const startContract: jest.Mocked<BasePathStart> = {
get: jest.fn(),
addToPath: jest.fn(),
removeFromPath: jest.fn(),
};
startContract.get.mockReturnValue('get');
startContract.addToPath.mockReturnValue('addToPath');
startContract.removeFromPath.mockReturnValue('removeFromPath');
return startContract;
};

type BasePathServiceContract = PublicMethodsOf<BasePathService>;
const createMock = () => {
const mocked: jest.Mocked<BasePathServiceContract> = {
start: jest.fn(),
};
mocked.start.mockReturnValue(createStartContractMock());
return mocked;
};

export const basePathServiceMock = {
create: createMock,
createStartContract: createStartContractMock,
};
6 changes: 3 additions & 3 deletions src/core/public/base_path/base_path_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

import { injectedMetadataServiceMock } from '../injected_metadata/injected_metadata_service.mock';
import { BasePathService } from './base_path_service';

function setup(options: any = {}) {
Expand All @@ -25,9 +26,8 @@ function setup(options: any = {}) {

const service = new BasePathService();

const injectedMetadata = {
getBasePath: jest.fn().mockReturnValue(injectedBasePath),
} as any;
const injectedMetadata = injectedMetadataServiceMock.createStartContract();
injectedMetadata.getBasePath.mockReturnValue(injectedBasePath);

const start = service.start({
injectedMetadata,
Expand Down
60 changes: 60 additions & 0 deletions src/core/public/chrome/chrome_service.mock.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 { BehaviorSubject } from 'rxjs';
import { Brand, Breadcrumb, ChromeService, ChromeStart } from './chrome_service';

const createStartContractMock = () => {
const startContract: jest.Mocked<ChromeStart> = {
setBrand: jest.fn(),
getBrand$: jest.fn(),
setIsVisible: jest.fn(),
getIsVisible$: jest.fn(),
setIsCollapsed: jest.fn(),
getIsCollapsed$: jest.fn(),
addApplicationClass: jest.fn(),
removeApplicationClass: jest.fn(),
getApplicationClasses$: jest.fn(),
getBreadcrumbs$: jest.fn(),
setBreadcrumbs: jest.fn(),
getHelpExtension$: jest.fn(),
setHelpExtension: jest.fn(),
};
startContract.getBrand$.mockReturnValue(new BehaviorSubject({} as Brand));
startContract.getIsVisible$.mockReturnValue(new BehaviorSubject(false));
startContract.getIsCollapsed$.mockReturnValue(new BehaviorSubject(false));
startContract.getApplicationClasses$.mockReturnValue(new BehaviorSubject(['class-name']));
startContract.getBreadcrumbs$.mockReturnValue(new BehaviorSubject([{} as Breadcrumb]));
startContract.getHelpExtension$.mockReturnValue(new BehaviorSubject(undefined));
return startContract;
};

type ChromeServiceContract = PublicMethodsOf<ChromeService>;
const createMock = () => {
const mocked: jest.Mocked<ChromeServiceContract> = {
start: jest.fn(),
stop: jest.fn(),
};
mocked.start.mockReturnValue(createStartContractMock());
return mocked;
};

export const chromeServiceMock = {
create: createMock,
createStartContract: createStartContractMock,
};
21 changes: 5 additions & 16 deletions src/core/public/chrome/chrome_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import * as Rx from 'rxjs';
import { toArray } from 'rxjs/operators';

import { injectedMetadataServiceMock } from '../injected_metadata/injected_metadata_service.mock';
import { notificationServiceMock } from '../notifications/notifications_service.mock';

const store = new Map();
(window as any).localStorage = {
setItem: (key: string, value: string) => store.set(String(key), String(value)),
Expand All @@ -31,22 +34,8 @@ import { ChromeService } from './chrome_service';

function defaultStartDeps(): any {
return {
notifications: {
toasts: {
addWarning: jest.fn(),
},
},
injectedMetadata: {
injectedMetadataStart: true,
getCspConfig: jest.fn().mockReturnValue({ warnLegacyBrowsers: true }),
getKibanaVersion: jest.fn().mockReturnValue('kibanaVersion'),
getLegacyMetadata: jest.fn().mockReturnValue({
uiSettings: {
defaults: { legacyInjectedUiSettingDefaults: true },
user: { legacyInjectedUiSettingUserValues: true },
},
}),
},
notifications: notificationServiceMock.createStartContract(),
injectedMetadata: injectedMetadataServiceMock.createStartContract(),
};
}

Expand Down
Loading

0 comments on commit 03afe53

Please sign in to comment.