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

Migrate base path APIs and UiSettings client to new platform #22694

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d02cd0e
create basePath service for new platform
Aug 21, 2018
68f7f01
migrate ui settings to new platform
Aug 22, 2018
743b553
[ui/core/uiSetttings] actually await expect(promise) calls
spalger Sep 4, 2018
9618563
Merge branch 'master' of github.com:elastic/kibana into HEAD
Sep 5, 2018
df345e1
[ui/core/uiSettings] add tests for uiSettingsService
Sep 5, 2018
05d9b8e
test out mock class helper
Sep 5, 2018
b772a7d
fix reference to uiSettings.subscribe()
Sep 5, 2018
4691b7b
[ui/chrome/test] derive basePath from internals like legacy implement…
Sep 5, 2018
dc9f43b
Merge branch 'master' of github.com:elastic/kibana into migrate-new-p…
Sep 6, 2018
dc5ddc7
[core/public/uiSettingsClient/types] include all properties, even uns…
Sep 6, 2018
02f0cc9
fix typos
Sep 8, 2018
63bcb4d
limit exported types to those in use
Sep 8, 2018
f5e6643
remove unnecessary copy of modify_url util in ui/public
Sep 8, 2018
1bcd411
remove unnecessary types from JSDoc
Sep 8, 2018
60d8924
use Number constructor rather than parseInt
Sep 8, 2018
cf2e10f
[core/public/legacyService] test that uiSettings is initialized
Sep 8, 2018
a7db8fd
[core/public/uiSettingsClient] use readonly when possible
Sep 8, 2018
ea4cf06
[core/public/uiSettingsClient] use imported type since available
Sep 8, 2018
ddbd2b8
[core/public/uiSettingsClient] remove unnecessary Boolean cast
Sep 8, 2018
8dec538
Merge branch 'master' of github.com:elastic/kibana into migrate-new-p…
Sep 8, 2018
d2f5816
[core/public/basePath/removeBasePath] handle a couple edge-cases better
Sep 8, 2018
aa60f3e
fix modifyUrl import
Sep 8, 2018
aac0a7c
fix another modifyUrl import (this time run eslint to verify :P)
Sep 8, 2018
19eb622
Merge branch 'master' of github.com:elastic/kibana into migrate-new-p…
Sep 8, 2018
b1a6ddf
[jest] update snapshots
spalger Sep 8, 2018
596ca89
import directly from core when necessary to avoid infecting jest with…
Sep 8, 2018
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
94 changes: 94 additions & 0 deletions src/core/public/base_path/base_path_service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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 } from './base_path_service';

function setup(options: any = {}) {
const injectedBasePath: string =
options.injectedBasePath === undefined ? '/foo/bar' : options.injectedBasePath;

const service = new BasePathService();

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

const startContract = service.start({
injectedMetadata,
});

return {
service,
startContract,
injectedBasePath,
};
}

describe('startContract.get()', () => {
it('returns an empty string if no basePath is injected', () => {
const { startContract } = setup({ injectedBasePath: null });
expect(startContract.get()).toBe('');
});

it('returns the injected basePath', () => {
const { startContract } = setup();
expect(startContract.get()).toBe('/foo/bar');
});
});

describe('startContract.addToPath()', () => {
it('adds the base path to the path if it is relative and starts with a slash', () => {
const { startContract } = setup();
expect(startContract.addToPath('/a/b')).toBe('/foo/bar/a/b');
});

it('leaves the query string and hash of path unchanged', () => {
const { startContract } = setup();
expect(startContract.addToPath('/a/b?x=y#c/d/e')).toBe('/foo/bar/a/b?x=y#c/d/e');
});

it('returns the path unchanged if it does not start with a slash', () => {
const { startContract } = setup();
expect(startContract.addToPath('a/b')).toBe('a/b');
});

it('returns the path unchanged it it has a hostname', () => {
const { startContract } = setup();
expect(startContract.addToPath('http://localhost:5601/a/b')).toBe('http://localhost:5601/a/b');
});
});

describe('startContract.removeFromPath()', () => {
it('removes the basePath if relative path starts with it', () => {
const { startContract } = setup();
expect(startContract.removeFromPath('/foo/bar/a/b')).toBe('/a/b');
});

it('leaves query string and hash intact', () => {
const { startContract } = setup();
expect(startContract.removeFromPath('/foo/bar/a/b?c=y#1234')).toBe('/a/b?c=y#1234');
});

it('ignores urls with hostnames', () => {
const { startContract } = setup();
expect(startContract.removeFromPath('http://localhost:5601/foo/bar/a/b')).toBe(
'http://localhost:5601/foo/bar/a/b'
);
});
});
66 changes: 66 additions & 0 deletions src/core/public/base_path/base_path_service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 { InjectedMetadataStartContract } from '../injected_metadata';
import { modifyUrl } from '../utils';

interface Deps {
injectedMetadata: InjectedMetadataStartContract;
}

export class BasePathService {
public start({ injectedMetadata }: Deps) {
const basePath = injectedMetadata.getBasePath() || '';

return {
/**
* Get the current basePath as defined by the server
*/
get() {
return basePath;
},

/**
* Add the current basePath to a path string.
* @param path a relative url including the leading `/`, otherwise it will be returned without modification
spalger marked this conversation as resolved.
Show resolved Hide resolved
*/
addToPath(path: string) {
return modifyUrl(path, parts => {
if (!parts.hostname && parts.pathname && parts.pathname.startsWith('/')) {
parts.pathname = `${basePath}${parts.pathname}`;
}
});
},

/**
* Remove the basePath from a path that starts with it
* @param path a relative url that starts with the basePath, which will be stripped
*/
removeFromPath(path: string) {
if (!basePath) {
return path;
}

return path.startsWith(basePath) ? path.slice(basePath.length) : path;
spalger marked this conversation as resolved.
Show resolved Hide resolved
},
};
}
}

export type BasePathStartContract = ReturnType<BasePathService['start']>;
20 changes: 20 additions & 0 deletions src/core/public/base_path/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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 { BasePathService, BasePathStartContract } from './base_path_service';
43 changes: 43 additions & 0 deletions src/core/public/core_system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
* under the License.
*/

import { BasePathService } from './base_path';
import { FatalErrorsService } from './fatal_errors';
import { InjectedMetadataService } from './injected_metadata';
import { LegacyPlatformService } from './legacy_platform';
import { LoadingCountService } from './loading_count';
import { NotificationsService } from './notifications';
import { UiSettingsService } from './ui_settings';

