From 880ee2f353c3aac71b1698b612c2ab71e0c0419d Mon Sep 17 00:00:00 2001 From: Paul Cowan Date: Mon, 12 Dec 2022 17:46:49 +0000 Subject: [PATCH] add EmbeddedScaffolderWorkflow test Signed-off-by: Paul Cowan --- .../EmbeddedScaffolderWorkflow.test.tsx | 130 +++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) diff --git a/plugins/scaffolder/src/next/EmbeddedScaffolderWorkflow/EmbeddedScaffolderWorkflow.test.tsx b/plugins/scaffolder/src/next/EmbeddedScaffolderWorkflow/EmbeddedScaffolderWorkflow.test.tsx index e3ac127f68..ef35f0caef 100644 --- a/plugins/scaffolder/src/next/EmbeddedScaffolderWorkflow/EmbeddedScaffolderWorkflow.test.tsx +++ b/plugins/scaffolder/src/next/EmbeddedScaffolderWorkflow/EmbeddedScaffolderWorkflow.test.tsx @@ -13,6 +13,134 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +import { ApiProvider } from '@backstage/core-app-api'; +import { + MockAnalyticsApi, + renderInTestApp, + TestApiRegistry, +} from '@backstage/test-utils'; +import { act, fireEvent } from '@testing-library/react'; +import React from 'react'; +import { EmbeddedScaffolderWorkflow } from './EmbeddedScaffolderWorkflow'; +import { scaffolderApiRef } from '../../api'; +import { ScaffolderApi } from '../../types'; +import { analyticsApiRef } from '@backstage/core-plugin-api'; + +const scaffolderApiMock: jest.Mocked = { + scaffold: jest.fn(), + getTemplateParameterSchema: jest.fn(), + getIntegrationsList: jest.fn(), + getTask: jest.fn(), + streamLogs: jest.fn(), + listActions: jest.fn(), + listTasks: jest.fn(), +}; + +const analyticsMock = new MockAnalyticsApi(); +const apis = TestApiRegistry.from( + [scaffolderApiRef, scaffolderApiMock], + [analyticsApiRef, analyticsMock], +); + describe('', () => { - it('should embed workflow inside another component', () => {}); + it('should embed workflow inside another component', async () => { + const onComplete = jest.fn(); + const onError = jest.fn(); + scaffolderApiMock.scaffold.mockResolvedValue({ taskId: 'xyz' }); + + scaffolderApiMock.getTemplateParameterSchema.mockResolvedValue({ + steps: [ + { + title: 'Step 1', + schema: { + properties: { + name: { + type: 'string', + }, + }, + }, + }, + ], + title: 'React JSON Schema Form Test', + }); + + const { getByRole, getAllByRole, getByText } = await renderInTestApp( + + +

Security Insights

+

+ Security insights actionable advice to improve security posture + of your application +

+

+ You must complete on-boarding process to activate security + insights on this project. +

+ + } + finishPage={ + <> +

Congratulations, this application is complete!

+ + } + ReviewStateWrapper={() => ( +

This is a different wrapper for the review page

+ )} + /> +
, + ); + + // frontPage is rendered + expect(getByRole('heading').innerHTML).toBe('Security Insights'); + + // move to workflow + await act(async () => { + fireEvent.click(getByRole('button')); + }); + + // Test template title is overriden + expect(getByRole('heading').innerHTML).toBe( + 'Different title than template', + ); + + expect(getByRole('textbox').innerHTML).toBeDefined(); + + const input = getByRole('textbox') as HTMLInputElement; + + // The initial state of the form can be set + expect(input.value).toBe('prefilled-name'); + + await act(async () => { + fireEvent.click(getAllByRole('button')[1] as HTMLButtonElement); + }); + + // Can supply a different Review wrapper + expect( + getByText('This is a different wrapper for the review page'), + ).toBeDefined(); + + await act(async () => { + fireEvent.click(getAllByRole('button')[1] as HTMLButtonElement); + }); + + // the final page is inserted after the workflow + expect( + getByText('Congratulations, this application is complete!'), + ).toBeDefined(); + }); });