Skip to content

Commit

Permalink
[ui/core/uiSettings] add tests for uiSettingsService
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Sep 5, 2018
1 parent 9618563 commit df345e1
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`#start() load order useLegacyTestHarness = false loads ui/modules before ui/chrome, and both before legacy files 1`] = `
Array [
"ui/metadata",
"ui/notify/fatal_error",
"ui/notify/toasts",
"ui/chrome/api/loading_count",
"ui/chrome/api/base_path",
"ui/chrome/api/ui_settings",
"ui/chrome",
"legacy files",
]
`;

exports[`#start() load order useLegacyTestHarness = true loads ui/modules before ui/test_harness, and both before legacy files 1`] = `
Array [
"ui/metadata",
"ui/notify/fatal_error",
"ui/notify/toasts",
"ui/chrome/api/loading_count",
"ui/chrome/api/base_path",
"ui/chrome/api/ui_settings",
"ui/test_harness",
"legacy files",
]
`;

exports[`#stop() destroys the angular scope and empties the targetDomElement if angular is bootstraped to targetDomElement 1`] = `
<div
class="ng-scope"
Expand Down
33 changes: 14 additions & 19 deletions src/core/public/legacy_platform/legacy_platform_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,22 @@ jest.mock('ui/chrome/api/base_path', () => {
};
});

const mockUiSettingsInit = jest.fn();
jest.mock('ui/chrome/api/ui_settings', () => {
mockLoadOrder.push('ui/chrome/api/ui_settings');
return {
__newPlatformInit__: mockUiSettingsInit,
};
});

import { LegacyPlatformService } from './legacy_platform_service';

const fatalErrorsStartContract = {} as any;
const notificationsStartContract = {
toasts: {},
} as any;

const injectedMetadataStartContract = {
const injectedMetadataStartContract: any = {
getBasePath: jest.fn(),
getLegacyMetadata: jest.fn(),
};
Expand All @@ -101,6 +109,8 @@ const basePathStartContract = {
removeFromPath: jest.fn(),
};

const uiSettingsStartContract: any = {};

const defaultParams = {
targetDomElement: document.createElement('div'),
requireLegacyFiles: jest.fn(() => {
Expand All @@ -114,6 +124,7 @@ const defaultStartDeps = {
notifications: notificationsStartContract,
loadingCount: loadingCountStartContract,
basePath: basePathStartContract,
uiSettings: uiSettingsStartContract,
};

afterEach(() => {
Expand Down Expand Up @@ -224,15 +235,7 @@ describe('#start()', () => {

legacyPlatform.start(defaultStartDeps);

expect(mockLoadOrder).toEqual([
'ui/metadata',
'ui/notify/fatal_error',
'ui/notify/toasts',
'ui/chrome/api/loading_count',
'ui/chrome/api/base_path',
'ui/chrome',
'legacy files',
]);
expect(mockLoadOrder).toMatchSnapshot();
});
});

Expand All @@ -247,15 +250,7 @@ describe('#start()', () => {

legacyPlatform.start(defaultStartDeps);

expect(mockLoadOrder).toEqual([
'ui/metadata',
'ui/notify/fatal_error',
'ui/notify/toasts',
'ui/chrome/api/loading_count',
'ui/chrome/api/base_path',
'ui/test_harness',
'legacy files',
]);
expect(mockLoadOrder).toMatchSnapshot();
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`#start contstructs UiSettingsClient and UiSettingsApi: UiSettingsApi args 1`] = `
[MockFunction MockUiSettingsApi] {
"calls": Array [
Array [
Object {
"basePathStartContract": true,
},
"kibanaVersion",
],
],
}
`;

exports[`#start contstructs UiSettingsClient and UiSettingsApi: UiSettingsClient args 1`] = `
[MockFunction MockUiSettingsClient] {
"calls": Array [
Array [
Object {
"api": mockConstructor {
"getLoadingCount$": [MockFunction] {
"calls": Array [
Array [],
],
},
"stop": [MockFunction],
},
"defaults": Object {
"legacyInjectedUiSettingDefaults": true,
},
"initialSettings": Object {
"legacyInjectedUiSettingUserValues": true,
},
"onUpdateError": [Function],
},
],
],
}
`;

exports[`#start passes the uiSettings loading count to the loading count api: loadingCount.add calls 1`] = `
[MockFunction] {
"calls": Array [
Array [
Object {
"loadingCountObservable": true,
},
],
],
}
`;
113 changes: 113 additions & 0 deletions src/core/public/ui_settings/ui_settings_service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.
*/

// Mock the UiSettingsApi class
import { UiSettingsApi } from './ui_settings_api';
const MockUiSettingsApi = jest
.fn<UiSettingsApi>(function(this: any) {
this.stop = jest.fn();
this.getLoadingCount$ = jest.fn().mockReturnValue({
loadingCountObservable: true,
});
})
.mockName('MockUiSettingsApi');
jest.mock('./ui_settings_api', () => ({
UiSettingsApi: MockUiSettingsApi,
}));

// Mock the UiSettingsClient class
import { UiSettingsClient } from './ui_settings_client';
const MockUiSettingsClient = jest
.fn<UiSettingsClient>(function(this: any) {
this.stop = jest.fn();
})
.mockName('MockUiSettingsClient');
jest.mock('./ui_settings_client', () => ({
UiSettingsClient: MockUiSettingsClient,
}));

// Load the service
import { UiSettingsService } from './ui_settings_service';

const loadingCountStartContract = {
loadingCountStartContract: true,
add: jest.fn(),
};

const defaultDeps: any = {
notifications: {
notificationsStartContract: true,
},
loadingCount: loadingCountStartContract,
injectedMetadata: {
injectedMetadataStartContract: true,
getKibanaVersion: jest.fn().mockReturnValue('kibanaVersion'),
getLegacyMetadata: jest.fn().mockReturnValue({
uiSettings: {
defaults: { legacyInjectedUiSettingDefaults: true },
user: { legacyInjectedUiSettingUserValues: true },
},
}),
},
basePath: {
basePathStartContract: true,
},
};

afterEach(() => {
jest.clearAllMocks();
});

describe('#start', () => {
it('returns an instance of UiSettingsClient', () => {
const start = new UiSettingsService().start(defaultDeps);
expect(start).toBeInstanceOf(MockUiSettingsClient);
});

it('contstructs UiSettingsClient and UiSettingsApi', () => {
new UiSettingsService().start(defaultDeps);

expect(MockUiSettingsApi).toMatchSnapshot('UiSettingsApi args');
expect(MockUiSettingsClient).toMatchSnapshot('UiSettingsClient args');
});

it('passes the uiSettings loading count to the loading count api', () => {
new UiSettingsService().start(defaultDeps);

expect(loadingCountStartContract.add).toMatchSnapshot('loadingCount.add calls');
});
});

describe('#stop', () => {
it('runs fine if service never started', () => {
const service = new UiSettingsService();
expect(() => service.stop()).not.toThrowError();
});

it('stops the uiSettingsClient and uiSettingsApi', () => {
const service = new UiSettingsService();
const client = service.start(defaultDeps);
const [[{ api }]] = MockUiSettingsClient.mock.calls;
jest.spyOn(client, 'stop');
jest.spyOn(api, 'stop');
service.stop();
expect(api.stop).toHaveBeenCalledTimes(1);
expect(client.stop).toHaveBeenCalledTimes(1);
});
});

0 comments on commit df345e1

Please sign in to comment.