This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core/ui] bootstrap the legacy platform within the new platform (elas…
…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
Showing
40 changed files
with
934 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
33 changes: 33 additions & 0 deletions
33
src/core/public/injected_metadata/__fixtures__/frozen_object_mutation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
13 changes: 13 additions & 0 deletions
13
src/core/public/injected_metadata/__fixtures__/frozen_object_mutation.tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
}); |
Oops, something went wrong.