From 4a494abf78cec101e8bbabd04d05aa96b3a25a76 Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Tue, 7 Oct 2025 20:57:39 +0200 Subject: [PATCH] frontend-test-utils: introduce renderTestApp and mark features option as deprecated Signed-off-by: Vincenzo Scamporlino --- packages/frontend-test-utils/src/app/index.ts | 1 + .../src/app/renderInTestApp.tsx | 2 + .../src/app/renderTestApp.tsx | 81 +++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 packages/frontend-test-utils/src/app/renderTestApp.tsx diff --git a/packages/frontend-test-utils/src/app/index.ts b/packages/frontend-test-utils/src/app/index.ts index 6c6ef26e4c..9db67dbb49 100644 --- a/packages/frontend-test-utils/src/app/index.ts +++ b/packages/frontend-test-utils/src/app/index.ts @@ -21,3 +21,4 @@ export { } from './createExtensionTester'; export { renderInTestApp, type TestAppOptions } from './renderInTestApp'; +export { renderTestApp, type RenderTestAppOptions } from './renderTestApp'; diff --git a/packages/frontend-test-utils/src/app/renderInTestApp.tsx b/packages/frontend-test-utils/src/app/renderInTestApp.tsx index e1b35eba6c..2b8efb643f 100644 --- a/packages/frontend-test-utils/src/app/renderInTestApp.tsx +++ b/packages/frontend-test-utils/src/app/renderInTestApp.tsx @@ -69,6 +69,8 @@ export type TestAppOptions = { /** * Additional extensions to add to the test app. + * + * @deprecated Use `renderTestApp` instead. */ extensions?: ExtensionDefinition[]; diff --git a/packages/frontend-test-utils/src/app/renderTestApp.tsx b/packages/frontend-test-utils/src/app/renderTestApp.tsx new file mode 100644 index 0000000000..1644ee1c30 --- /dev/null +++ b/packages/frontend-test-utils/src/app/renderTestApp.tsx @@ -0,0 +1,81 @@ +/* + * Copyright 2025 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 { createSpecializedApp } from '@backstage/frontend-app-api'; +import { + coreExtensionData, + createFrontendPlugin, + ExtensionDefinition, + FrontendFeature, +} from '@backstage/frontend-plugin-api'; +import { render } from '@testing-library/react'; +import appPlugin from '@backstage/plugin-app'; +import { JsonObject } from '@backstage/types'; +import { ConfigReader } from '@backstage/config'; + +export type RenderTestAppOptions = { + /** + * Additional configuration passed to the app when rendering elements inside it. + */ + config?: JsonObject; + /** + * Additional extensions to add to the test app. + */ + extensions: ExtensionDefinition[]; + + /** + * Additional features to add to the test app. + */ + features?: FrontendFeature[]; +}; + +const appPluginOverride = appPlugin.withOverrides({ + extensions: [ + appPlugin.getExtension('sign-in-page:app').override({ + disabled: true, + }), + ], +}); + +export function renderTestApp(options: RenderTestAppOptions) { + const features: FrontendFeature[] = [ + createFrontendPlugin({ + pluginId: 'test', + extensions: options.extensions, + }), + appPluginOverride, + ]; + + if (options.features) { + features.push(...options.features); + } + + const app = createSpecializedApp({ + features, + config: options.config + ? ConfigReader.fromConfigs([ + { + context: 'render-config', + data: options.config, + }, + ]) + : undefined, + }); + + return render( + app.tree.root.instance!.getData(coreExtensionData.reactElement), + ); +}