From 02941e9c6efaf8a9d14417a4c78dcc1d91c01139 Mon Sep 17 00:00:00 2001 From: Hellgren Heikki Date: Tue, 4 Feb 2025 13:27:43 +0200 Subject: [PATCH] test(scaffolder): add ongoing task title rendering test Signed-off-by: Hellgren Heikki --- .../OngoingTask/OngoingTask.test.tsx | 17 +++++++++++++++-- .../src/components/OngoingTask/OngoingTask.tsx | 18 ++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/plugins/scaffolder/src/components/OngoingTask/OngoingTask.test.tsx b/plugins/scaffolder/src/components/OngoingTask/OngoingTask.test.tsx index 371757852f..7a04854c6f 100644 --- a/plugins/scaffolder/src/components/OngoingTask/OngoingTask.test.tsx +++ b/plugins/scaffolder/src/components/OngoingTask/OngoingTask.test.tsx @@ -48,7 +48,10 @@ jest.mock('@backstage/plugin-scaffolder-react', () => ({ task: { spec: { steps: [], - templateInfo: { entity: { metadata: { name: 'my-template' } } }, + templateInfo: { + entityRef: 'template:default/my-template', + entity: { metadata: { name: 'my-template' } }, + }, }, }, }), @@ -60,7 +63,11 @@ describe('OngoingTask', () => { getTask: jest.fn().mockImplementation(async () => {}), }; - const mockEntityPresentationApi = {}; + const mockEntityPresentationApi = { + forEntity: jest.fn().mockReturnValue({ + promise: new Promise(resolve => resolve({ primaryTitle: 'My template' })), + }), + }; beforeEach(async () => { jest.clearAllMocks(); @@ -83,6 +90,12 @@ describe('OngoingTask', () => { { mountedRoutes: { '/': rootRouteRef } }, ); }; + + it('should render title', async () => { + const rendered = await render(); + expect(rendered.getByText('My template')).toBeInTheDocument(); + }); + it('should trigger cancel api on "Cancel" click in context menu', async () => { const rendered = await render(); const cancelOptionLabel = 'Cancel'; diff --git a/plugins/scaffolder/src/components/OngoingTask/OngoingTask.tsx b/plugins/scaffolder/src/components/OngoingTask/OngoingTask.tsx index 9fff6b51ad..06620e7280 100644 --- a/plugins/scaffolder/src/components/OngoingTask/OngoingTask.tsx +++ b/plugins/scaffolder/src/components/OngoingTask/OngoingTask.tsx @@ -44,6 +44,7 @@ import { import { useTranslationRef } from '@backstage/core-plugin-api/alpha'; import { scaffolderTranslationRef } from '../../translation'; import { entityPresentationApiRef } from '@backstage/plugin-catalog-react'; +import { default as reactUseAsync } from 'react-use/esm/useAsync'; const useStyles = makeStyles(theme => ({ contentWrapper: { @@ -82,7 +83,6 @@ export const OngoingTask = (props: { const scaffolderApi = useApi(scaffolderApiRef); const entityPresentationApi = useApi(entityPresentationApiRef); const taskStream = useTaskEventStream(taskId!); - const [templateName, setTemplateName] = useState(); const classes = useStyles(); const steps = useMemo( () => @@ -126,13 +126,12 @@ export const OngoingTask = (props: { } }, [taskStream.error, taskStream.completed]); - useEffect(() => { + const { value: presentation } = reactUseAsync(async () => { const templateEntityRef = taskStream.task?.spec.templateInfo?.entityRef; if (!templateEntityRef) { - return; + return undefined; } - const presentation = entityPresentationApi.forEntity(templateEntityRef); - setTemplateName(presentation.snapshot.primaryTitle); + return entityPresentationApi.forEntity(templateEntityRef).promise; }, [entityPresentationApi, taskStream.task?.spec.templateInfo?.entityRef]); const activeStep = useMemo(() => { @@ -202,13 +201,16 @@ export const OngoingTask = (props: {
- {t('ongoingTask.title')} {templateName} + {t('ongoingTask.title')}{' '} + {presentation ? presentation.primaryTitle : ''} } subtitle={t('ongoingTask.subtitle', { taskId: taskId as string })}