feat: some more work on splitting out the render method
Co-authored-by: Patrik Oldsberg <poldsberg@gmail.com> Co-authored-by: Fredrik Adelöw <freben@users.noreply.github.com> Co-authored-by: Camila Belo <camilaibs@users.noreply.github.com> Co-authored-by: Johan Haals <johan@haals.se> Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
import React from 'react';
|
||||
import { MemoryRouter, Link } from 'react-router-dom';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { RenderResult, render } from '@testing-library/react';
|
||||
import { createSpecializedApp } from '@backstage/frontend-app-api';
|
||||
import {
|
||||
@@ -24,15 +24,10 @@ import {
|
||||
Extension,
|
||||
ExtensionDataRef,
|
||||
ExtensionDefinition,
|
||||
IconComponent,
|
||||
RouteRef,
|
||||
coreExtensionData,
|
||||
createExtension,
|
||||
createExtensionInput,
|
||||
createExtensionOverrides,
|
||||
createNavItemExtension,
|
||||
createRouterExtension,
|
||||
useRouteRef,
|
||||
} from '@backstage/frontend-plugin-api';
|
||||
import { Config, ConfigReader } from '@backstage/config';
|
||||
import { JsonArray, JsonObject, JsonValue } from '@backstage/types';
|
||||
@@ -215,14 +210,12 @@ export class ExtensionTester {
|
||||
*/
|
||||
render(options?: { config?: JsonObject }): RenderResult {
|
||||
const { config = {} } = options ?? {};
|
||||
|
||||
const [subject] = this.#extensions;
|
||||
if (!subject) {
|
||||
throw new Error(
|
||||
'No subject found. At least one extension should be added to the tester.',
|
||||
);
|
||||
}
|
||||
|
||||
const app = createSpecializedApp({
|
||||
features: [
|
||||
createExtensionOverrides({
|
||||
@@ -239,7 +232,6 @@ export class ExtensionTester {
|
||||
],
|
||||
config: this.#getConfig(config),
|
||||
});
|
||||
|
||||
return render(app.createRoot());
|
||||
}
|
||||
|
||||
|
||||
@@ -15,25 +15,23 @@
|
||||
*/
|
||||
|
||||
import React from 'react';
|
||||
import {
|
||||
ExtensionDefinition,
|
||||
IconComponent,
|
||||
RouteRef,
|
||||
coreExtensionData,
|
||||
createExtension,
|
||||
createExtensionInput,
|
||||
createExtensionOverrides,
|
||||
createNavItemExtension,
|
||||
createRouterExtension,
|
||||
useRouteRef,
|
||||
} from '@backstage/frontend-plugin-api';
|
||||
import { Link, MemoryRouter } from 'react-router-dom';
|
||||
import { createSpecializedApp } from '@backstage/frontend-app-api';
|
||||
import { render } from '@testing-library/react';
|
||||
import { resolveExtensionDefinition } from '@backstage/frontend-plugin-api/src/wiring/resolveExtensionDefinition';
|
||||
import { resolve } from 'path';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import { JsonObject } from '@backstage/types';
|
||||
import {
|
||||
createExtension,
|
||||
createExtensionOverrides,
|
||||
createRouterExtension,
|
||||
ExtensionDefinition,
|
||||
coreExtensionData,
|
||||
RouteRef,
|
||||
useRouteRef,
|
||||
createExtensionInput,
|
||||
IconComponent,
|
||||
createNavItemExtension,
|
||||
} from '@backstage/frontend-plugin-api';
|
||||
|
||||
/**
|
||||
* Options to customize the behavior of the test app.
|
||||
@@ -120,26 +118,35 @@ const TestAppNavExtension = createExtension({
|
||||
* Renders the given element in a test app, for use in unit tests.
|
||||
*/
|
||||
export function renderInTestApp(
|
||||
element: JSX.Element,
|
||||
element: JSX.Element | { extensions: ExtensionDefinition<any, any>[] },
|
||||
options?: TestAppOptions,
|
||||
) {
|
||||
const extensions: Array<ExtensionDefinition<any, any>> = [
|
||||
createExtension({
|
||||
namespace: 'test',
|
||||
attachTo: { id: 'app/routes', input: 'routes' },
|
||||
output: [coreExtensionData.reactElement, coreExtensionData.routePath],
|
||||
factory: () => {
|
||||
return [
|
||||
coreExtensionData.reactElement(element),
|
||||
coreExtensionData.routePath('/'),
|
||||
const extensions: Array<ExtensionDefinition<any, any>> =
|
||||
'extensions' in element
|
||||
? element.extensions
|
||||
: [
|
||||
createExtension({
|
||||
namespace: 'test',
|
||||
attachTo: { id: 'app/routes', input: 'routes' },
|
||||
output: [
|
||||
coreExtensionData.reactElement,
|
||||
coreExtensionData.routePath,
|
||||
],
|
||||
factory: () => {
|
||||
return [
|
||||
coreExtensionData.reactElement(element),
|
||||
coreExtensionData.routePath('/'),
|
||||
];
|
||||
},
|
||||
}),
|
||||
createRouterExtension({
|
||||
namespace: 'test',
|
||||
Component: ({ children }) => (
|
||||
<MemoryRouter>{children}</MemoryRouter>
|
||||
),
|
||||
}),
|
||||
TestAppNavExtension,
|
||||
];
|
||||
},
|
||||
}),
|
||||
createRouterExtension({
|
||||
namespace: 'test',
|
||||
Component: ({ children }) => <MemoryRouter>{children}</MemoryRouter>,
|
||||
}),
|
||||
];
|
||||
|
||||
if (options?.mountedRoutes) {
|
||||
for (const [path, routeRef] of Object.entries(options.mountedRoutes)) {
|
||||
|
||||
Reference in New Issue
Block a user