forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce component registry for advanced ui settings screen (elastic…
…#22315) (elastic#22391) * introduce component registry for advanced ui settings screen
- Loading branch information
Showing
12 changed files
with
344 additions
and
26 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
5 changes: 5 additions & 0 deletions
5
...lic/management/sections/settings/components/__snapshots__/component_registry.test.js.snap
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,5 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`getSettingsComponent should throw an error when requesting a component that does not exist 1`] = `"Component not found with id does not exist"`; | ||
|
||
exports[`registerSettingsComponent should disallow registering a component with a duplicate id 1`] = `"Component with id test2 is already registered."`; |
73 changes: 73 additions & 0 deletions
73
src/core_plugins/kibana/public/management/sections/settings/components/component_registry.js
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,73 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
const registry = {}; | ||
|
||
/** | ||
* Attempts to register the provided component. | ||
* If a component with that ID is already registered, then the registration fails. | ||
* | ||
* @param {*} id the id of the component to register | ||
* @param {*} component the component | ||
*/ | ||
export function tryRegisterSettingsComponent(id, component) { | ||
if (id in registry) { | ||
return false; | ||
} | ||
|
||
registerSettingsComponent(id, component); | ||
return true; | ||
} | ||
|
||
/** | ||
* Attempts to register the provided component, with the ability to optionally allow | ||
* the component to override an existing one. | ||
* | ||
* If the intent is to override, then `allowOverride` must be set to true, otherwise an exception is thrown. | ||
* | ||
* @param {*} id the id of the component to register | ||
* @param {*} component the component | ||
* @param {*} allowOverride (default: false) - optional flag to allow this component to override a previously registered component | ||
*/ | ||
export function registerSettingsComponent(id, component, allowOverride = false) { | ||
if (!allowOverride && id in registry) { | ||
throw new Error(`Component with id ${id} is already registered.`); | ||
} | ||
|
||
// Setting a display name if one does not already exist. | ||
// This enhances the snapshots, as well as the debugging experience. | ||
if (!component.displayName) { | ||
component.displayName = id; | ||
} | ||
|
||
registry[id] = component; | ||
} | ||
|
||
/** | ||
* Retrieve a registered component by its ID. | ||
* If the component does not exist, then an exception is thrown. | ||
* | ||
* @param {*} id the ID of the component to retrieve | ||
*/ | ||
export function getSettingsComponent(id) { | ||
if (!(id in registry)) { | ||
throw new Error(`Component not found with id ${id}`); | ||
} | ||
return registry[id]; | ||
} |
89 changes: 89 additions & 0 deletions
89
..._plugins/kibana/public/management/sections/settings/components/component_registry.test.js
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,89 @@ | ||
/* | ||
* 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 { tryRegisterSettingsComponent, registerSettingsComponent, getSettingsComponent } from './component_registry'; | ||
|
||
|
||
describe('tryRegisterSettingsComponent', () => { | ||
it('should allow a component to be registered', () => { | ||
const component = {}; | ||
expect(tryRegisterSettingsComponent('tryTest1', component)).toEqual(true); | ||
}); | ||
|
||
it('should return false if the component is already registered, and not allow an override', () => { | ||
const component = {}; | ||
expect(tryRegisterSettingsComponent('tryTest2', component)).toEqual(true); | ||
|
||
const updatedComponent = { updated: 'yay' }; | ||
expect(tryRegisterSettingsComponent('tryTest2', updatedComponent)).toEqual(false); | ||
expect(getSettingsComponent('tryTest2')).toBe(component); | ||
}); | ||
}); | ||
|
||
describe('registerSettingsComponent', () => { | ||
it('should allow a component to be registered', () => { | ||
const component = {}; | ||
registerSettingsComponent('test', component); | ||
}); | ||
|
||
it('should disallow registering a component with a duplicate id', () => { | ||
const component = {}; | ||
registerSettingsComponent('test2', component); | ||
expect(() => registerSettingsComponent('test2', 'some other component')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('should allow a component to be overriden', () => { | ||
const component = {}; | ||
registerSettingsComponent('test3', component); | ||
|
||
const anotherComponent = { 'anotherComponent': 'ok' }; | ||
registerSettingsComponent('test3', anotherComponent, true); | ||
|
||
expect(getSettingsComponent('test3')).toBe(anotherComponent); | ||
}); | ||
|
||
it('should set a displayName for the component if one does not exist', () => { | ||
const component = {}; | ||
registerSettingsComponent('display_name_component', component); | ||
|
||
expect(component.displayName).toEqual('display_name_component'); | ||
}); | ||
|
||
it('should not set a displayName for the component if one already exists', () => { | ||
const component = { | ||
displayName: '<AwesomeComponent>' | ||
}; | ||
|
||
registerSettingsComponent('another_display_name_component', component); | ||
|
||
expect(component.displayName).toEqual('<AwesomeComponent>'); | ||
}); | ||
}); | ||
|
||
describe('getSettingsComponent', () => { | ||
it('should allow a component to be retrieved', () => { | ||
const component = {}; | ||
registerSettingsComponent('test4', component); | ||
expect(getSettingsComponent('test4')).toBe(component); | ||
}); | ||
|
||
it('should throw an error when requesting a component that does not exist', () => { | ||
expect(() => getSettingsComponent('does not exist')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
...ugins/kibana/public/management/sections/settings/components/default_component_registry.js
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,27 @@ | ||
/* | ||
* 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 { tryRegisterSettingsComponent } from './component_registry'; | ||
import { PageTitle } from './page_title'; | ||
|
||
export const PAGE_TITLE_COMPONENT = 'advanced_settings_page_title'; | ||
|
||
export function registerDefaultComponents() { | ||
tryRegisterSettingsComponent(PAGE_TITLE_COMPONENT, PageTitle); | ||
} |
43 changes: 43 additions & 0 deletions
43
.../kibana/public/management/sections/settings/components/default_component_registry.test.js
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,43 @@ | ||
/* | ||
* 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 { registerDefaultComponents, PAGE_TITLE_COMPONENT } from './default_component_registry'; | ||
import { getSettingsComponent, registerSettingsComponent } from './component_registry'; | ||
import { PageTitle } from './page_title'; | ||
|
||
describe('default_component_registry', () => { | ||
it('should register default components with the registry', () => { | ||
registerDefaultComponents(); | ||
expect(getSettingsComponent(PAGE_TITLE_COMPONENT)).toEqual(PageTitle); | ||
}); | ||
|
||
it('should be able to call "registerDefaultComponents" several times without throwing', () => { | ||
registerDefaultComponents(); | ||
registerDefaultComponents(); | ||
registerDefaultComponents(); | ||
}); | ||
|
||
it('should not override components if they are already registered', () => { | ||
const newComponent = {}; | ||
registerSettingsComponent(PAGE_TITLE_COMPONENT, newComponent, true); | ||
registerDefaultComponents(); | ||
|
||
expect(getSettingsComponent(PAGE_TITLE_COMPONENT)).toEqual(newComponent); | ||
}); | ||
}); |
13 changes: 13 additions & 0 deletions
13
.../management/sections/settings/components/page_title/__snapshots__/page_title.test.js.snap
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 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`PageTitle should render normally 1`] = ` | ||
<EuiText | ||
grow={true} | ||
> | ||
<h1 | ||
data-test-subj="managementSettingsTitle" | ||
> | ||
Settings | ||
</h1> | ||
</EuiText> | ||
`; |
20 changes: 20 additions & 0 deletions
20
src/core_plugins/kibana/public/management/sections/settings/components/page_title/index.js
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 { PageTitle } from './page_title'; |
Oops, something went wrong.