From a93fa18a1e1751fc3f0a7dc875930649ae7e3c11 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sat, 27 Jul 2024 17:02:32 +0200 Subject: [PATCH] frontend-test-utils: update to support v2 extensions Signed-off-by: Patrik Oldsberg --- .../src/app/createExtensionTester.tsx | 56 ++++++++++++++----- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/packages/frontend-test-utils/src/app/createExtensionTester.tsx b/packages/frontend-test-utils/src/app/createExtensionTester.tsx index 69c4b09d06..81b74c9fce 100644 --- a/packages/frontend-test-utils/src/app/createExtensionTester.tsx +++ b/packages/frontend-test-utils/src/app/createExtensionTester.tsx @@ -19,6 +19,7 @@ import { MemoryRouter, Link } from 'react-router-dom'; import { RenderResult, render } from '@testing-library/react'; import { createSpecializedApp } from '@backstage/frontend-app-api'; import { + ExtensionDataValue, ExtensionDefinition, IconComponent, RouteRef, @@ -93,21 +94,48 @@ export class ExtensionTester { options?: { config?: TConfigInput }, ): ExtensionTester { const tester = new ExtensionTester(); - const { output, factory, ...rest } = toInternalExtensionDefinition(subject); + const internal = toInternalExtensionDefinition(subject); + // attaching to app/routes to render as index route - const extension = createExtension({ - ...rest, - attachTo: { id: 'app/routes', input: 'routes' }, - output: { - ...output, - path: coreExtensionData.routePath, - }, - factory: params => ({ - ...factory(params), - path: '/', - }), - }); - tester.add(extension, options as TConfigInput & {}); + if (internal.version === 'v1') { + tester.add( + createExtension({ + ...internal, + attachTo: { id: 'app/routes', input: 'routes' }, + output: { + ...internal.output, + path: coreExtensionData.routePath, + }, + factory: params => ({ + ...internal.factory(params as any), + path: '/', + }), + }), + options as TConfigInput & {}, + ); + } else if (internal.version === 'v2') { + tester.add( + createExtension({ + ...internal, + attachTo: { id: 'app/routes', input: 'routes' }, + output: internal.output.find( + ref => ref.id === coreExtensionData.routePath.id, + ) + ? internal.output + : [...internal.output, coreExtensionData.routePath], + factory: params => { + const parentOutput = Array.from( + internal.factory(params) as Iterable< + ExtensionDataValue + >, + ).filter(val => val.id !== coreExtensionData.routePath.id); + + return [...parentOutput, coreExtensionData.routePath('/')]; + }, + }), + options as TConfigInput & {}, + ); + } return tester; }