Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
[core/ui] bootstrap the legacy platform within the new platform (elas…
Browse files Browse the repository at this point in the history
…tic#20699)

Fixes elastic#20694

Implements super basic new platform `core` system, which includes two services: `core.injectedMetadata` and `core.legacyPlatform`. The `core` currently has two responsibilities:

 1. read the metadata from the DOM and initialize the `ui/metadata` module with legacy metadata, proving out how we plan to expose data from the new platform through the existing APIs/modules to the legacy platform.
 2. bootstrap the legacy platform by loading either `ui/chrome` or `ui/test_harness`

Because `core` mutates the `ui/metadata` module before bootstrapping the legacy platform all existing consumers of `ui/metadata` won't be impacted by the fact that metadata loading was moved into the new platform. We plan to do this for many other services that will need to exist in both the legacy and new platforms, like `ui/chrome` (see elastic#20696).
  • Loading branch information
Spencer committed Jul 18, 2018
1 parent 05bbe62 commit c516507
Show file tree
Hide file tree
Showing 40 changed files with 934 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ exports.getWebpackConfig = function(kibanaPath, projectRoot, config) {
const alias = {
// Kibana defaults https://github.com/elastic/kibana/blob/6998f074542e8c7b32955db159d15661aca253d7/src/ui/ui_bundler_env.js#L30-L36
ui: fromKibana('src/ui/public'),
ui_framework: fromKibana('ui_framework'),
test_harness: fromKibana('src/test_harness/public'),
querystring: 'querystring-browser',
moment$: fromKibana('webpackShims/moment'),
Expand Down
126 changes: 126 additions & 0 deletions src/core/public/core_system.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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 { InjectedMetadataService } from './injected_metadata';
import { LegacyPlatformService } from './legacy_platform';

const MockLegacyPlatformService = jest.fn<LegacyPlatformService>(
function _MockLegacyPlatformService(this: any) {
this.start = jest.fn();
}
);
jest.mock('./legacy_platform', () => ({
LegacyPlatformService: MockLegacyPlatformService,
}));

const mockInjectedMetadataStartContract = {};
const MockInjectedMetadataService = jest.fn<InjectedMetadataService>(
function _MockInjectedMetadataService(this: any) {
this.start = jest.fn().mockReturnValue(mockInjectedMetadataStartContract);
}
);
jest.mock('./injected_metadata', () => ({
InjectedMetadataService: MockInjectedMetadataService,
}));

import { CoreSystem } from './core_system';

const defaultCoreSystemParams = {
rootDomElement: null!,
injectedMetadata: {} as any,
requireLegacyFiles: jest.fn(),
};

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

describe('constructor', () => {
it('creates instances of services', () => {
// tslint:disable no-unused-expression
new CoreSystem({
...defaultCoreSystemParams,
});

expect(MockInjectedMetadataService).toHaveBeenCalledTimes(1);
expect(MockLegacyPlatformService).toHaveBeenCalledTimes(1);
});

it('passes injectedMetadata param to InjectedMetadataService', () => {
const injectedMetadata = { injectedMetadata: true } as any;

// tslint:disable no-unused-expression
new CoreSystem({
...defaultCoreSystemParams,
injectedMetadata,
});

expect(MockInjectedMetadataService).toHaveBeenCalledTimes(1);
expect(MockInjectedMetadataService).toHaveBeenCalledWith({
injectedMetadata,
});
});

it('passes rootDomElement, requireLegacyFiles, and useLegacyTestHarness to LegacyPlatformService', () => {
const rootDomElement = { rootDomElement: true } as any;
const requireLegacyFiles = { requireLegacyFiles: true } as any;
const useLegacyTestHarness = { useLegacyTestHarness: true } as any;

// tslint:disable no-unused-expression
new CoreSystem({
...defaultCoreSystemParams,
rootDomElement,
requireLegacyFiles,
useLegacyTestHarness,
});

expect(MockLegacyPlatformService).toHaveBeenCalledTimes(1);
expect(MockLegacyPlatformService).toHaveBeenCalledWith({
rootDomElement,
requireLegacyFiles,
useLegacyTestHarness,
});
});
});

describe('#start()', () => {
function startCore() {
const core = new CoreSystem({
...defaultCoreSystemParams,
});

core.start();
}

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

it('calls lifecycleSystem#start()', () => {
startCore();
const [mockInstance] = MockLegacyPlatformService.mock.instances;
expect(mockInstance.start).toHaveBeenCalledTimes(1);
expect(mockInstance.start).toHaveBeenCalledWith({
injectedMetadata: mockInjectedMetadataStartContract,
});
});
});
59 changes: 59 additions & 0 deletions src/core/public/core_system.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 { InjectedMetadataParams, InjectedMetadataService } from './injected_metadata';
import { LegacyPlatformParams, LegacyPlatformService } from './legacy_platform';

interface Params {
injectedMetadata: InjectedMetadataParams['injectedMetadata'];
rootDomElement: LegacyPlatformParams['rootDomElement'];
requireLegacyFiles: LegacyPlatformParams['requireLegacyFiles'];
useLegacyTestHarness?: LegacyPlatformParams['useLegacyTestHarness'];
}

/**
* The CoreSystem is the root of the new platform, and starts all parts
* of Kibana in the UI, including the LegacyPlatform which is managed
* by the LegacyPlatformService. As we migrate more things to the new
* platform the CoreSystem will get many more Services.
*/
export class CoreSystem {
private injectedMetadata: InjectedMetadataService;
private legacyPlatform: LegacyPlatformService;

constructor(params: Params) {
const { rootDomElement, injectedMetadata, requireLegacyFiles, useLegacyTestHarness } = params;

this.injectedMetadata = new InjectedMetadataService({
injectedMetadata,
});

this.legacyPlatform = new LegacyPlatformService({
rootDomElement,
requireLegacyFiles,
useLegacyTestHarness,
});
}

public start() {
this.legacyPlatform.start({
injectedMetadata: this.injectedMetadata.start(),
});
}
}
20 changes: 20 additions & 0 deletions src/core/public/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 { CoreSystem } from './core_system';
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 { deepFreeze } from '../deep_freeze';

const obj = deepFreeze({
foo: {
bar: {
baz: 1,
},
},
});

delete obj.foo;
obj.foo = 1;
obj.foo.bar.baz = 2;
obj.foo.bar.box = false;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"strict": true,
"skipLibCheck": true,
"lib": [
"esnext"
]
},
"include": [
"frozen_object_mutation.ts",
"../deep_freeze.ts"
]
}
102 changes: 102 additions & 0 deletions src/core/public/injected_metadata/deep_freeze.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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 { resolve } from 'path';

import execa from 'execa';

import { deepFreeze } from './deep_freeze';

it('returns the first argument with all original references', () => {
const a = {};
const b = {};
const c = { a, b };

const frozen = deepFreeze(c);
expect(frozen).toBe(c);
expect(frozen.a).toBe(a);
expect(frozen.b).toBe(b);
});

it('prevents adding properties to argument', () => {
const frozen = deepFreeze({});
expect(() => {
// @ts-ignore ts knows this shouldn't be possible, but just making sure
frozen.foo = true;
}).toThrowError(`object is not extensible`);
});

it('prevents changing properties on argument', () => {
const frozen = deepFreeze({ foo: false });
expect(() => {
// @ts-ignore ts knows this shouldn't be possible, but just making sure
frozen.foo = true;
}).toThrowError(`read only property 'foo'`);
});

it('prevents changing properties on nested children of argument', () => {
const frozen = deepFreeze({ foo: { bar: { baz: { box: 1 } } } });
expect(() => {
// @ts-ignore ts knows this shouldn't be possible, but just making sure
frozen.foo.bar.baz.box = 2;
}).toThrowError(`read only property 'box'`);
});

it('prevents adding items to a frozen array', () => {
const frozen = deepFreeze({ foo: [1] });
expect(() => {
// @ts-ignore ts knows this shouldn't be possible, but just making sure
frozen.foo.push(2);
}).toThrowError(`object is not extensible`);
});

it('prevents reassigning items in a frozen array', () => {
const frozen = deepFreeze({ foo: [1] });
expect(() => {
// @ts-ignore ts knows this shouldn't be possible, but just making sure
frozen.foo[0] = 2;
}).toThrowError(`read only property '0'`);
});

it('types return values to prevent mutations in typescript', async () => {
const result = await execa.stdout(
'tsc',
[
'--noEmit',
'--project',
resolve(__dirname, '__fixtures__/frozen_object_mutation.tsconfig.json'),
],
{
cwd: resolve(__dirname, '__fixtures__'),
reject: false,
}
);

const errorCodeRe = /\serror\s(TS\d{4}):/g;
const errorCodes = [];
while (true) {
const match = errorCodeRe.exec(result);
if (!match) {
break;
}
errorCodes.push(match[1]);
}

expect(errorCodes).toEqual(['TS2704', 'TS2540', 'TS2540', 'TS2339']);
});
Loading

0 comments on commit c516507

Please sign in to comment.