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

[7.x] Add client side service mocks (#32999) #33420

Merged
merged 1 commit into from
Mar 18, 2019
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
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