From bcaf90bfb20a68a46ab14ded59effc92cc22c588 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 16 Aug 2024 13:46:36 +0200 Subject: [PATCH] frontend-test-utils: avoid overriding test subject in extension tester unless rendering Signed-off-by: Patrik Oldsberg --- .../src/app/createExtensionTester.tsx | 81 +++++++++---------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/packages/frontend-test-utils/src/app/createExtensionTester.tsx b/packages/frontend-test-utils/src/app/createExtensionTester.tsx index b3c1b385e3..1f901699b1 100644 --- a/packages/frontend-test-utils/src/app/createExtensionTester.tsx +++ b/packages/frontend-test-utils/src/app/createExtensionTester.tsx @@ -144,46 +144,7 @@ export class ExtensionTester { options?: { config?: TConfigInput }, ): ExtensionTester { const tester = new ExtensionTester(); - const internal = toInternalExtensionDefinition(subject); - - // attaching to app/routes to render as index route - 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 any), - ).filter(val => val.id !== coreExtensionData.routePath.id); - - return [...parentOutput, coreExtensionData.routePath('/')]; - }, - }), - options as TConfigInput & {}, - ); - } + tester.add(subject, options as TConfigInput & {}); return tester; } @@ -288,11 +249,49 @@ export class ExtensionTester { ); } + const subjectInternal = toInternalExtensionDefinition(subject.definition); + let subjectOverride; + // attaching to app/routes to render as index route + if (subjectInternal.version === 'v1') { + subjectOverride = createExtension({ + ...subjectInternal, + attachTo: { id: 'app/routes', input: 'routes' }, + output: { + ...subjectInternal.output, + path: coreExtensionData.routePath, + }, + factory: params => ({ + ...subjectInternal.factory(params as any), + path: '/', + }), + }); + } else if (subjectInternal.version === 'v2') { + subjectOverride = createExtension({ + ...subjectInternal, + attachTo: { id: 'app/routes', input: 'routes' }, + output: subjectInternal.output.find( + ref => ref.id === coreExtensionData.routePath.id, + ) + ? subjectInternal.output + : [...subjectInternal.output, coreExtensionData.routePath], + factory: params => { + const parentOutput = Array.from( + subjectInternal.factory(params as any), + ).filter(val => val.id !== coreExtensionData.routePath.id); + + return [...parentOutput, coreExtensionData.routePath('/')]; + }, + }); + } else { + throw new Error('Unsupported extension version'); + } + const app = createSpecializedApp({ features: [ createExtensionOverrides({ extensions: [ - ...this.#extensions.map(extension => extension.definition), + subjectOverride, + ...this.#extensions.slice(1).map(extension => extension.definition), TestAppNavExtension, createRouterExtension({ namespace: 'test',