Mocks for CoreStart, CoreSetup and PluginInitializerContext (#39351) (#39894)

* Mocks for CoreStart, CoreSetup and PluginInitializerContext

* Public CoreStart, CoreSetup mocks

* Update api signature/docs

* Convert embaddable_api tests to new core mocks

* CR Feedback

* Introduce ui_new_platform.test.mocks and refactor embedabble tests

* Hack to get TS warnings for Core mocks

* Core mocks types cleanup & hack to get TS warnings for Server Core mocks

* Use __mocks__ new_platform

* Remove accidently commited auto-mock

* Introduce MockedKeys type for Core mocks

* Better typing/docs for UiSettings

* Revert "Use __mocks__ new_platform"

This reverts commit 2d666facc8.

* Add missing mock to test

* Cleanup UiSettings types
This commit is contained in:
Rudolf Meijering 2019-07-01 19:54:34 +02:00 committed by GitHub
parent 6991ebdc73
commit 0a469d2d96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
50 changed files with 259 additions and 158 deletions

View file

@ -140,11 +140,13 @@ module.exports = {
'src/core/public/**/*',
'!src/core/public/index.ts',
'!src/core/public/mocks.ts',
'!src/core/public/*.test.mocks.ts',
'!src/core/public/utils/**/*',
'src/core/server/**/*',
'!src/core/server/index.ts',
'!src/core/server/mocks.ts',
'!src/core/server/*.test.mocks.ts',
'src/plugins/**/public/**/*',
'!src/plugins/**/public/index*',

View file

@ -19,5 +19,5 @@ export interface CoreSetup
| [fatalErrors](./kibana-plugin-public.coresetup.fatalerrors.md) | <code>FatalErrorsSetup</code> | [FatalErrorsSetup](./kibana-plugin-public.fatalerrorssetup.md) |
| [http](./kibana-plugin-public.coresetup.http.md) | <code>HttpSetup</code> | [HttpSetup](./kibana-plugin-public.httpsetup.md) |
| [notifications](./kibana-plugin-public.coresetup.notifications.md) | <code>NotificationsSetup</code> | [NotificationsSetup](./kibana-plugin-public.notificationssetup.md) |
| [uiSettings](./kibana-plugin-public.coresetup.uisettings.md) | <code>UiSettingsSetup</code> | [UiSettingsSetup](./kibana-plugin-public.uisettingssetup.md) |
| [uiSettings](./kibana-plugin-public.coresetup.uisettings.md) | <code>UiSettingsClientContract</code> | [UiSettingsClient](./kibana-plugin-public.uisettingsclient.md) |

View file

@ -4,10 +4,10 @@
## CoreSetup.uiSettings property
[UiSettingsSetup](./kibana-plugin-public.uisettingssetup.md)
[UiSettingsClient](./kibana-plugin-public.uisettingsclient.md)
<b>Signature:</b>
```typescript
uiSettings: UiSettingsSetup;
uiSettings: UiSettingsClientContract;
```

View file

@ -23,5 +23,5 @@ export interface CoreStart
| [i18n](./kibana-plugin-public.corestart.i18n.md) | <code>I18nStart</code> | [I18nStart](./kibana-plugin-public.i18nstart.md) |
| [notifications](./kibana-plugin-public.corestart.notifications.md) | <code>NotificationsStart</code> | [NotificationsStart](./kibana-plugin-public.notificationsstart.md) |
| [overlays](./kibana-plugin-public.corestart.overlays.md) | <code>OverlayStart</code> | [OverlayStart](./kibana-plugin-public.overlaystart.md) |
| [uiSettings](./kibana-plugin-public.corestart.uisettings.md) | <code>UiSettingsStart</code> | [UiSettingsStart](./kibana-plugin-public.uisettingsstart.md) |
| [uiSettings](./kibana-plugin-public.corestart.uisettings.md) | <code>UiSettingsClientContract</code> | [UiSettingsClient](./kibana-plugin-public.uisettingsclient.md) |

View file

@ -4,10 +4,10 @@
## CoreStart.uiSettings property
[UiSettingsStart](./kibana-plugin-public.uisettingsstart.md)
[UiSettingsClient](./kibana-plugin-public.uisettingsclient.md)
<b>Signature:</b>
```typescript
uiSettings: UiSettingsStart;
uiSettings: UiSettingsClientContract;
```

View file

@ -63,6 +63,5 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [PluginInitializer](./kibana-plugin-public.plugininitializer.md) | The <code>plugin</code> export at the root of a plugin's <code>public</code> directory should conform to this interface. |
| [RecursiveReadonly](./kibana-plugin-public.recursivereadonly.md) | |
| [ToastInput](./kibana-plugin-public.toastinput.md) | |
| [UiSettingsSetup](./kibana-plugin-public.uisettingssetup.md) | |
| [UiSettingsStart](./kibana-plugin-public.uisettingsstart.md) | |
| [UiSettingsClientContract](./kibana-plugin-public.uisettingsclientcontract.md) | [UiSettingsClient](./kibana-plugin-public.uisettingsclient.md) |

View file

@ -10,7 +10,7 @@ Constructs a new instance of the `ToastsApi` class
```typescript
constructor(deps: {
uiSettings: UiSettingsSetup;
uiSettings: UiSettingsClientContract;
});
```
@ -18,5 +18,5 @@ constructor(deps: {
| Parameter | Type | Description |
| --- | --- | --- |
| deps | <code>{</code><br/><code> uiSettings: UiSettingsSetup;</code><br/><code> }</code> | |
| deps | <code>{</code><br/><code> uiSettings: UiSettingsClientContract;</code><br/><code> }</code> | |

View file

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-public](./kibana-plugin-public.md) &gt; [UiSettingsClientContract](./kibana-plugin-public.uisettingsclientcontract.md)
## UiSettingsClientContract type
[UiSettingsClient](./kibana-plugin-public.uisettingsclient.md)
<b>Signature:</b>
```typescript
export declare type UiSettingsClientContract = PublicMethodsOf<UiSettingsClient>;
```

View file

@ -1,12 +0,0 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-public](./kibana-plugin-public.md) &gt; [UiSettingsSetup](./kibana-plugin-public.uisettingssetup.md)
## UiSettingsSetup type
<b>Signature:</b>
```typescript
export declare type UiSettingsSetup = UiSettingsClient;
```

View file

@ -1,12 +0,0 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-public](./kibana-plugin-public.md) &gt; [UiSettingsStart](./kibana-plugin-public.uisettingsstart.md)
## UiSettingsStart type
<b>Signature:</b>
```typescript
export declare type UiSettingsStart = UiSettingsClient;
```

View file

@ -8,7 +8,7 @@
```typescript
config: {
create: <Schema>() => Observable<Schema>;
createIfExists: <Schema>() => Observable<Schema | undefined>;
create: <T = ConfigSchema>() => Observable<T>;
createIfExists: <T = ConfigSchema>() => Observable<T | undefined>;
};
```

View file

@ -9,14 +9,14 @@ Context that's available to plugins during initialization stage.
<b>Signature:</b>
```typescript
export interface PluginInitializerContext
export interface PluginInitializerContext<ConfigSchema = unknown>
```
## Properties
| Property | Type | Description |
| --- | --- | --- |
| [config](./kibana-plugin-server.plugininitializercontext.config.md) | <code>{</code><br/><code> create: &lt;Schema&gt;() =&gt; Observable&lt;Schema&gt;;</code><br/><code> createIfExists: &lt;Schema&gt;() =&gt; Observable&lt;Schema &#124; undefined&gt;;</code><br/><code> }</code> | |
| [config](./kibana-plugin-server.plugininitializercontext.config.md) | <code>{</code><br/><code> create: &lt;T = ConfigSchema&gt;() =&gt; Observable&lt;T&gt;;</code><br/><code> createIfExists: &lt;T = ConfigSchema&gt;() =&gt; Observable&lt;T &#124; undefined&gt;;</code><br/><code> }</code> | |
| [env](./kibana-plugin-server.plugininitializercontext.env.md) | <code>{</code><br/><code> mode: EnvironmentMode;</code><br/><code> }</code> | |
| [logger](./kibana-plugin-server.plugininitializercontext.logger.md) | <code>LoggerFactory</code> | |

View file

@ -63,7 +63,7 @@ import {
} from './notifications';
import { OverlayRef, OverlayStart } from './overlays';
import { Plugin, PluginInitializer, PluginInitializerContext } from './plugins';
import { UiSettingsClient, UiSettingsSetup, UiSettingsStart, UiSettingsState } from './ui_settings';
import { UiSettingsClient, UiSettingsState, UiSettingsClientContract } from './ui_settings';
import { ApplicationSetup, Capabilities, ApplicationStart } from './application';
import { DocLinksStart } from './doc_links';
@ -87,8 +87,8 @@ export interface CoreSetup {
http: HttpSetup;
/** {@link NotificationsSetup} */
notifications: NotificationsSetup;
/** {@link UiSettingsSetup} */
uiSettings: UiSettingsSetup;
/** {@link UiSettingsClient} */
uiSettings: UiSettingsClientContract;
}
/**
@ -115,8 +115,8 @@ export interface CoreStart {
notifications: NotificationsStart;
/** {@link OverlayStart} */
overlays: OverlayStart;
/** {@link UiSettingsStart} */
uiSettings: UiSettingsStart;
/** {@link UiSettingsClient} */
uiSettings: UiSettingsClientContract;
}
/** @internal */
@ -134,19 +134,10 @@ export interface InternalCoreStart extends CoreStart {
export {
ApplicationSetup,
ApplicationStart,
DocLinksStart,
HttpServiceBase,
HttpSetup,
HttpStart,
HttpInterceptor,
ErrorToastOptions,
FatalErrorsSetup,
FatalErrorInfo,
Capabilities,
ChromeStart,
ChromeBadge,
ChromeBreadcrumb,
ChromeBrand,
ChromeBreadcrumb,
ChromeHelpExtension,
ChromeNavControl,
ChromeNavControls,
@ -155,20 +146,28 @@ export {
ChromeNavLinkUpdateableFields,
ChromeRecentlyAccessed,
ChromeRecentlyAccessedHistoryItem,
ChromeStart,
DocLinksStart,
ErrorToastOptions,
FatalErrorInfo,
FatalErrorsSetup,
HttpInterceptor,
HttpServiceBase,
HttpSetup,
HttpStart,
I18nStart,
LegacyNavLink,
Plugin,
PluginInitializer,
PluginInitializerContext,
NotificationsSetup,
NotificationsStart,
OverlayRef,
OverlayStart,
Plugin,
PluginInitializer,
PluginInitializerContext,
Toast,
ToastInput,
ToastsApi,
UiSettingsClient,
UiSettingsClientContract,
UiSettingsState,
UiSettingsSetup,
UiSettingsStart,
};

View file

@ -16,6 +16,16 @@
* specific language governing permissions and limitations
* under the License.
*/
import { applicationServiceMock } from './application/application_service.mock';
import { chromeServiceMock } from './chrome/chrome_service.mock';
import { CoreSetup, CoreStart, PluginInitializerContext } from '.';
import { docLinksServiceMock } from './doc_links/doc_links_service.mock';
import { fatalErrorsServiceMock } from './fatal_errors/fatal_errors_service.mock';
import { httpServiceMock } from './http/http_service.mock';
import { i18nServiceMock } from './i18n/i18n_service.mock';
import { notificationServiceMock } from './notifications/notifications_service.mock';
import { overlayServiceMock } from './overlays/overlay_service.mock';
import { uiSettingsServiceMock } from './ui_settings/ui_settings_service.mock';
export { chromeServiceMock } from './chrome/chrome_service.mock';
export { docLinksServiceMock } from './doc_links/doc_links_service.mock';
@ -25,4 +35,37 @@ export { i18nServiceMock } from './i18n/i18n_service.mock';
export { injectedMetadataServiceMock } from './injected_metadata/injected_metadata_service.mock';
export { legacyPlatformServiceMock } from './legacy/legacy_service.mock';
export { notificationServiceMock } from './notifications/notifications_service.mock';
export { overlayServiceMock } from './overlays/overlay_service.mock';
export { uiSettingsServiceMock } from './ui_settings/ui_settings_service.mock';
function createCoreSetupMock() {
const mock: MockedKeys<CoreSetup> = {
fatalErrors: fatalErrorsServiceMock.createSetupContract(),
http: httpServiceMock.createSetupContract(),
notifications: notificationServiceMock.createSetupContract(),
uiSettings: uiSettingsServiceMock.createSetupContract(),
};
return mock;
}
function createCoreStartMock() {
const mock: MockedKeys<CoreStart> = {
application: applicationServiceMock.createStartContract(),
chrome: chromeServiceMock.createStartContract(),
docLinks: docLinksServiceMock.createStartContract(),
http: httpServiceMock.createStartContract(),
i18n: i18nServiceMock.createStartContract(),
notifications: notificationServiceMock.createStartContract(),
overlays: overlayServiceMock.createStartContract(),
uiSettings: uiSettingsServiceMock.createStartContract(),
};
return mock;
}
export const coreMock = {
createSetup: createCoreSetupMock,
createStart: createCoreStartMock,
createPluginInitializerContext: jest.fn() as jest.Mock<PluginInitializerContext>,
};

View file

@ -22,11 +22,11 @@ import { i18n } from '@kbn/i18n';
import { Subscription } from 'rxjs';
import { I18nStart } from '../i18n';
import { ToastsService, ToastsSetup, ToastsStart } from './toasts';
import { UiSettingsSetup } from '../ui_settings';
import { UiSettingsClientContract } from '../ui_settings';
import { OverlayStart } from '../overlays';
interface SetupDeps {
uiSettings: UiSettingsSetup;
uiSettings: UiSettingsClientContract;
}
interface StartDeps {

View file

@ -33,7 +33,7 @@ async function getCurrentToasts(toasts: ToastsApi) {
function uiSettingsMock() {
const mock = uiSettingsServiceMock.createSetupContract();
(mock.get as jest.Mock<typeof mock['get']>).mockImplementation(() => (config: string) => {
mock.get.mockImplementation(() => (config: string) => {
switch (config) {
case 'notifications:lifetime:info':
return 5000;

View file

@ -22,7 +22,7 @@ import React from 'react';
import * as Rx from 'rxjs';
import { ErrorToast } from './error_toast';
import { UiSettingsSetup } from '../../ui_settings';
import { UiSettingsClientContract } from '../../ui_settings';
import { OverlayStart } from '../../overlays';
type ToastInputFields = Pick<Toast, Exclude<keyof Toast, 'id'>>;
@ -58,11 +58,11 @@ const normalizeToast = (toastOrTitle: ToastInput) => {
export class ToastsApi {
private toasts$ = new Rx.BehaviorSubject<Toast[]>([]);
private idCounter = 0;
private uiSettings: UiSettingsSetup;
private uiSettings: UiSettingsClientContract;
private overlays?: OverlayStart;
constructor(deps: { uiSettings: UiSettingsSetup }) {
constructor(deps: { uiSettings: UiSettingsClientContract }) {
this.uiSettings = deps.uiSettings;
}

View file

@ -22,13 +22,13 @@ import { render, unmountComponentAtNode } from 'react-dom';
import { EuiGlobalToastListToast as Toast } from '@elastic/eui';
import { I18nStart } from '../../i18n';
import { UiSettingsSetup } from '../../ui_settings';
import { UiSettingsClientContract } from '../../ui_settings';
import { GlobalToastList } from './global_toast_list';
import { ToastsApi } from './toasts_api';
import { OverlayStart } from '../../overlays';
interface SetupDeps {
uiSettings: UiSettingsSetup;
uiSettings: UiSettingsClientContract;
}
interface StartDeps {

View file

@ -23,6 +23,10 @@ const createStartContractMock = () => {
openFlyout: jest.fn(),
openModal: jest.fn(),
};
startContract.openModal.mockReturnValue({
close: jest.fn(),
onClose: Promise.resolve(),
});
return startContract;
};

View file

@ -41,7 +41,6 @@ import { fatalErrorsServiceMock } from '../fatal_errors/fatal_errors_service.moc
import { uiSettingsServiceMock } from '../ui_settings/ui_settings_service.mock';
import { injectedMetadataServiceMock } from '../injected_metadata/injected_metadata_service.mock';
import { httpServiceMock } from '../http/http_service.mock';
import { UiSettingsClient } from '../ui_settings';
import { CoreSetup, CoreStart } from '..';
import { docLinksServiceMock } from '../doc_links/doc_links_service.mock';
@ -77,7 +76,7 @@ beforeEach(() => {
fatalErrors: fatalErrorsServiceMock.createSetupContract(),
http: httpServiceMock.createSetupContract(),
notifications: notificationServiceMock.createSetupContract(),
uiSettings: uiSettingsServiceMock.createSetupContract() as jest.Mocked<UiSettingsClient>,
uiSettings: uiSettingsServiceMock.createSetupContract(),
};
mockSetupContext = omit(mockSetupDeps, 'application', 'injectedMetadata');
mockStartDeps = {
@ -89,7 +88,7 @@ beforeEach(() => {
injectedMetadata: injectedMetadataServiceMock.createStartContract(),
notifications: notificationServiceMock.createStartContract(),
overlays: overlayServiceMock.createStartContract(),
uiSettings: uiSettingsServiceMock.createStartContract() as jest.Mocked<UiSettingsClient>,
uiSettings: uiSettingsServiceMock.createStartContract(),
};
mockStartContext = {
...omit(mockStartDeps, 'injectedMetadata'),

View file

@ -179,7 +179,7 @@ export interface CoreSetup {
// (undocumented)
notifications: NotificationsSetup;
// (undocumented)
uiSettings: UiSettingsSetup;
uiSettings: UiSettingsClientContract;
}
// @public
@ -199,7 +199,7 @@ export interface CoreStart {
// (undocumented)
overlays: OverlayStart;
// (undocumented)
uiSettings: UiSettingsStart;
uiSettings: UiSettingsClientContract;
}
// @internal
@ -508,7 +508,7 @@ export type ToastInput = string | ToastInputFields | Promise<ToastInputFields>;
// @public (undocumented)
export class ToastsApi {
constructor(deps: {
uiSettings: UiSettingsSetup;
uiSettings: UiSettingsClientContract;
});
// (undocumented)
add(toastOrTitle: ToastInput): Toast;
@ -557,10 +557,7 @@ export class UiSettingsClient {
}
// @public (undocumented)
export type UiSettingsSetup = UiSettingsClient;
// @public (undocumented)
export type UiSettingsStart = UiSettingsClient;
export type UiSettingsClientContract = PublicMethodsOf<UiSettingsClient>;
// @public (undocumented)
export interface UiSettingsState {

View file

@ -0,0 +1,30 @@
/*
* 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 './mocks';
export const coreSetupMock = coreMock.createSetup();
export const coreStartMock = coreMock.createStart();
jest.doMock('ui/new_platform', () => {
return {
npStart: { core: coreStartMock },
npSetup: { core: coreSetupMock },
};
});

View file

@ -17,6 +17,6 @@
* under the License.
*/
export { UiSettingsService, UiSettingsSetup, UiSettingsStart } from './ui_settings_service';
export { UiSettingsClient } from './ui_settings_client';
export { UiSettingsService } from './ui_settings_service';
export { UiSettingsClient, UiSettingsClientContract } from './ui_settings_client';
export { UiSettingsState } from './types';

View file

@ -31,6 +31,12 @@ interface UiSettingsClientParams {
initialSettings?: UiSettingsState;
}
/**
* {@link UiSettingsClient}
* @public
*/
export type UiSettingsClientContract = PublicMethodsOf<UiSettingsClient>;
/** @public */
export class UiSettingsClient {
private readonly update$ = new Rx.Subject<{ key: string; newValue: any; oldValue: any }>();

View file

@ -17,10 +17,10 @@
* under the License.
*/
import * as Rx from 'rxjs';
import { UiSettingsService, UiSettingsSetup } from './ui_settings_service';
import { UiSettingsService, UiSettingsClientContract } from './';
const createSetupContractMock = () => {
const setupContract: jest.Mocked<PublicMethodsOf<UiSettingsSetup>> = {
const setupContract: jest.Mocked<UiSettingsClientContract> = {
getAll: jest.fn(),
get: jest.fn(),
get$: jest.fn(),
@ -41,8 +41,7 @@ const createSetupContractMock = () => {
setupContract.getSaved$.mockReturnValue(new Rx.Subject<any>());
setupContract.getUpdateErrors$.mockReturnValue(new Rx.Subject<any>());
// we have to suppress type errors until decide how to mock es6 class
return (setupContract as unknown) as UiSettingsSetup;
return setupContract;
};
type UiSettingsServiceContract = PublicMethodsOf<UiSettingsService>;

View file

@ -21,7 +21,7 @@ import { HttpSetup } from '../http';
import { InjectedMetadataSetup } from '../injected_metadata';
import { UiSettingsApi } from './ui_settings_api';
import { UiSettingsClient } from './ui_settings_client';
import { UiSettingsClient, UiSettingsClientContract } from './ui_settings_client';
interface UiSettingsServiceDeps {
http: HttpSetup;
@ -33,7 +33,7 @@ export class UiSettingsService {
private uiSettingsApi?: UiSettingsApi;
private uiSettingsClient?: UiSettingsClient;
public setup({ http, injectedMetadata }: UiSettingsServiceDeps): UiSettingsSetup {
public setup({ http, injectedMetadata }: UiSettingsServiceDeps): UiSettingsClientContract {
this.uiSettingsApi = new UiSettingsApi(http);
http.addLoadingCount(this.uiSettingsApi.getLoadingCount$());
@ -49,7 +49,7 @@ export class UiSettingsService {
return this.uiSettingsClient;
}
public start(): UiSettingsStart {
public start(): UiSettingsClientContract {
return this.uiSettingsClient!;
}
@ -63,9 +63,3 @@ export class UiSettingsService {
}
}
}
/** @public */
export type UiSettingsSetup = UiSettingsClient;
/** @public */
export type UiSettingsStart = UiSettingsClient;

View file

@ -73,4 +73,5 @@ const createHttpServiceMock = () => {
export const httpServiceMock = {
create: createHttpServiceMock,
createSetupContract: createSetupContractMock,
createStartContract: createStartContractMock,
};

View file

@ -16,9 +16,60 @@
* specific language governing permissions and limitations
* under the License.
*/
import { of } from 'rxjs';
import { PluginInitializerContext, CoreSetup, CoreStart } from '.';
import { loggingServiceMock } from './logging/logging_service.mock';
import { elasticsearchServiceMock } from './elasticsearch/elasticsearch_service.mock';
import { httpServiceMock } from './http/http_service.mock';
export { configServiceMock } from './config/config_service.mock';
export { elasticsearchServiceMock } from './elasticsearch/elasticsearch_service.mock';
export { httpServiceMock } from './http/http_service.mock';
export { loggingServiceMock } from './logging/logging_service.mock';
export { SavedObjectsClientMock } from './saved_objects/service/saved_objects_client.mock';
export function pluginInitializerContextConfigMock<T>(config: T) {
const mock: jest.Mocked<PluginInitializerContext<T>['config']> = {
create: jest.fn().mockReturnValue(of(config)),
createIfExists: jest.fn().mockReturnValue(of(config)),
};
return mock;
}
function pluginInitializerContextMock<T>(config: T) {
const mock: jest.Mocked<PluginInitializerContext<T>> = {
logger: loggingServiceMock.create(),
env: {
mode: {
dev: true,
name: 'development',
prod: false,
},
},
config: pluginInitializerContextConfigMock<T>(config),
};
return mock;
}
function createCoreSetupMock() {
const mock: MockedKeys<CoreSetup> = {
elasticsearch: elasticsearchServiceMock.createSetupContract(),
http: httpServiceMock.createSetupContract(),
};
return mock;
}
function createCoreStartMock() {
const mock: MockedKeys<CoreStart> = {};
return mock;
}
export const coreMock = {
createSetup: createCoreSetupMock,
createStart: createCoreStartMock,
createPluginInitializerContext: pluginInitializerContextMock,
};

View file

@ -30,12 +30,12 @@ import { CoreSetup, CoreStart } from '..';
*
* @public
*/
export interface PluginInitializerContext {
export interface PluginInitializerContext<ConfigSchema = unknown> {
env: { mode: EnvironmentMode };
logger: LoggerFactory;
config: {
create: <Schema>() => Observable<Schema>;
createIfExists: <Schema>() => Observable<Schema | undefined>;
create: <T = ConfigSchema>() => Observable<T>;
createIfExists: <T = ConfigSchema>() => Observable<T | undefined>;
};
}

View file

@ -326,11 +326,11 @@ export interface Plugin<TSetup, TStart, TPluginsSetup extends Record<PluginName,
export type PluginInitializer<TSetup, TStart, TPluginsSetup extends Record<PluginName, unknown> = {}, TPluginsStart extends Record<PluginName, unknown> = {}> = (core: PluginInitializerContext) => Plugin<TSetup, TStart, TPluginsSetup, TPluginsStart>;
// @public
export interface PluginInitializerContext {
export interface PluginInitializerContext<ConfigSchema = unknown> {
// (undocumented)
config: {
create: <Schema>() => Observable<Schema>;
createIfExists: <Schema>() => Observable<Schema | undefined>;
create: <T = ConfigSchema>() => Observable<T>;
createIfExists: <T = ConfigSchema>() => Observable<T | undefined>;
};
// (undocumented)
env: {

View file

@ -17,7 +17,8 @@
* under the License.
*/
import '../np_core.test.mocks';
import '../ui_capabilities.test.mocks';
import '../../../../../core/public/ui_new_platform.test.mocks';
import { HelloWorldAction, SayHelloAction, EmptyEmbeddable } from '../test_samples/index';

View file

@ -17,7 +17,8 @@
* under the License.
*/
import '../np_core.test.mocks';
import '../ui_capabilities.test.mocks';
import '../../../../../core/public/ui_new_platform.test.mocks';
import {
FilterableEmbeddable,
FilterableEmbeddableFactory,

View file

@ -17,7 +17,8 @@
* under the License.
*/
import '../np_core.test.mocks';
import '../ui_capabilities.test.mocks';
import '../../../../../core/public/ui_new_platform.test.mocks';
import * as Rx from 'rxjs';
import { skip } from 'rxjs/operators';

View file

@ -17,7 +17,8 @@
* under the License.
*/
import '../np_core.test.mocks';
import '../ui_capabilities.test.mocks';
import '../../../../../core/public/ui_new_platform.test.mocks';
import {
ContactCardEmbeddable,
ContactCardEmbeddableInput,

View file

@ -19,7 +19,8 @@
/* eslint-disable max-classes-per-file */
import '../np_core.test.mocks';
import '../ui_capabilities.test.mocks';
import '../../../../../core/public/ui_new_platform.test.mocks';
import { skip } from 'rxjs/operators';
import { ContactCardEmbeddable, FilterableEmbeddable } from '../test_samples/index';

View file

@ -17,7 +17,8 @@
* under the License.
*/
import './np_core.test.mocks';
import './ui_capabilities.test.mocks';
import '../../../../core/public/ui_new_platform.test.mocks';
import {
HelloWorldAction,

View file

@ -17,7 +17,8 @@
* under the License.
*/
import '../np_core.test.mocks';
import '../ui_capabilities.test.mocks';
import '../../../../../core/public/ui_new_platform.test.mocks';
import React from 'react';

View file

@ -17,7 +17,8 @@
* under the License.
*/
import '../../../../np_core.test.mocks';
import '../../../../ui_capabilities.test.mocks';
import '../../../../../../../../core/public/ui_new_platform.test.mocks';
import {
FilterableContainer,

View file

@ -16,8 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/
import { getModalContents } from '../../../../np_core.test.mocks';
import '../../../../ui_capabilities.test.mocks';
import { coreStartMock } from '../../../../../../../../core/public/ui_new_platform.test.mocks';
import React from 'react';
import {
@ -79,7 +79,8 @@ test('create new calls factory.adds a panel to the container', async done => {
await nextTick();
(getModalContents().props as ContactCardInitializerProps).onCreate({
const overlayMock = coreStartMock.overlays;
((overlayMock.openModal.mock.calls[0][0] as any).props as ContactCardInitializerProps).onCreate({
firstName: 'Dany',
lastName: 'Targaryan',
});

View file

@ -17,7 +17,8 @@
* under the License.
*/
import '../../../../np_core.test.mocks';
import '../../../../ui_capabilities.test.mocks';
import '../../../../../../../../core/public/ui_new_platform.test.mocks';
import {
CONTACT_CARD_EMBEDDABLE,

View file

@ -17,7 +17,8 @@
* under the License.
*/
import '../../../../np_core.test.mocks';
import '../../../../ui_capabilities.test.mocks';
import '../../../../../../../../core/public/ui_new_platform.test.mocks';
import React from 'react';
import {

View file

@ -17,7 +17,8 @@
* under the License.
*/
import '../../../np_core.test.mocks';
import '../../../ui_capabilities.test.mocks';
import '../../../../../../../core/public/ui_new_platform.test.mocks';
import { EmbeddableInput } from '../../../embeddables/i_embeddable';
import { Embeddable } from '../../../embeddables/embeddable';

View file

@ -17,7 +17,8 @@
* under the License.
*/
import '../../../np_core.test.mocks';
import '../../../ui_capabilities.test.mocks';
import '../../../../../../../core/public/ui_new_platform.test.mocks';
jest.mock('ui/inspector', () => ({
Inspector: {

View file

@ -17,7 +17,8 @@
* under the License.
*/
import '../../../np_core.test.mocks';
import '../../../ui_capabilities.test.mocks';
import '../../../../../../../core/public/ui_new_platform.test.mocks';
import {
FilterableContainer,

View file

@ -17,7 +17,8 @@
* under the License.
*/
import '../np_core.test.mocks';
import '../ui_capabilities.test.mocks';
import '../../../../../core/public/ui_new_platform.test.mocks';
import { EuiContextMenuPanelDescriptor } from '@elastic/eui';

View file

@ -17,7 +17,8 @@
* under the License.
*/
import '../np_core.test.mocks';
import '../ui_capabilities.test.mocks';
import '../../../../../core/public/ui_new_platform.test.mocks';
import { triggerRegistry } from '../triggers';
import { HELLO_WORLD_ACTION_ID } from '../test_samples';

View file

@ -17,36 +17,6 @@
* under the License.
*/
import { fatalErrorsServiceMock, notificationServiceMock } from '../../../../core/public/mocks';
let modalContents: React.Component;
export const getModalContents = () => modalContents;
jest.doMock('ui/new_platform', () => {
return {
npStart: {
core: {
overlays: {
openFlyout: jest.fn(),
openModal: (component: React.Component) => {
modalContents = component;
return {
close: jest.fn(),
};
},
},
},
},
npSetup: {
core: {
fatalErrors: fatalErrorsServiceMock.createSetupContract(),
notifications: notificationServiceMock.createSetupContract(),
},
},
};
});
jest.doMock('ui/capabilities', () => ({
uiCapabilities: {
visualize: {

View file

@ -24,20 +24,18 @@ import { ToastNotifications } from './toast_notifications';
function toastDeps() {
const uiSettingsMock = uiSettingsServiceMock.createSetupContract();
(uiSettingsMock.get as jest.Mock<typeof uiSettingsMock['get']>).mockImplementation(
() => (config: string) => {
switch (config) {
case 'notifications:lifetime:info':
return 5000;
case 'notifications:lifetime:warning':
return 10000;
case 'notification:lifetime:error':
return 30000;
default:
throw new Error(`Accessing ${config} is not supported in the mock.`);
}
uiSettingsMock.get.mockImplementation(() => (config: string) => {
switch (config) {
case 'notifications:lifetime:info':
return 5000;
case 'notifications:lifetime:warning':
return 10000;
case 'notification:lifetime:error':
return 30000;
default:
throw new Error(`Accessing ${config} is not supported in the mock.`);
}
);
});
return {
uiSettings: uiSettingsMock,
i18n: i18nServiceMock.createStartContract(),

2
typings/index.d.ts vendored
View file

@ -28,3 +28,5 @@ type MethodKeysOf<T> = {
}[keyof T];
type PublicMethodsOf<T> = Pick<T, MethodKeysOf<T>>;
type MockedKeys<T> = { [P in keyof T]: jest.Mocked<T[P]> };

View file

@ -22,3 +22,5 @@ type MethodKeysOf<T> = {
type PublicMethodsOf<T> = Pick<T, MethodKeysOf<T>>;
declare module 'axios/lib/adapters/xhr';
type MockedKeys<T> = { [P in keyof T]: jest.Mocked<T[P]> };