const MockLegacyPlatformService = jest.fn<LegacyPlatformService>(
function _MockLegacyPlatformService(this: any) {
Expand Down Expand Up @@ -76,6 +78,24 @@ jest.mock('./loading_count', () => ({
LoadingCountService: MockLoadingCountService,
}));

const mockBasePathStartContract = {};
const MockBasePathService = jest.fn<BasePathService>(function _MockNotificationsService(this: any) {
this.start = jest.fn().mockReturnValue(mockBasePathStartContract);
});
jest.mock('./base_path', () => ({
BasePathService: MockBasePathService,
}));

const mockUiSettingsContract = {};
const MockUiSettingsService = jest.fn<UiSettingsService>(function _MockNotificationsService(
this: any
) {
this.start = jest.fn().mockReturnValue(mockUiSettingsContract);
});
jest.mock('./ui_settings', () => ({
UiSettingsService: MockUiSettingsService,
}));

import { CoreSystem } from './core_system';
jest.spyOn(CoreSystem.prototype, 'stop');

Expand All @@ -101,6 +121,8 @@ describe('constructor', () => {
expect(MockFatalErrorsService).toHaveBeenCalledTimes(1);
expect(MockNotificationsService).toHaveBeenCalledTimes(1);
expect(MockLoadingCountService).toHaveBeenCalledTimes(1);
expect(MockBasePathService).toHaveBeenCalledTimes(1);
expect(MockUiSettingsService).toHaveBeenCalledTimes(1);
});

it('passes injectedMetadata param to InjectedMetadataService', () => {
Expand Down Expand Up @@ -221,6 +243,27 @@ describe('#start()', () => {
});
});

it('calls basePath#start()', () => {
startCore();
const [mockInstance] = MockBasePathService.mock.instances;
expect(mockInstance.start).toHaveBeenCalledTimes(1);
expect(mockInstance.start).toHaveBeenCalledWith({
injectedMetadata: mockInjectedMetadataStartContract,
});
});

it('calls uiSettings#start()', () => {
startCore();
const [mockInstance] = MockUiSettingsService.mock.instances;
expect(mockInstance.start).toHaveBeenCalledTimes(1);
expect(mockInstance.start).toHaveBeenCalledWith({
notifications: mockNotificationStartContract,
loadingCount: mockLoadingCountContract,
injectedMetadata: mockInjectedMetadataStartContract,
basePath: mockBasePathStartContract,
});
});

it('calls fatalErrors#start()', () => {
startCore();
const [mockInstance] = MockFatalErrorsService.mock.instances;
Expand Down
23 changes: 22 additions & 1 deletion src/core/public/core_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
*/

import './core.css';

import { BasePathService } from './base_path';
import { FatalErrorsService } from './fatal_errors';
import { InjectedMetadataParams, InjectedMetadataService } from './injected_metadata';
import { LegacyPlatformParams, LegacyPlatformService } from './legacy_platform';
import { LoadingCountService } from './loading_count';
import { NotificationsService } from './notifications';
import { UiSettingsService } from './ui_settings';

interface Params {
rootDomElement: HTMLElement;
Expand All @@ -43,6 +46,8 @@ export class CoreSystem {
private readonly legacyPlatform: LegacyPlatformService;
private readonly notifications: NotificationsService;
private readonly loadingCount: LoadingCountService;
private readonly uiSettings: UiSettingsService;
private readonly basePath: BasePathService;

private readonly rootDomElement: HTMLElement;
private readonly notificationsTargetDomElement: HTMLDivElement;
Expand Down Expand Up @@ -71,6 +76,8 @@ export class CoreSystem {
});

this.loadingCount = new LoadingCountService();
this.basePath = new BasePathService();
this.uiSettings = new UiSettingsService();

this.legacyPlatformTargetDomElement = document.createElement('div');
this.legacyPlatform = new LegacyPlatformService({
Expand All @@ -92,7 +99,21 @@ export class CoreSystem {
const injectedMetadata = this.injectedMetadata.start();
const fatalErrors = this.fatalErrors.start();
const loadingCount = this.loadingCount.start({ fatalErrors });
this.legacyPlatform.start({ injectedMetadata, fatalErrors, notifications, loadingCount });
const basePath = this.basePath.start({ injectedMetadata });
const uiSettings = this.uiSettings.start({
notifications,
loadingCount,
injectedMetadata,
basePath,
});
this.legacyPlatform.start({
injectedMetadata,
fatalErrors,
notifications,
loadingCount,
basePath,
uiSettings,
});
} catch (error) {
this.fatalErrors.add(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface InjectedMetadataParams {
injectedMetadata: {
version: string;
buildNumber: number;
basePath: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: basePath is always going to be a string, just will be 0-length one if basePath isn't defined, correct? (just was a bit confused by injectedBasePath: null in base_path_service.test.ts)

legacyMetadata: {
[key: string]: any;
};
Expand All @@ -42,6 +43,14 @@ export class InjectedMetadataService {

public start() {
return {
getBasePath: () => {
return this.state.basePath;
},

getKibanaVersion: () => {
return this.getKibanaVersion();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: haha, my IDE mistakenly marks this as a recursive call :)

},

getLegacyMetadata: () => {
return this.state.legacyMetadata;
},
Expand Down
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
Loading