diff --git a/packages/frontend-test-utils/api-report.md b/packages/frontend-test-utils/api-report.md index 14e1c68c4c..36fc5e30b6 100644 --- a/packages/frontend-test-utils/api-report.md +++ b/packages/frontend-test-utils/api-report.md @@ -3,6 +3,8 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts +/// + import { ErrorWithContext } from '@backstage/test-utils'; import { ExtensionDefinition } from '@backstage/frontend-plugin-api'; import { JsonObject } from '@backstage/types'; @@ -63,6 +65,9 @@ export { MockStorageApi }; export { MockStorageBucket }; +// @public +export function renderInTestApp(element: JSX.Element): RenderResult; + export { setupRequestMockHandlers }; export { TestApiProvider }; diff --git a/packages/frontend-test-utils/src/app/index.ts b/packages/frontend-test-utils/src/app/index.ts index 3152fac6e7..3f19146c5c 100644 --- a/packages/frontend-test-utils/src/app/index.ts +++ b/packages/frontend-test-utils/src/app/index.ts @@ -18,3 +18,5 @@ export { createExtensionTester, type ExtensionTester, } from './createExtensionTester'; + +export { renderInTestApp } from './renderInTestApp'; diff --git a/packages/frontend-test-utils/src/app/renderInTestApp.test.tsx b/packages/frontend-test-utils/src/app/renderInTestApp.test.tsx new file mode 100644 index 0000000000..e13d1157ef --- /dev/null +++ b/packages/frontend-test-utils/src/app/renderInTestApp.test.tsx @@ -0,0 +1,27 @@ +/* + * Copyright 2023 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 React from 'react'; +import { screen } from '@testing-library/react'; +import { renderInTestApp } from './renderInTestApp'; + +describe('renderInTestApp', () => { + it('should render the given component in a page', async () => { + const Component = () =>
Test
; + renderInTestApp(); + expect(screen.getByText('Test')).toBeInTheDocument(); + }); +}); diff --git a/packages/frontend-test-utils/src/app/renderInTestApp.tsx b/packages/frontend-test-utils/src/app/renderInTestApp.tsx new file mode 100644 index 0000000000..f4011535a4 --- /dev/null +++ b/packages/frontend-test-utils/src/app/renderInTestApp.tsx @@ -0,0 +1,38 @@ +/* + * Copyright 2023 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 { + coreExtensionData, + createExtension, +} from '@backstage/frontend-plugin-api'; +import { createExtensionTester } from './createExtensionTester'; + +/** + * @public + * Renders the given element in a test app, for use in unit tests. + */ +export function renderInTestApp(element: JSX.Element) { + const extension = createExtension({ + namespace: 'test', + attachTo: { id: 'core', input: 'root' }, + output: { + element: coreExtensionData.reactElement, + }, + factory: () => ({ element }), + }); + const tester = createExtensionTester(extension); + return tester.render(); +} diff --git a/yarn.lock b/yarn.lock index 0d8d076b45..9759cf8787 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3916,7 +3916,7 @@ __metadata: languageName: node linkType: hard -"@backstage/core-components@npm:^0.13.7, @backstage/core-components@npm:^0.13.8": +"@backstage/core-components@npm:^0.13.8": version: 0.13.8 resolution: "@backstage/core-components@npm:0.13.8" dependencies: @@ -4042,7 +4042,7 @@ __metadata: languageName: unknown linkType: soft -"@backstage/core-plugin-api@npm:^1.3.0, @backstage/core-plugin-api@npm:^1.5.0, @backstage/core-plugin-api@npm:^1.7.0, @backstage/core-plugin-api@npm:^1.8.0": +"@backstage/core-plugin-api@npm:^1.3.0, @backstage/core-plugin-api@npm:^1.5.0, @backstage/core-plugin-api@npm:^1.8.0": version: 1.8.0 resolution: "@backstage/core-plugin-api@npm:1.8.0" dependencies: @@ -5950,7 +5950,7 @@ __metadata: languageName: unknown linkType: soft -"@backstage/plugin-catalog-react@npm:^1.2.4, @backstage/plugin-catalog-react@npm:^1.8.5, @backstage/plugin-catalog-react@npm:^1.9.1": +"@backstage/plugin-catalog-react@npm:^1.2.4, @backstage/plugin-catalog-react@npm:^1.9.1": version: 1.9.1 resolution: "@backstage/plugin-catalog-react@npm:1.9.1" dependencies: