Cleaned up the code and added a test

Signed-off-by: bnechyporenko <bnechyporenko@bol.com>
This commit is contained in:
bnechyporenko
2022-05-31 13:35:00 +02:00
parent 549d7ad907
commit dab3abbec4
7 changed files with 57 additions and 39 deletions
+14 -26
View File
@@ -16,33 +16,23 @@ When you are creating your plugin, you have a possibility to use a metadata fiel
customizable elements. For example
```typescript jsx
export type MyPluginMetadataProps = {
createButtonTitle: string;
};
export const myPluginMetadataRef = createMetadataRef<MyPluginMetadataProps>({
id: 'MyPluginMetadataProvider',
});
export const myPluginMetadata = {
MyPluginMetadataProvider: {
// or [myPluginMetadataRef.id]
createButtonTitle: 'Create Component',
},
};
const plugin = createPlugin({
id: 'myPlugin',
metadata: [myPluginMetadata],
id: 'my-plugin',
options: {
createButtonTitle: 'Create',
},
});
```
And the rendering part of the exposed component can retrieve that metadata as:
```typescript jsx
export function DefaultMyPluginWelcomePage(props: DefaultMyPluginWelcomeProps) {
const { createButtonTitle } =
useMetadata<DefaultMyPluginWelcomeProps>(myPluginMetadataRef);
export type CatalogPageOptionsProps = {
createButtonTitle: string;
};
export function DefaultMyPluginWelcomePage() {
const { createButtonTitle } = usePluginOptions<CatalogPageOptionsProps>();
return (
<div>
@@ -60,10 +50,8 @@ plugin. Example:
```typescript jsx
import { myPlugin } from '@backstage/my-plugin';
myPlugin.reconfigure({
MyPluginMetadataProvider: {
// or [myPluginMetadataRef.id]
createButtonTitle: 'Make Component',
},
});
myPlugin.reconfigure((options: CatalogPageOptionsProps) => ({
...options,
createButtonTitle: 'Maybe Create',
}));
```
-6
View File
@@ -139,12 +139,6 @@ const app = createApp({
const AppProvider = app.getProvider();
const AppRouter = app.getRouter();
// TODO: remove it, only for testing here
catalogPlugin.reconfigure((options: AnyPluginOptions) => ({
...options,
createButtonTitle: 'Maybe Create',
}));
const routes = (
<FlatRoutes>
<Navigate key="/" to="catalog" />
@@ -0,0 +1,38 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed 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 React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { usePluginOptions, PluginOptionsProvider } from './usePluginOptions';
describe('usePluginOptions', () => {
it('should provide a versioned value to hook', () => {
const rendered = renderHook(() => usePluginOptions(), {
wrapper: ({ children }) => (
<PluginOptionsProvider
pluginOptions={{ 'key-1': 'value-1', 'key-2': 'value-2' }}
>
{children}
</PluginOptionsProvider>
),
});
expect(rendered.result.current).toEqual({
'key-1': 'value-1',
'key-2': 'value-2',
});
});
});
@@ -37,7 +37,7 @@ export interface PluginOptionsProviderProps {
export const PluginOptionsProvider = ({
children,
pluginOptions,
}: PluginOptionsProviderProps) => {
}: PluginOptionsProviderProps): JSX.Element => {
const value = { pluginOptions };
const { Provider } = createVersionedContext<{ 1: AnyPluginOptions }>(
contextKey,
@@ -57,7 +57,7 @@ export interface DefaultCatalogPageProps {
tableOptions?: TableProps<CatalogTableRow>['options'];
}
export type CatalogPageMetadataProps = {
export type CatalogPageOptionsProps = {
createButtonTitle: string;
};
@@ -73,7 +73,7 @@ export function DefaultCatalogPage(props: DefaultCatalogPageProps) {
useApi(configApiRef).getOptionalString('organization.name') ?? 'Backstage';
const createComponentLink = useRouteRef(createComponentRouteRef);
const { createButtonTitle } = usePluginOptions<CatalogPageMetadataProps>();
const { createButtonTitle } = usePluginOptions<CatalogPageOptionsProps>();
return (
<PageWithHeader title={`${orgName} Catalog`} themeId="home">
-3
View File
@@ -72,9 +72,6 @@ export const catalogPlugin = createPlugin({
createComponent: createComponentRouteRef,
viewTechDoc: viewTechDocRouteRef,
},
options: {
createButtonTitle: 'Create',
},
});
/** @public */
+2 -1
View File
@@ -9,6 +9,7 @@
"compilerOptions": {
"outDir": "dist-types",
"rootDir": ".",
"useUnknownInCatchVariables": false
"useUnknownInCatchVariables": false,
"jsx": "react"
}
}