core-compat-api: test + support APIs for convertLegacyPlugin

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-08-16 13:15:54 +02:00
parent 71fb0269e3
commit 6f23e5c24e
2 changed files with 96 additions and 1 deletions
@@ -0,0 +1,91 @@
/*
* Copyright 2024 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 {
createPlugin as createLegacyPlugin,
createRouteRef as createLegacyRouteRef,
createExternalRouteRef as createLegacyExternalRouteRef,
createApiFactory,
createApiRef,
} from '@backstage/core-plugin-api';
import { convertLegacyPlugin } from './convertLegacyPlugin';
import { PageBlueprint } from '@backstage/frontend-plugin-api';
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
import { toInternalBackstagePlugin } from '../../frontend-plugin-api/src/wiring/createFrontendPlugin';
describe('convertLegacyPlugin', () => {
it('should convert a plain legacy plugin to a new plugin', () => {
expect(
convertLegacyPlugin(createLegacyPlugin({ id: 'test' }), {
extensions: [],
}),
).toMatchInlineSnapshot(`
{
"$$type": "@backstage/BackstagePlugin",
"extensions": [],
"externalRoutes": {},
"featureFlags": [],
"getExtension": [Function],
"id": "test",
"routes": {},
"toString": [Function],
"version": "v1",
"withOverrides": [Function],
}
`);
});
it('should convert a legacy plugin with options to a new plugin', () => {
const apiRef = createApiRef<string>({ id: 'plugin.test.client' });
const routeRef = createLegacyRouteRef({ id: 'test' });
const extRouteRef = createLegacyExternalRouteRef({ id: 'testExt' });
const converted = convertLegacyPlugin(
createLegacyPlugin({
id: 'test',
apis: [createApiFactory(apiRef, 'hello')],
routes: { test: routeRef },
externalRoutes: {
testExt: extRouteRef,
},
featureFlags: [{ name: 'test-flag' }],
}),
{
extensions: [
PageBlueprint.make({
params: { defaultPath: '/test', loader: async () => ({} as any) },
}),
],
},
);
const internalConverted = toInternalBackstagePlugin(converted);
expect(internalConverted.id).toBe('test');
expect(internalConverted.routes).toEqual({
test: routeRef,
});
expect(internalConverted.externalRoutes).toEqual({
testExt: extRouteRef,
});
expect(internalConverted.featureFlags).toEqual([{ name: 'test-flag' }]);
expect(internalConverted.extensions.map(e => e.id)).toEqual([
'api:test/plugin.test.client',
'page:test',
]);
});
});
@@ -16,6 +16,7 @@
import { BackstagePlugin as LegacyBackstagePlugin } from '@backstage/core-plugin-api';
import {
ApiBlueprint,
ExtensionDefinition,
BackstagePlugin as NewBackstagePlugin,
createFrontendPlugin,
@@ -27,11 +28,14 @@ export function convertLegacyPlugin(
legacyPlugin: LegacyBackstagePlugin,
options: { extensions: ExtensionDefinition<any, any>[] },
): NewBackstagePlugin {
const apiExtensions = Array.from(legacyPlugin.getApis()).map(factory =>
ApiBlueprint.make({ name: factory.api.id, params: { factory } }),
);
return createFrontendPlugin({
id: legacyPlugin.getId(),
featureFlags: [...legacyPlugin.getFeatureFlags()],
routes: convertLegacyRouteRefs(legacyPlugin.routes ?? {}),
externalRoutes: convertLegacyRouteRefs(legacyPlugin.externalRoutes ?? {}),
extensions: options.extensions,
extensions: [...apiExtensions, ...options.extensions],
});
}