From b46d34cfe1d9fe0cdf5839cb59f0a4d7bd3dd840 Mon Sep 17 00:00:00 2001 From: David Ovrelid Date: Fri, 31 May 2024 06:59:53 +0200 Subject: [PATCH 01/15] Moved API add mutations up to app-development, and started to do the same for remove mutations --- .../features/processEditor/ProcessEditor.tsx | 2 + .../handlers/OnProcessTaskAddHandler.test.ts | 122 +++++++++++++++--- .../handlers/OnProcessTaskAddHandler.ts | 76 ++++++++++- .../handlers/OnProcessTaskRemoveHandler.ts | 35 +++++ .../mutations/useAddLayoutSetMutation.ts | 16 ++- .../src/hooks/useBpmnEditor.test.tsx | 15 ++- .../process-editor/src/hooks/useBpmnEditor.ts | 56 ++++---- .../process-editor/src/types/OnProcessTask.ts | 3 +- .../AddProcessTaskManager.test.ts | 107 --------------- .../AddProcessTaskManager.ts | 97 -------------- .../src/utils/ProcessTaskManager/index.ts | 1 - .../src/utils/hookUtils/hookUtils.ts | 5 +- 12 files changed, 274 insertions(+), 261 deletions(-) delete mode 100644 frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.test.ts delete mode 100644 frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.ts diff --git a/frontend/app-development/features/processEditor/ProcessEditor.tsx b/frontend/app-development/features/processEditor/ProcessEditor.tsx index 96e4329d79b..c489a947844 100644 --- a/frontend/app-development/features/processEditor/ProcessEditor.tsx +++ b/frontend/app-development/features/processEditor/ProcessEditor.tsx @@ -116,7 +116,9 @@ export const ProcessEditor = (): React.ReactElement => { org, app, currentPolicy, + addLayoutSet, mutateApplicationPolicy, + addDataTypeToAppMetadata, ); onProcessTaskAddHandler.handleOnProcessTaskAdd(taskMetadata); }; diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts index 7bdb416e853..13a40741906 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts @@ -1,25 +1,78 @@ import type { Policy } from 'app-shared/types/Policy'; import type { OnProcessTaskEvent } from '@altinn/process-editor/types/OnProcessTask'; -import type { TaskEvent } from '@altinn/process-editor/utils/ProcessTaskManager'; +import { TaskEvent } from '@altinn/process-editor/utils/ProcessTaskManager'; import { OnProcessTaskAddHandler } from './OnProcessTaskAddHandler'; +import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; + +const orgMock = 'testOrg'; +const appMock = 'testApp'; +const currentPolicyMock: Policy = { + requiredAuthenticationLevelOrg: '3', + requiredAuthenticationLevelEndUser: '3', + rules: [], +}; +const addLayoutSetMock = jest.fn(); +const mutateApplicationPolicyMock = jest.fn(); +const addDataTypeToAppMetadataMock = jest.fn(); + +const createOnProcessTaskHandler = () => + new OnProcessTaskAddHandler( + orgMock, + appMock, + currentPolicyMock, + addLayoutSetMock, + mutateApplicationPolicyMock, + addDataTypeToAppMetadataMock, + ); + +const createTaskEvent = (extensionConfig?: object): TaskEvent => + ({ + element: { + id: 'testId', + businessObject: { + id: 'testEventId', + $type: BpmnTypeEnum.Task, + extensionElements: extensionConfig ? { values: [extensionConfig] } : undefined, + }, + }, + }) as TaskEvent; describe('OnProcessTaskAddHandler', () => { - it('should add default payment policy to current policy when task type is payment', () => { - const org = 'testOrg'; - const app = 'testApp'; - const currentPolicy: Policy = { - requiredAuthenticationLevelOrg: '3', - requiredAuthenticationLevelEndUser: '3', - rules: [], - }; + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should add layoutSet when data-task is added', () => { + const onProcessTaskAddHandler = createOnProcessTaskHandler(); + + onProcessTaskAddHandler.handleOnProcessTaskAdd({ + taskEvent: createTaskEvent(), + taskType: 'data', + }); - const mutateApplicationPolicy = jest.fn(); + expect(addLayoutSetMock).toHaveBeenCalledWith({ + layoutSetConfig: { id: 'testId', tasks: ['testId'] }, + layoutSetIdToUpdate: 'testId', + }); + }); + + it('should add layoutSet, dataType and default policy when payment task is added', () => { const taskMetadata: OnProcessTaskEvent = { taskType: 'payment', taskEvent: { element: { id: 'testElementId', - businessObject: {}, + businessObject: { + id: 'testEventId', + $type: BpmnTypeEnum.Task, + extensionElements: { + values: [ + { + paymentConfig: { paymentDataType: 'paymentInformation' }, + }, + ], + }, + }, }, } as TaskEvent, }; @@ -41,14 +94,47 @@ describe('OnProcessTaskAddHandler', () => { ], }; - const onProcessTaskAddHandler = new OnProcessTaskAddHandler( - org, - app, - currentPolicy, - mutateApplicationPolicy, - ); + const onProcessTaskAddHandler = createOnProcessTaskHandler(); + onProcessTaskAddHandler.handleOnProcessTaskAdd(taskMetadata); + + expect(addLayoutSetMock).toHaveBeenCalledWith({ + layoutSetConfig: { + id: 'testElementId', + tasks: ['testElementId'], + }, + layoutSetIdToUpdate: 'testElementId', + }); + expect(addDataTypeToAppMetadataMock).toHaveBeenCalledWith({ dataTypeId: 'paymentInformation' }); + expect(mutateApplicationPolicyMock).toHaveBeenCalledWith(expectedResponse); + }); + + it('should add datatype when signing task is added', () => { + const onProcessTaskAddHandler = createOnProcessTaskHandler(); + + const taskMetadata: OnProcessTaskEvent = { + taskType: 'signing', + taskEvent: { + element: { + id: 'testElementId', + businessObject: { + id: 'testEventId', + $type: BpmnTypeEnum.Task, + extensionElements: { + values: [ + { + signatureConfig: { signatureDataType: 'signingInformation' }, + }, + ], + }, + }, + }, + } as TaskEvent, + }; onProcessTaskAddHandler.handleOnProcessTaskAdd(taskMetadata); - expect(mutateApplicationPolicy).toHaveBeenCalledWith(expectedResponse); + + expect(addDataTypeToAppMetadataMock).toHaveBeenCalledWith({ + dataTypeId: 'signingInformation', + }); }); }); diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts index 91115313861..0edba3a29af 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts @@ -1,13 +1,18 @@ -import type { OnProcessTaskEvent } from '@altinn/process-editor/types/OnProcessTask'; import { PaymentPolicyBuilder } from '../../../utils/policy'; +import type { OnProcessTaskEvent } from '@altinn/process-editor/types/OnProcessTask'; import type { Policy } from 'app-shared/types/Policy'; +import type { LayoutSetConfig } from 'app-shared/types/api/LayoutSetsResponse'; +import { getDataTypeIdFromBusinessObject } from '@altinn/process-editor/utils/hookUtils/hookUtils'; +import { AddLayoutSetMutation } from '../../../hooks/mutations/useAddLayoutSetMutation'; export class OnProcessTaskAddHandler { constructor( private readonly org: string, private readonly app: string, private readonly currentPolicy: Policy, + private readonly addLayoutSet: AddLayoutSetMutation, private readonly mutateApplicationPolicy: (policy: Policy) => void, + private readonly addDataTypeToAppMetadata: (data: { dataTypeId: string }) => void, ) {} /** @@ -15,12 +20,48 @@ export class OnProcessTaskAddHandler { * @param taskMetadata */ public handleOnProcessTaskAdd(taskMetadata: OnProcessTaskEvent): void { + if (taskMetadata.taskType === 'data') { + this.handleDataTaskAdd(taskMetadata); + } + if (taskMetadata.taskType === 'payment') { this.handlePaymentTaskAdd(taskMetadata); } + + if (taskMetadata.taskType === 'signing') { + this.handleSigningTaskAdd(taskMetadata); + } + } + + /** + * Adds a layout set to the added data task + * @param taskMetadata + * @private + */ + private handleDataTaskAdd(taskMetadata: OnProcessTaskEvent): void { + this.addLayoutSet(this.createLayoutSetConfig(taskMetadata.taskEvent)); } + /** + * Adds a dataType, layoutSet and default policy to the added payment task + * @param taskMetadata + * @private + */ private handlePaymentTaskAdd(taskMetadata: OnProcessTaskEvent): void { + // Add layoutSet for the task + this.addLayoutSet(this.createLayoutSetConfig(taskMetadata.taskEvent)); + + // Add dataType + const dataTypeId = getDataTypeIdFromBusinessObject( + taskMetadata.taskType, + taskMetadata.taskEvent.element.businessObject, + ); + + this.addDataTypeToAppMetadata({ + dataTypeId, + }); + + // Add default policy const paymentPolicyBuilder = new PaymentPolicyBuilder(this.org, this.app); const defaultPaymentPolicy = paymentPolicyBuilder.getDefaultPaymentPolicy( taskMetadata.taskEvent.element.id, @@ -32,4 +73,37 @@ export class OnProcessTaskAddHandler { rules: [...this.currentPolicy.rules, ...defaultPaymentPolicy.rules], }); } + + /** + * Adds a dataType to the added signing task + * @param taskMetadata + * @private + */ + private handleSigningTaskAdd(taskMetadata: OnProcessTaskEvent): void { + // Add DataType + const dataTypeId = getDataTypeIdFromBusinessObject( + taskMetadata.taskType, + taskMetadata.taskEvent.element.businessObject, + ); + + this.addDataTypeToAppMetadata({ + dataTypeId, + }); + } + + /** + * Creates the layout set config for the task + * @returns {{layoutSetIdToUpdate: string, layoutSetConfig: LayoutSetConfig}} + * @private + */ + private createLayoutSetConfig(taskEvent: OnProcessTaskEvent['taskEvent']): { + layoutSetIdToUpdate: string; + layoutSetConfig: LayoutSetConfig; + } { + const elementId = taskEvent.element.id; + return { + layoutSetIdToUpdate: elementId, + layoutSetConfig: { id: elementId, tasks: [elementId] }, + }; + } } diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts index 2a63a6f2ff7..4c7a4f8f578 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts @@ -1,13 +1,21 @@ import type { Policy } from 'app-shared/types/Policy'; import type { OnProcessTaskEvent } from '@altinn/process-editor/types/OnProcessTask'; import { PaymentPolicyBuilder } from '../../../utils/policy'; +import { + getDataTypeIdFromBusinessObject, + getLayoutSetIdFromTaskId, +} from '@altinn/process-editor/utils/hookUtils/hookUtils'; +import { LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; export class OnProcessTaskRemoveHandler { constructor( private readonly org: string, private readonly app: string, private readonly currentPolicy: Policy, + private readonly layoutSets: LayoutSets, private readonly mutateApplicationPolicy: (policy: Policy) => void, + private readonly deleteDataTypeFromAppMetadata: (data: { dataTypeId: string }) => void, + private readonly deleteLayoutSet: (data: { layoutSetIdToUpdate: string }) => void, ) {} /** @@ -20,7 +28,23 @@ export class OnProcessTaskRemoveHandler { } } + /** + * Deletes the dataType, layoutSet and policy for the deleted payment task + * @param taskMetadata + * @private + */ private handlePaymentTaskRemove(taskMetadata: OnProcessTaskEvent): void { + // Delete dataType + const dataTypeId = getDataTypeIdFromBusinessObject( + taskMetadata.taskType, + taskMetadata.taskEvent.element.businessObject, + ); + + this.deleteDataTypeFromAppMetadata({ + dataTypeId, + }); + + // Delete policy const paymentPolicyBuilder = new PaymentPolicyBuilder(this.org, this.app); const currentPaymentRuleId = paymentPolicyBuilder.getPolicyRuleId( taskMetadata.taskEvent.element.id, @@ -33,5 +57,16 @@ export class OnProcessTaskRemoveHandler { }; this.mutateApplicationPolicy(updatedPolicy); + + // Delete layout set + const layoutSetId = getLayoutSetIdFromTaskId( + taskMetadata.taskEvent.element.id, + this.layoutSets, + ); + if (layoutSetId) { + this.deleteLayoutSet({ + layoutSetIdToUpdate: layoutSetId, + }); + } } } diff --git a/frontend/app-development/hooks/mutations/useAddLayoutSetMutation.ts b/frontend/app-development/hooks/mutations/useAddLayoutSetMutation.ts index 9fa216a66d7..320c8ff8127 100644 --- a/frontend/app-development/hooks/mutations/useAddLayoutSetMutation.ts +++ b/frontend/app-development/hooks/mutations/useAddLayoutSetMutation.ts @@ -1,9 +1,21 @@ -import { useMutation, useQueryClient } from '@tanstack/react-query'; +import { type UseMutateFunction, useMutation, useQueryClient } from '@tanstack/react-query'; import { useServicesContext } from 'app-shared/contexts/ServicesContext'; import { QueryKey } from 'app-shared/types/QueryKey'; import type { LayoutSetConfig, LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; import { useLocalStorage } from 'app-shared/hooks/useLocalStorage'; -import type { LayoutSetsResponse } from 'app-shared/types/api/AddLayoutSetResponse'; +import type { + AddLayoutSetResponse, + LayoutSetsResponse, +} from 'app-shared/types/api/AddLayoutSetResponse'; + +export type AddLayoutSetMutation = UseMutateFunction< + { + layoutSets: AddLayoutSetResponse; + layoutSetConfig: LayoutSetConfig; + }, + Error, + { layoutSetIdToUpdate: string; layoutSetConfig: LayoutSetConfig } +>; const isLayoutSets = (obj: LayoutSetsResponse): obj is LayoutSets => { if (obj === undefined || !(obj instanceof Object)) return false; diff --git a/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx b/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx index f65d13097d8..4c252966418 100644 --- a/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx +++ b/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx @@ -139,7 +139,7 @@ describe('useBpmnEditor', () => { }); it('should handle "shape.remove" event', async () => { - renderUseBpmnEditor(false, 'shape.remove'); + renderUseBpmnEditor(true, 'shape.remove'); const handleTaskRemoveMock = jest.fn(); (RemoveProcessTaskManager as jest.Mock).mockImplementation(() => ({ @@ -147,6 +147,19 @@ describe('useBpmnEditor', () => { })); await waitFor(() => expect(handleTaskRemoveMock).toHaveBeenCalledTimes(1)); + await waitFor(() => expect(setBpmnDetailsMock).toHaveBeenCalledWith(null)); + }); + + it('should handle "shape.add" event', async () => { + renderUseBpmnEditor(true, 'shape.remove'); + + const handleTaskRemoveMock = jest.fn(); + (RemoveProcessTaskManager as jest.Mock).mockImplementation(() => ({ + handleAdd: () => handleTaskRemoveMock(), + })); + + await waitFor(() => expect(handleTaskRemoveMock).toHaveBeenCalledTimes(1)); + await waitFor(() => expect(setBpmnDetailsMock).toHaveBeenCalledWith(null)); }); it('should call setBpmnDetails when "element.click" event is triggered on eventBus', () => { diff --git a/frontend/packages/process-editor/src/hooks/useBpmnEditor.ts b/frontend/packages/process-editor/src/hooks/useBpmnEditor.ts index b158d7088d3..6888585fcda 100644 --- a/frontend/packages/process-editor/src/hooks/useBpmnEditor.ts +++ b/frontend/packages/process-editor/src/hooks/useBpmnEditor.ts @@ -5,11 +5,7 @@ import { useBpmnModeler } from './useBpmnModeler'; import { getBpmnEditorDetailsFromBusinessObject } from '../utils/hookUtils'; import { useBpmnConfigPanelFormContext } from '../contexts/BpmnConfigPanelContext'; import { useBpmnApiContext } from '../contexts/BpmnApiContext'; -import { - AddProcessTaskManager, - RemoveProcessTaskManager, - type TaskEvent, -} from '../utils/ProcessTaskManager'; +import { RemoveProcessTaskManager, type TaskEvent } from '../utils/ProcessTaskManager'; // Wrapper around bpmn-js to Reactify it @@ -25,9 +21,7 @@ export const useBpmnEditor = (): UseBpmnViewerResult => { const { getModeler, destroyModeler } = useBpmnModeler(); const { - addLayoutSet, deleteLayoutSet, - addDataTypeToAppMetadata, deleteDataTypeFromAppMetadata, saveBpmn, layoutSets, @@ -36,10 +30,10 @@ export const useBpmnEditor = (): UseBpmnViewerResult => { } = useBpmnApiContext(); // Needs to update the layoutSetsRef.current when layoutSets changes to avoid staled data in the event listeners - const layoutSetsRef = useRef(layoutSets); - useEffect(() => { - layoutSetsRef.current = layoutSets; - }, [layoutSets]); + // const layoutSetsRef = useRef(layoutSets); + // useEffect(() => { + // layoutSetsRef.current = layoutSets; + // }, [layoutSets]); const handleCommandStackChanged = async () => { saveBpmn(await getUpdatedXml(), metaDataFormRef.current || null); @@ -48,28 +42,28 @@ export const useBpmnEditor = (): UseBpmnViewerResult => { const handleShapeAdd = (taskEvent: TaskEvent): void => { const bpmnDetails = getBpmnEditorDetailsFromBusinessObject(taskEvent?.element?.businessObject); - const addProcessTaskManager = new AddProcessTaskManager( - addLayoutSet, - addDataTypeToAppMetadata, - bpmnDetails, - onProcessTaskAdd, - ); - - addProcessTaskManager.handleTaskAdd(taskEvent); + onProcessTaskAdd({ + taskEvent, + taskType: bpmnDetails.taskType, + }); updateBpmnDetailsByTaskEvent(taskEvent); }; const handleShapeRemove = (taskEvent: TaskEvent): void => { const bpmnDetails = getBpmnEditorDetailsFromBusinessObject(taskEvent?.element?.businessObject); - const removeProcessTaskManager = new RemoveProcessTaskManager( - layoutSetsRef.current, - deleteLayoutSet, - deleteDataTypeFromAppMetadata, - bpmnDetails, - onProcessTaskRemove, - ); - - removeProcessTaskManager.handleTaskRemove(taskEvent); + // const removeProcessTaskManager = new RemoveProcessTaskManager( + // layoutSetsRef.current, + // deleteLayoutSet, + // deleteDataTypeFromAppMetadata, + // bpmnDetails, + // onProcessTaskRemove, + // ); + // + // removeProcessTaskManager.handleTaskRemove(taskEvent); + onProcessTaskRemove({ + taskEvent, + taskType: bpmnDetails.taskType, + }); setBpmnDetails(null); }; @@ -95,13 +89,13 @@ export const useBpmnEditor = (): UseBpmnViewerResult => { }; const initializeBpmnChanges = () => { - modelerRef.current.on('commandStack.changed', async () => { + modelerRef.current.on('commandStack.changed', async (): Promise => { await handleCommandStackChanged(); }); - modelerRef.current.on('shape.add', (taskEvent: TaskEvent) => { + modelerRef.current.on('shape.add', (taskEvent: TaskEvent): void => { handleShapeAdd(taskEvent); }); - modelerRef.current.on('shape.remove', (taskEvent: TaskEvent) => { + modelerRef.current.on('shape.remove', (taskEvent: TaskEvent): void => { handleShapeRemove(taskEvent); }); }; diff --git a/frontend/packages/process-editor/src/types/OnProcessTask.ts b/frontend/packages/process-editor/src/types/OnProcessTask.ts index 50da2236fc3..a19ee110b33 100644 --- a/frontend/packages/process-editor/src/types/OnProcessTask.ts +++ b/frontend/packages/process-editor/src/types/OnProcessTask.ts @@ -1,6 +1,7 @@ import type { TaskEvent } from '@altinn/process-editor/utils/ProcessTaskManager'; +import { BpmnTaskType } from '@altinn/process-editor/types/BpmnTaskType'; export type OnProcessTaskEvent = { taskEvent?: TaskEvent; - taskType: string; + taskType: BpmnTaskType; }; diff --git a/frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.test.ts b/frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.test.ts deleted file mode 100644 index bea3fb59bd3..00000000000 --- a/frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.test.ts +++ /dev/null @@ -1,107 +0,0 @@ -import { AddProcessTaskManager } from './AddProcessTaskManager'; -import { BpmnTypeEnum } from '../../enum/BpmnTypeEnum'; -import { type TaskEvent } from '../ProcessTaskManager/types'; -import { type BpmnDetails } from '../../types/BpmnDetails'; -import { type BpmnTaskType } from '../../types/BpmnTaskType'; - -describe('AddProcessTaskManager', () => { - const createBpmnDetails = (id: string, name: string, taskType: BpmnTaskType): BpmnDetails => ({ - id, - name, - taskType, - type: BpmnTypeEnum.Task, - }); - - const createTaskEvent = (taskType: string, extensionConfig?: object): TaskEvent => - ({ - element: { - businessObject: { - id: 'testEventId', - $type: BpmnTypeEnum.Task, - extensionElements: extensionConfig ? { values: [extensionConfig] } : undefined, - }, - }, - }) as TaskEvent; - - const addLayoutSet = jest.fn(); - const addDataTypeToAppMetadata = jest.fn(); - const onProcessTaskAdd = jest.fn(); - - beforeEach(() => { - jest.resetAllMocks(); - }); - - const createAddProcessTaskManager = (bpmnDetails: BpmnDetails) => - new AddProcessTaskManager( - addLayoutSet, - addDataTypeToAppMetadata, - bpmnDetails, - onProcessTaskAdd, - ); - - it('should add layoutSet when data-task is added', () => { - const bpmnDetails = createBpmnDetails('testId', 'dataTask', 'data'); - const addProcessTaskManager = createAddProcessTaskManager(bpmnDetails); - - addProcessTaskManager.handleTaskAdd(createTaskEvent('data')); - - expect(addLayoutSet).toHaveBeenCalledWith({ - layoutSetConfig: { id: 'testId', tasks: ['testId'] }, - layoutSetIdToUpdate: 'testId', - }); - }); - - it('should add layoutSet and dataType when PaymentTask is added', () => { - const bpmnDetails = createBpmnDetails('testId', 'paymentTask', 'payment'); - const addProcessTaskManager = createAddProcessTaskManager(bpmnDetails); - - addProcessTaskManager.handleTaskAdd( - createTaskEvent('payment', { - taskType: 'payment', - paymentConfig: { paymentDataType: 'paymentInformation' }, - }), - ); - - expect(addLayoutSet).toHaveBeenCalledWith({ - layoutSetConfig: { id: 'testId', tasks: ['testId'] }, - layoutSetIdToUpdate: 'testId', - }); - - expect(addDataTypeToAppMetadata).toHaveBeenCalledWith({ - dataTypeId: 'paymentInformation', - }); - }); - - it('should add layoutSet and datatype when signing task is added', () => { - const bpmnDetails = createBpmnDetails('testId', 'signingTask', 'signing'); - const addProcessTaskManager = createAddProcessTaskManager(bpmnDetails); - - addProcessTaskManager.handleTaskAdd( - createTaskEvent('signing', { - taskType: 'signing', - signatureConfig: { signatureDataType: 'signingInformation' }, - }), - ); - - expect(addDataTypeToAppMetadata).toHaveBeenCalledWith({ - dataTypeId: 'signingInformation', - }); - }); - - it('should inform the consumer of the package that a task has been added with the taskEvent and taskType', () => { - const bpmnDetails = createBpmnDetails('testId', 'signingTask', 'signing'); - const addProcessTaskManager = createAddProcessTaskManager(bpmnDetails); - - const taskEvent = createTaskEvent('signing', { - taskType: 'signing', - signatureConfig: { signatureDataType: 'signingInformation' }, - }); - - addProcessTaskManager.handleTaskAdd(taskEvent); - - expect(onProcessTaskAdd).toHaveBeenCalledWith({ - taskEvent, - taskType: 'signing', - }); - }); -}); diff --git a/frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.ts b/frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.ts deleted file mode 100644 index 6dc899b4956..00000000000 --- a/frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { type LayoutSetConfig } from 'app-shared/types/api/LayoutSetsResponse'; -import { getDataTypeIdFromBusinessObject } from '../../utils/hookUtils/hookUtils'; -import { type BpmnApiContextProps } from '../../contexts/BpmnApiContext'; -import { type BpmnDetails } from '../../types/BpmnDetails'; -import { type TaskEvent } from './types'; - -export class AddProcessTaskManager { - constructor( - private readonly addLayoutSet: BpmnApiContextProps['addLayoutSet'], - private readonly addDataTypeToAppMetadata: BpmnApiContextProps['addDataTypeToAppMetadata'], - private readonly bpmnDetails: BpmnDetails, - private readonly onProcessTaskAdd: BpmnApiContextProps['onProcessTaskAdd'], - ) {} - - /** - * Handles the task add event and delegates the handling to the specific task type - * @param taskEvent - */ - public handleTaskAdd(taskEvent: TaskEvent): void { - if (this.bpmnDetails.taskType === 'data') { - this.handleDataTaskAdd(); - } - - if (this.bpmnDetails.taskType === 'payment') { - this.handlePaymentTaskAdd(taskEvent); - } - - if (this.bpmnDetails.taskType === 'signing') { - this.handleSigningTaskAdd(taskEvent); - } - - // Informs the consumer of this package that a task with the given taskEvent and taskType has been added - this.onProcessTaskAdd({ - taskEvent, - taskType: this.bpmnDetails.taskType, - }); - } - - /** - * Adds a layout set to the added data task - * @private - */ - private handleDataTaskAdd(): void { - this.addLayoutSet(this.createLayoutSetConfig()); - } - - /** - * Adds a dataType and layoutSet to the added payment task - * @param taskEvent - * @private - */ - private handlePaymentTaskAdd(taskEvent: TaskEvent): void { - // Add layout set to the task - this.addLayoutSet(this.createLayoutSetConfig()); - - // Add dataType - const dataTypeId = getDataTypeIdFromBusinessObject( - this.bpmnDetails.taskType, - taskEvent.element.businessObject, - ); - - this.addDataTypeToAppMetadata({ - dataTypeId, - }); - } - - /** - * Adds a dataType and layout set to the added signing task - * @param taskEvent - * @private - */ - private handleSigningTaskAdd(taskEvent: TaskEvent): void { - const dataTypeId = getDataTypeIdFromBusinessObject( - this.bpmnDetails.taskType, - taskEvent.element.businessObject, - ); - - this.addDataTypeToAppMetadata({ - dataTypeId, - }); - } - - /** - * Creates the layout set config for the task - * @returns {{layoutSetIdToUpdate: string, layoutSetConfig: LayoutSetConfig}} - * @private - */ - private createLayoutSetConfig(): { - layoutSetIdToUpdate: string; - layoutSetConfig: LayoutSetConfig; - } { - return { - layoutSetIdToUpdate: this.bpmnDetails.id, - layoutSetConfig: { id: this.bpmnDetails.id, tasks: [this.bpmnDetails.id] }, - }; - } -} diff --git a/frontend/packages/process-editor/src/utils/ProcessTaskManager/index.ts b/frontend/packages/process-editor/src/utils/ProcessTaskManager/index.ts index 058d60aef5b..2a40534b777 100644 --- a/frontend/packages/process-editor/src/utils/ProcessTaskManager/index.ts +++ b/frontend/packages/process-editor/src/utils/ProcessTaskManager/index.ts @@ -1,3 +1,2 @@ -export { AddProcessTaskManager } from './AddProcessTaskManager'; export { RemoveProcessTaskManager } from './RemoveProcessTaskManager'; export type { TaskEvent } from './types'; diff --git a/frontend/packages/process-editor/src/utils/hookUtils/hookUtils.ts b/frontend/packages/process-editor/src/utils/hookUtils/hookUtils.ts index 5900912ed05..e3e913ec2ff 100644 --- a/frontend/packages/process-editor/src/utils/hookUtils/hookUtils.ts +++ b/frontend/packages/process-editor/src/utils/hookUtils/hookUtils.ts @@ -65,7 +65,8 @@ export const getDataTypeIdFromBusinessObject = ( return businessObject?.extensionElements?.values[0][configNode][dataTypeName]; }; -export const getLayoutSetIdFromTaskId = (bpmnDetails: BpmnDetails, layoutSets: LayoutSets) => { - const layoutSet = layoutSets.sets.find((set) => set.tasks[0] === bpmnDetails.id); +// TODO remember to update every places where getLayoutSetIdFromTaskId is used to match elementId argument +export const getLayoutSetIdFromTaskId = (elementId: string, layoutSets: LayoutSets) => { + const layoutSet = layoutSets.sets.find((set) => set.tasks[0] === elementId); return layoutSet?.id; }; From cc496502f97c48ddf0a707d1092ebf006bf05192 Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Fri, 31 May 2024 13:52:31 +0200 Subject: [PATCH 02/15] Part 2 for clean up --- .../OnProcessTaskRemoveHandler.test.ts | 131 ++++++++++++++++-- .../handlers/OnProcessTaskRemoveHandler.ts | 57 +++++++- .../process-editor/src/ProcessEditor.test.tsx | 2 - .../process-editor/src/ProcessEditor.tsx | 6 - .../src/contexts/BpmnApiContext.tsx | 7 - .../src/hooks/useBpmnEditor.test.tsx | 15 +- .../process-editor/src/hooks/useBpmnEditor.ts | 26 +--- .../types.ts => types/TaskEvent.ts} | 2 +- .../RemoveProcessTaskManager.test.ts | 118 ---------------- .../RemoveProcessTaskManager.ts | 104 -------------- .../src/utils/ProcessTaskManager/index.ts | 2 - .../test/mocks/bpmnContextMock.ts | 2 - 12 files changed, 185 insertions(+), 287 deletions(-) rename frontend/packages/process-editor/src/{utils/ProcessTaskManager/types.ts => types/TaskEvent.ts} (57%) delete mode 100644 frontend/packages/process-editor/src/utils/ProcessTaskManager/RemoveProcessTaskManager.test.ts delete mode 100644 frontend/packages/process-editor/src/utils/ProcessTaskManager/RemoveProcessTaskManager.ts delete mode 100644 frontend/packages/process-editor/src/utils/ProcessTaskManager/index.ts diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts index 8763dc12f6e..1478fc993d2 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts @@ -1,13 +1,70 @@ import type { Policy } from 'app-shared/types/Policy'; import type { OnProcessTaskEvent } from '@altinn/process-editor/types/OnProcessTask'; -import type { TaskEvent } from '@altinn/process-editor/utils/ProcessTaskManager'; +import { TaskEvent } from '@altinn/process-editor/utils/ProcessTaskManager'; import { OnProcessTaskRemoveHandler } from './OnProcessTaskRemoveHandler'; +import type { LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; +import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; + +const orgMock = 'testOrg'; +const appMock = 'testApp'; +const currentPolicyMock: Policy = { + requiredAuthenticationLevelOrg: '3', + requiredAuthenticationLevelEndUser: '3', + rules: [], +}; +const layoutSetsMock = { + sets: [], +}; + +const mutateApplicationPolicyMock = jest.fn(); +const deleteDataTypeFromAppMetadataMock = jest.fn(); +const deletelayoutSetMock = jest.fn(); + +const createOnRemoveProcessTaskHandler = ({ currentPolicy, layoutSets }: any) => { + return new OnProcessTaskRemoveHandler( + orgMock, + appMock, + currentPolicy || currentPolicyMock, + layoutSets || layoutSetsMock, + mutateApplicationPolicyMock, + deleteDataTypeFromAppMetadataMock, + deletelayoutSetMock, + ); +}; describe('OnProcessTaskRemoveHandler', () => { - it('should remove payment policy from current policy when task type is payment', () => { - const org = 'testOrg'; - const app = 'testApp'; + beforeEach(() => { + jest.resetAllMocks(); + }); + + it('should remove layoutSet when data-task is deleted', () => { + const layoutSets: LayoutSets = { + sets: [{ id: 'testLayoutSetId', dataType: 'data', tasks: ['testElementId'] }], + }; + const taskMetadata: OnProcessTaskEvent = { + taskType: 'data', + taskEvent: { + element: { + id: 'testElementId', + businessObject: { + id: 'testEventId', + $type: BpmnTypeEnum.Task, + extensionElements: undefined, + }, + }, + } as TaskEvent, + }; + + const onProcessTaskRemoveHandler = createOnRemoveProcessTaskHandler({ + layoutSets, + }); + + onProcessTaskRemoveHandler.handleOnProcessTaskRemove(taskMetadata); + expect(deletelayoutSetMock).toHaveBeenCalledWith({ layoutSetIdToUpdate: 'testLayoutSetId' }); + }); + + it('should remove payment policy from current policy when task type is payment', () => { const currentPolicy: Policy = { requiredAuthenticationLevelOrg: '3', requiredAuthenticationLevelEndUser: '3', @@ -43,7 +100,6 @@ describe('OnProcessTaskRemoveHandler', () => { ], }; - const mutateApplicationPolicy = jest.fn(); const taskMetadata: OnProcessTaskEvent = { taskType: 'payment', taskEvent: { @@ -54,14 +110,67 @@ describe('OnProcessTaskRemoveHandler', () => { } as TaskEvent, }; - const onProcessTaskRemoveHandler = new OnProcessTaskRemoveHandler( - org, - app, + const onProcessTaskRemoveHandler = createOnRemoveProcessTaskHandler({ currentPolicy, - mutateApplicationPolicy, - ); + }); onProcessTaskRemoveHandler.handleOnProcessTaskRemove(taskMetadata); - expect(mutateApplicationPolicy).toHaveBeenCalledWith(expectedResponse); + expect(mutateApplicationPolicyMock).toHaveBeenCalledWith(expectedResponse); + expect(deletelayoutSetMock).not.toHaveBeenCalled(); + }); + + it('should delete layoutSet for payment-task if layoutSet exists', () => { + const layoutSets: LayoutSets = { + sets: [{ id: 'testLayoutSetId', dataType: 'payment', tasks: ['testElementId'] }], + }; + + const taskMetadata: OnProcessTaskEvent = { + taskType: 'payment', + taskEvent: { + element: { + id: 'testElementId', + businessObject: { + id: 'testEventId', + $type: BpmnTypeEnum.Task, + extensionElements: undefined, + }, + }, + } as TaskEvent, + }; + + const onProcessTaskRemoveHandler = createOnRemoveProcessTaskHandler({ + layoutSets, + }); + + onProcessTaskRemoveHandler.handleOnProcessTaskRemove(taskMetadata); + expect(deletelayoutSetMock).toHaveBeenCalledWith({ layoutSetIdToUpdate: 'testLayoutSetId' }); + }); + + it('should remove datatype from app metadata and delete layoutSet when the signing Task is deleted', () => { + const layoutSets: LayoutSets = { + sets: [{ id: 'testLayoutSetId', dataType: 'signing', tasks: ['testElementId'] }], + }; + + const taskMetadata: OnProcessTaskEvent = { + taskType: 'signing', + taskEvent: { + element: { + id: 'testElementId', + businessObject: { + id: 'testEventId', + $type: BpmnTypeEnum.Task, + extensionElements: undefined, + }, + }, + } as TaskEvent, + }; + + const onProcessTaskRemoveHandler = createOnRemoveProcessTaskHandler({ + layoutSets, + }); + + onProcessTaskRemoveHandler.handleOnProcessTaskRemove(taskMetadata); + expect(deleteDataTypeFromAppMetadataMock).toHaveBeenCalled(); + expect(deletelayoutSetMock).toHaveBeenCalledWith({ layoutSetIdToUpdate: 'testLayoutSetId' }); }); }); diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts index 4c7a4f8f578..ab1740f0a7d 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts @@ -23,9 +23,34 @@ export class OnProcessTaskRemoveHandler { * @param taskMetadata */ public handleOnProcessTaskRemove(taskMetadata: OnProcessTaskEvent): void { + if (taskMetadata.taskType === 'data') { + this.handleDataTaskRemove(taskMetadata); + } + if (taskMetadata.taskType === 'payment') { this.handlePaymentTaskRemove(taskMetadata); } + + if (taskMetadata.taskType === 'signing') { + this.handleSigningTaskRemove(taskMetadata); + } + } + + /** + * Deletes the layoutSet from the deleted data task + * @private + */ + private handleDataTaskRemove(taskMetadata: OnProcessTaskEvent): void { + const layoutSetId = getLayoutSetIdFromTaskId( + taskMetadata.taskEvent.element.id, + this.layoutSets, + ); + + if (layoutSetId) { + this.deleteLayoutSet({ + layoutSetIdToUpdate: layoutSetId, + }); + } } /** @@ -58,11 +83,41 @@ export class OnProcessTaskRemoveHandler { this.mutateApplicationPolicy(updatedPolicy); - // Delete layout set + // Delete layoutSet + const layoutSetId = getLayoutSetIdFromTaskId( + taskMetadata.taskEvent.element.id, + this.layoutSets, + ); + + if (layoutSetId) { + this.deleteLayoutSet({ + layoutSetIdToUpdate: layoutSetId, + }); + } + } + + /** + * Deletes layoutSet and dataType from the deleted signing task + * @param taskMetadata + * @private + */ + private handleSigningTaskRemove(taskMetadata: OnProcessTaskEvent): void { + // Delete dataType + const dataTypeId = getDataTypeIdFromBusinessObject( + taskMetadata.taskType, + taskMetadata.taskEvent.element.businessObject, + ); + + this.deleteDataTypeFromAppMetadata({ + dataTypeId, + }); + + // Delete layoutSet const layoutSetId = getLayoutSetIdFromTaskId( taskMetadata.taskEvent.element.id, this.layoutSets, ); + if (layoutSetId) { this.deleteLayoutSet({ layoutSetIdToUpdate: layoutSetId, diff --git a/frontend/packages/process-editor/src/ProcessEditor.test.tsx b/frontend/packages/process-editor/src/ProcessEditor.test.tsx index bde52c8ebd6..ed0c68a6aab 100644 --- a/frontend/packages/process-editor/src/ProcessEditor.test.tsx +++ b/frontend/packages/process-editor/src/ProcessEditor.test.tsx @@ -22,8 +22,6 @@ const defaultProps: ProcessEditorProps = { deleteLayoutSet: jest.fn(), mutateLayoutSetId: jest.fn(), mutateDataType: jest.fn(), - addDataTypeToAppMetadata: jest.fn(), - deleteDataTypeFromAppMetadata: jest.fn(), openPolicyEditor: jest.fn(), onProcessTaskRemove: jest.fn(), onProcessTaskAdd: jest.fn(), diff --git a/frontend/packages/process-editor/src/ProcessEditor.tsx b/frontend/packages/process-editor/src/ProcessEditor.tsx index eb21774b3d0..d8dfadc6f18 100644 --- a/frontend/packages/process-editor/src/ProcessEditor.tsx +++ b/frontend/packages/process-editor/src/ProcessEditor.tsx @@ -24,8 +24,6 @@ export type ProcessEditorProps = { deleteLayoutSet: BpmnApiContextProps['deleteLayoutSet']; mutateLayoutSetId: BpmnApiContextProps['mutateLayoutSetId']; mutateDataType: BpmnApiContextProps['mutateDataType']; - addDataTypeToAppMetadata: BpmnApiContextProps['addDataTypeToAppMetadata']; - deleteDataTypeFromAppMetadata: BpmnApiContextProps['deleteDataTypeFromAppMetadata']; saveBpmn: (bpmnXml: string, metaData?: MetaDataForm) => void; openPolicyEditor: BpmnApiContextProps['openPolicyEditor']; onProcessTaskAdd: BpmnApiContextProps['onProcessTaskAdd']; @@ -43,8 +41,6 @@ export const ProcessEditor = ({ deleteLayoutSet, mutateLayoutSetId, mutateDataType, - addDataTypeToAppMetadata, - deleteDataTypeFromAppMetadata, saveBpmn, openPolicyEditor, onProcessTaskAdd, @@ -71,8 +67,6 @@ export const ProcessEditor = ({ deleteLayoutSet={deleteLayoutSet} mutateLayoutSetId={mutateLayoutSetId} mutateDataType={mutateDataType} - addDataTypeToAppMetadata={addDataTypeToAppMetadata} - deleteDataTypeFromAppMetadata={deleteDataTypeFromAppMetadata} saveBpmn={saveBpmn} openPolicyEditor={openPolicyEditor} onProcessTaskAdd={onProcessTaskAdd} diff --git a/frontend/packages/process-editor/src/contexts/BpmnApiContext.tsx b/frontend/packages/process-editor/src/contexts/BpmnApiContext.tsx index 2bd670a1c4f..e24c4eb5398 100644 --- a/frontend/packages/process-editor/src/contexts/BpmnApiContext.tsx +++ b/frontend/packages/process-editor/src/contexts/BpmnApiContext.tsx @@ -20,9 +20,6 @@ export type BpmnApiContextProps = { deleteLayoutSet: (data: { layoutSetIdToUpdate: string }) => void; mutateLayoutSetId: (data: { layoutSetIdToUpdate: string; newLayoutSetId: string }) => void; mutateDataType: (dataTypeChange: DataTypeChange, options?: QueryOptions) => void; - addDataTypeToAppMetadata: (data: { dataTypeId: string }) => void; - deleteDataTypeFromAppMetadata: (data: { dataTypeId: string }) => void; - saveBpmn: (bpmnXml: string, metaData?: MetaDataForm) => void; openPolicyEditor: () => void; onProcessTaskAdd: (taskMetadata: OnProcessTaskEvent) => void; @@ -45,8 +42,6 @@ export const BpmnApiContextProvider = ({ deleteLayoutSet, mutateLayoutSetId, mutateDataType, - addDataTypeToAppMetadata, - deleteDataTypeFromAppMetadata, saveBpmn, openPolicyEditor, onProcessTaskRemove, @@ -63,8 +58,6 @@ export const BpmnApiContextProvider = ({ deleteLayoutSet, mutateLayoutSetId, mutateDataType, - addDataTypeToAppMetadata, - deleteDataTypeFromAppMetadata, saveBpmn, openPolicyEditor, onProcessTaskRemove, diff --git a/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx b/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx index 4c252966418..523235a5b68 100644 --- a/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx +++ b/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx @@ -10,7 +10,7 @@ import type { BpmnTaskType } from '../types/BpmnTaskType'; import type { LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; import { getMockBpmnElementForTask, mockBpmnDetails } from '../../test/mocks/bpmnDetailsMock'; import { mockModelerRef } from '../../test/mocks/bpmnModelerMock'; -import { AddProcessTaskManager, RemoveProcessTaskManager } from '../utils/ProcessTaskManager'; +import { RemoveProcessTaskManager } from '../utils/ProcessTaskManager'; const layoutSetId = 'someLayoutSetId'; const layoutSetsMock: LayoutSets = { @@ -77,6 +77,8 @@ jest.mock('./useBpmnModeler', () => ({ })); const setBpmnDetailsMock = jest.fn(); +const onProcessTaskAddMock = jest.fn(); +const onProcessTaskRemoveMock = jest.fn(); const overrideUseBpmnContext = () => { (useBpmnContext as jest.Mock).mockReturnValue({ getUpdatedXml: jest.fn(), @@ -103,9 +105,9 @@ const wrapper = ({ children }) => ( {children} @@ -130,12 +132,7 @@ describe('useBpmnEditor', () => { it('should handle "shape.add" event', async () => { renderUseBpmnEditor(false, 'shape.add'); - const handleTaskAddMock = jest.fn(); - (AddProcessTaskManager as jest.Mock).mockImplementation(() => ({ - handleTaskAdd: () => handleTaskAddMock(), - })); - - await waitFor(() => expect(handleTaskAddMock).toHaveBeenCalledTimes(1)); + await waitFor(() => expect(onProcessTaskAddMock).toHaveBeenCalledTimes(1)); }); it('should handle "shape.remove" event', async () => { diff --git a/frontend/packages/process-editor/src/hooks/useBpmnEditor.ts b/frontend/packages/process-editor/src/hooks/useBpmnEditor.ts index 6888585fcda..e6390e72bda 100644 --- a/frontend/packages/process-editor/src/hooks/useBpmnEditor.ts +++ b/frontend/packages/process-editor/src/hooks/useBpmnEditor.ts @@ -5,7 +5,7 @@ import { useBpmnModeler } from './useBpmnModeler'; import { getBpmnEditorDetailsFromBusinessObject } from '../utils/hookUtils'; import { useBpmnConfigPanelFormContext } from '../contexts/BpmnConfigPanelContext'; import { useBpmnApiContext } from '../contexts/BpmnApiContext'; -import { RemoveProcessTaskManager, type TaskEvent } from '../utils/ProcessTaskManager'; +import type { TaskEvent } from '../types/TaskEvent'; // Wrapper around bpmn-js to Reactify it @@ -20,20 +20,7 @@ export const useBpmnEditor = (): UseBpmnViewerResult => { const { metaDataFormRef, resetForm } = useBpmnConfigPanelFormContext(); const { getModeler, destroyModeler } = useBpmnModeler(); - const { - deleteLayoutSet, - deleteDataTypeFromAppMetadata, - saveBpmn, - layoutSets, - onProcessTaskAdd, - onProcessTaskRemove, - } = useBpmnApiContext(); - - // Needs to update the layoutSetsRef.current when layoutSets changes to avoid staled data in the event listeners - // const layoutSetsRef = useRef(layoutSets); - // useEffect(() => { - // layoutSetsRef.current = layoutSets; - // }, [layoutSets]); + const { saveBpmn, onProcessTaskAdd, onProcessTaskRemove } = useBpmnApiContext(); const handleCommandStackChanged = async () => { saveBpmn(await getUpdatedXml(), metaDataFormRef.current || null); @@ -51,15 +38,6 @@ export const useBpmnEditor = (): UseBpmnViewerResult => { const handleShapeRemove = (taskEvent: TaskEvent): void => { const bpmnDetails = getBpmnEditorDetailsFromBusinessObject(taskEvent?.element?.businessObject); - // const removeProcessTaskManager = new RemoveProcessTaskManager( - // layoutSetsRef.current, - // deleteLayoutSet, - // deleteDataTypeFromAppMetadata, - // bpmnDetails, - // onProcessTaskRemove, - // ); - // - // removeProcessTaskManager.handleTaskRemove(taskEvent); onProcessTaskRemove({ taskEvent, taskType: bpmnDetails.taskType, diff --git a/frontend/packages/process-editor/src/utils/ProcessTaskManager/types.ts b/frontend/packages/process-editor/src/types/TaskEvent.ts similarity index 57% rename from frontend/packages/process-editor/src/utils/ProcessTaskManager/types.ts rename to frontend/packages/process-editor/src/types/TaskEvent.ts index af39dc64186..8304756068b 100644 --- a/frontend/packages/process-editor/src/utils/ProcessTaskManager/types.ts +++ b/frontend/packages/process-editor/src/types/TaskEvent.ts @@ -1,4 +1,4 @@ -import { type BpmnBusinessObjectEditor } from '../../types/BpmnBusinessObjectEditor'; +import { type BpmnBusinessObjectEditor } from './BpmnBusinessObjectEditor'; export type TaskEvent = Event & { element: { diff --git a/frontend/packages/process-editor/src/utils/ProcessTaskManager/RemoveProcessTaskManager.test.ts b/frontend/packages/process-editor/src/utils/ProcessTaskManager/RemoveProcessTaskManager.test.ts deleted file mode 100644 index 58ae7f90cb9..00000000000 --- a/frontend/packages/process-editor/src/utils/ProcessTaskManager/RemoveProcessTaskManager.test.ts +++ /dev/null @@ -1,118 +0,0 @@ -import { RemoveProcessTaskManager } from './RemoveProcessTaskManager'; -import { type LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; -import { type BpmnTaskType } from '../../types/BpmnTaskType'; -import { type BpmnDetails } from '../../types/BpmnDetails'; -import { BpmnTypeEnum } from '../../enum/BpmnTypeEnum'; -import { type TaskEvent } from '../ProcessTaskManager/types'; - -describe('RemoveProcessTaskManager', () => { - beforeEach(() => { - jest.resetAllMocks(); - }); - const createBpmnDetails = (id: string, name: string, taskType: BpmnTaskType): BpmnDetails => ({ - id, - name, - taskType, - type: BpmnTypeEnum.Task, - }); - - const createTaskEvent = (taskType: string, extensionConfig?: object): TaskEvent => - ({ - element: { - businessObject: { - id: 'testEventId', - $type: taskType, - extensionElements: extensionConfig ? { values: [extensionConfig] } : undefined, - }, - }, - }) as TaskEvent; - - it('should remove layoutSet when data-task is deleted', () => { - const layoutSets: LayoutSets = { - sets: [{ id: 'testLayoutSetId', dataType: 'data', tasks: ['testTask'] }], - }; - - const deleteLayoutSet = jest.fn(); - const bpmnDetails = createBpmnDetails('testTask', 'testTask', 'data'); - - const removeProcessTaskManager = new RemoveProcessTaskManager( - layoutSets, - deleteLayoutSet, - jest.fn(), - bpmnDetails, - jest.fn(), - ); - - removeProcessTaskManager.handleTaskRemove({} as TaskEvent); - expect(deleteLayoutSet).toHaveBeenCalledWith({ layoutSetIdToUpdate: 'testLayoutSetId' }); - }); - - it('should remove datatype from app metadata and delete layoutSet when the signing Task is deleted', () => { - const layoutSets: LayoutSets = { - sets: [{ id: 'testLayoutSetId', dataType: 'signing', tasks: ['testTask'] }], - }; - - const deleteLayoutSet = jest.fn(); - const deleteDataTypeFromAppMetadata = jest.fn(); - const bpmnDetails = createBpmnDetails('testTask', 'testTask', 'signing'); - - const removeProcessTaskManager = new RemoveProcessTaskManager( - layoutSets, - deleteLayoutSet, - deleteDataTypeFromAppMetadata, - bpmnDetails, - jest.fn(), - ); - - removeProcessTaskManager.handleTaskRemove(createTaskEvent('signing')); - expect(deleteDataTypeFromAppMetadata).toHaveBeenCalled(); - expect(deleteLayoutSet).toHaveBeenCalledWith({ layoutSetIdToUpdate: 'testLayoutSetId' }); - }); - - it('should remove datatype and layoutSet when the payment Task is deleted', () => { - const layoutSets: LayoutSets = { - sets: [{ id: 'testLayoutSetId', dataType: 'payment', tasks: ['testTask'] }], - }; - - const deleteLayoutSet = jest.fn(); - const deleteDataTypeFromAppMetadata = jest.fn(); - const bpmnDetails = createBpmnDetails('testTask', 'testTask', 'payment'); - - const removeProcessTaskManager = new RemoveProcessTaskManager( - layoutSets, - deleteLayoutSet, - deleteDataTypeFromAppMetadata, - bpmnDetails, - jest.fn(), - ); - - removeProcessTaskManager.handleTaskRemove(createTaskEvent('payment')); - expect(deleteDataTypeFromAppMetadata).toHaveBeenCalled(); - expect(deleteLayoutSet).toHaveBeenCalledWith({ layoutSetIdToUpdate: 'testLayoutSetId' }); - }); - - it('should inform the consumer of the package that a task has been removed with the taskEvent and taskType', () => { - const layoutSets: LayoutSets = { - sets: [{ id: 'testLayoutSetId', dataType: 'payment', tasks: ['testTask'] }], - }; - - const deleteLayoutSet = jest.fn(); - const deleteDataTypeFromAppMetadata = jest.fn(); - const onProcessTaskRemove = jest.fn(); - const bpmnDetails = createBpmnDetails('testTask', 'testTask', 'payment'); - - const removeProcessTaskManager = new RemoveProcessTaskManager( - layoutSets, - deleteLayoutSet, - deleteDataTypeFromAppMetadata, - bpmnDetails, - onProcessTaskRemove, - ); - - removeProcessTaskManager.handleTaskRemove(createTaskEvent('payment')); - expect(onProcessTaskRemove).toHaveBeenCalledWith({ - taskEvent: createTaskEvent('payment'), - taskType: 'payment', - }); - }); -}); diff --git a/frontend/packages/process-editor/src/utils/ProcessTaskManager/RemoveProcessTaskManager.ts b/frontend/packages/process-editor/src/utils/ProcessTaskManager/RemoveProcessTaskManager.ts deleted file mode 100644 index 3df18321ae5..00000000000 --- a/frontend/packages/process-editor/src/utils/ProcessTaskManager/RemoveProcessTaskManager.ts +++ /dev/null @@ -1,104 +0,0 @@ -import { type BpmnApiContextProps } from '../../contexts/BpmnApiContext'; -import { type BpmnDetails } from '../../types/BpmnDetails'; -import { type TaskEvent } from './types'; -import { - getDataTypeIdFromBusinessObject, - getLayoutSetIdFromTaskId, -} from '../../utils/hookUtils/hookUtils'; - -export class RemoveProcessTaskManager { - constructor( - private readonly layoutSets: BpmnApiContextProps['layoutSets'], - private readonly deleteLayoutSet: BpmnApiContextProps['deleteLayoutSet'], - private readonly deleteDataTypeFromAppMetadata: BpmnApiContextProps['deleteDataTypeFromAppMetadata'], - private readonly bpmnDetails: BpmnDetails, - private readonly onProcessTaskRemove: BpmnApiContextProps['onProcessTaskRemove'], - ) {} - - /** - * Handles the task remove event and delegates the handling to the specific task type - * @param taskEvent - */ - public handleTaskRemove(taskEvent: TaskEvent): void { - if (this.bpmnDetails.taskType === 'data') { - this.handleDataTaskRemove(); - } - - if (this.bpmnDetails.taskType === 'payment') { - this.handlePaymentTaskRemove(taskEvent); - } - - if (this.bpmnDetails.taskType === 'signing') { - this.handleSigningTaskRemove(taskEvent); - } - - // Informs the consumer of this package that a task with the provided taskEvent and taskType has been removed - this.onProcessTaskRemove({ - taskEvent, - taskType: this.bpmnDetails.taskType, - }); - } - - /** - * Deletes the layout set from the deleted data task - * @private - */ - private handleDataTaskRemove(): void { - const layoutSetId = getLayoutSetIdFromTaskId(this.bpmnDetails, this.layoutSets); - - if (layoutSetId) { - this.deleteLayoutSet({ - layoutSetIdToUpdate: layoutSetId, - }); - } - } - - /** - * Deletes the dataType and layout set from the deleted payment task - * @param taskEvent - * @private - */ - private handlePaymentTaskRemove(taskEvent: TaskEvent): void { - // Delete dataType - const dataTypeId = getDataTypeIdFromBusinessObject( - this.bpmnDetails.taskType, - taskEvent.element.businessObject, - ); - - this.deleteDataTypeFromAppMetadata({ - dataTypeId, - }); - - // Delete layout set - const layoutSetId = getLayoutSetIdFromTaskId(this.bpmnDetails, this.layoutSets); - if (layoutSetId) { - this.deleteLayoutSet({ - layoutSetIdToUpdate: layoutSetId, - }); - } - } - - /** - * Deletes the dataType from the deleted signing task - * @param taskEvent - * @private - */ - private handleSigningTaskRemove(taskEvent: TaskEvent): void { - const dataTypeId = getDataTypeIdFromBusinessObject( - this.bpmnDetails.taskType, - taskEvent.element.businessObject, - ); - - this.deleteDataTypeFromAppMetadata({ - dataTypeId, - }); - - // Delete layout set - const layoutSetId = getLayoutSetIdFromTaskId(this.bpmnDetails, this.layoutSets); - if (layoutSetId) { - this.deleteLayoutSet({ - layoutSetIdToUpdate: layoutSetId, - }); - } - } -} diff --git a/frontend/packages/process-editor/src/utils/ProcessTaskManager/index.ts b/frontend/packages/process-editor/src/utils/ProcessTaskManager/index.ts deleted file mode 100644 index 2a40534b777..00000000000 --- a/frontend/packages/process-editor/src/utils/ProcessTaskManager/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { RemoveProcessTaskManager } from './RemoveProcessTaskManager'; -export type { TaskEvent } from './types'; diff --git a/frontend/packages/process-editor/test/mocks/bpmnContextMock.ts b/frontend/packages/process-editor/test/mocks/bpmnContextMock.ts index 6e8f8ba42ed..7010c2a4441 100644 --- a/frontend/packages/process-editor/test/mocks/bpmnContextMock.ts +++ b/frontend/packages/process-editor/test/mocks/bpmnContextMock.ts @@ -41,8 +41,6 @@ export const mockBpmnApiContextValue: BpmnApiContextProps = { deleteLayoutSet: jest.fn(), mutateLayoutSetId: jest.fn(), mutateDataType: jest.fn(), - addDataTypeToAppMetadata: jest.fn(), - deleteDataTypeFromAppMetadata: jest.fn(), saveBpmn: jest.fn(), openPolicyEditor: jest.fn(), onProcessTaskRemove: jest.fn(), From ebc7495e41c668af9f3f4ec4bfc6d10af2e2e695 Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Fri, 31 May 2024 14:12:12 +0200 Subject: [PATCH 03/15] fixed typescript issues --- .../features/processEditor/ProcessEditor.tsx | 5 +- .../handlers/OnProcessTaskAddHandler.test.ts | 2 +- .../OnProcessTaskRemoveHandler.test.ts | 2 +- .../src/hooks/useBpmnEditor.test.tsx | 49 +++++++++---------- .../process-editor/src/types/OnProcessTask.ts | 4 +- .../src/utils/hookUtils/hookUtils.test.ts | 6 +-- 6 files changed, 33 insertions(+), 35 deletions(-) diff --git a/frontend/app-development/features/processEditor/ProcessEditor.tsx b/frontend/app-development/features/processEditor/ProcessEditor.tsx index c489a947844..4d1a78d9624 100644 --- a/frontend/app-development/features/processEditor/ProcessEditor.tsx +++ b/frontend/app-development/features/processEditor/ProcessEditor.tsx @@ -128,7 +128,10 @@ export const ProcessEditor = (): React.ReactElement => { org, app, currentPolicy, + layoutSets, mutateApplicationPolicy, + deleteDataTypeFromAppMetadata, + deleteLayoutSet, ); onProcessTaskRemoveHandler.handleOnProcessTaskRemove(taskMetadata); }; @@ -150,8 +153,6 @@ export const ProcessEditor = (): React.ReactElement => { appLibVersion={appLibData.backendVersion} bpmnXml={hasBpmnQueryError ? null : bpmnXml} mutateDataType={mutateDataType} - addDataTypeToAppMetadata={addDataTypeToAppMetadata} - deleteDataTypeFromAppMetadata={deleteDataTypeFromAppMetadata} saveBpmn={saveBpmnXml} openPolicyEditor={() => { setSettingsModalSelectedTab('policy'); diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts index 13a40741906..6275aa88a4b 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts @@ -1,8 +1,8 @@ import type { Policy } from 'app-shared/types/Policy'; import type { OnProcessTaskEvent } from '@altinn/process-editor/types/OnProcessTask'; -import { TaskEvent } from '@altinn/process-editor/utils/ProcessTaskManager'; import { OnProcessTaskAddHandler } from './OnProcessTaskAddHandler'; import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; +import { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; const orgMock = 'testOrg'; const appMock = 'testApp'; diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts index 1478fc993d2..c9bfd8d8401 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts @@ -1,9 +1,9 @@ import type { Policy } from 'app-shared/types/Policy'; import type { OnProcessTaskEvent } from '@altinn/process-editor/types/OnProcessTask'; -import { TaskEvent } from '@altinn/process-editor/utils/ProcessTaskManager'; import { OnProcessTaskRemoveHandler } from './OnProcessTaskRemoveHandler'; import type { LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; +import { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; const orgMock = 'testOrg'; const appMock = 'testApp'; diff --git a/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx b/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx index 523235a5b68..7f371251920 100644 --- a/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx +++ b/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx @@ -10,7 +10,6 @@ import type { BpmnTaskType } from '../types/BpmnTaskType'; import type { LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; import { getMockBpmnElementForTask, mockBpmnDetails } from '../../test/mocks/bpmnDetailsMock'; import { mockModelerRef } from '../../test/mocks/bpmnModelerMock'; -import { RemoveProcessTaskManager } from '../utils/ProcessTaskManager'; const layoutSetId = 'someLayoutSetId'; const layoutSetsMock: LayoutSets = { @@ -50,8 +49,6 @@ class BpmnModelerMockImpl { } } -jest.mock('../utils/ProcessTaskManager'); - jest.mock('../utils/hookUtils', () => ({ getBpmnEditorDetailsFromBusinessObject: jest.fn().mockReturnValue({}), })); @@ -135,29 +132,29 @@ describe('useBpmnEditor', () => { await waitFor(() => expect(onProcessTaskAddMock).toHaveBeenCalledTimes(1)); }); - it('should handle "shape.remove" event', async () => { - renderUseBpmnEditor(true, 'shape.remove'); - - const handleTaskRemoveMock = jest.fn(); - (RemoveProcessTaskManager as jest.Mock).mockImplementation(() => ({ - handleTaskRemove: () => handleTaskRemoveMock(), - })); - - await waitFor(() => expect(handleTaskRemoveMock).toHaveBeenCalledTimes(1)); - await waitFor(() => expect(setBpmnDetailsMock).toHaveBeenCalledWith(null)); - }); - - it('should handle "shape.add" event', async () => { - renderUseBpmnEditor(true, 'shape.remove'); - - const handleTaskRemoveMock = jest.fn(); - (RemoveProcessTaskManager as jest.Mock).mockImplementation(() => ({ - handleAdd: () => handleTaskRemoveMock(), - })); - - await waitFor(() => expect(handleTaskRemoveMock).toHaveBeenCalledTimes(1)); - await waitFor(() => expect(setBpmnDetailsMock).toHaveBeenCalledWith(null)); - }); + // it('should handle "shape.remove" event', async () => { + // renderUseBpmnEditor(true, 'shape.remove'); + // + // const handleTaskRemoveMock = jest.fn(); + // (RemoveProcessTaskManager as jest.Mock).mockImplementation(() => ({ + // handleTaskRemove: () => handleTaskRemoveMock(), + // })); + // + // await waitFor(() => expect(handleTaskRemoveMock).toHaveBeenCalledTimes(1)); + // await waitFor(() => expect(setBpmnDetailsMock).toHaveBeenCalledWith(null)); + // }); + // + // it('should handle "shape.add" event', async () => { + // renderUseBpmnEditor(true, 'shape.remove'); + // + // const handleTaskRemoveMock = jest.fn(); + // (RemoveProcessTaskManager as jest.Mock).mockImplementation(() => ({ + // handleAdd: () => handleTaskRemoveMock(), + // })); + // + // await waitFor(() => expect(handleTaskRemoveMock).toHaveBeenCalledTimes(1)); + // await waitFor(() => expect(setBpmnDetailsMock).toHaveBeenCalledWith(null)); + // }); it('should call setBpmnDetails when "element.click" event is triggered on eventBus', () => { const currentEventName = 'element.click'; diff --git a/frontend/packages/process-editor/src/types/OnProcessTask.ts b/frontend/packages/process-editor/src/types/OnProcessTask.ts index a19ee110b33..0ff993eac6a 100644 --- a/frontend/packages/process-editor/src/types/OnProcessTask.ts +++ b/frontend/packages/process-editor/src/types/OnProcessTask.ts @@ -1,5 +1,5 @@ -import type { TaskEvent } from '@altinn/process-editor/utils/ProcessTaskManager'; -import { BpmnTaskType } from '@altinn/process-editor/types/BpmnTaskType'; +import { BpmnTaskType } from '../types/BpmnTaskType'; +import { TaskEvent } from '../types/TaskEvent'; export type OnProcessTaskEvent = { taskEvent?: TaskEvent; diff --git a/frontend/packages/process-editor/src/utils/hookUtils/hookUtils.test.ts b/frontend/packages/process-editor/src/utils/hookUtils/hookUtils.test.ts index 4a27b873823..a46a6873fa3 100644 --- a/frontend/packages/process-editor/src/utils/hookUtils/hookUtils.test.ts +++ b/frontend/packages/process-editor/src/utils/hookUtils/hookUtils.test.ts @@ -121,18 +121,18 @@ describe('hookUtils', () => { describe('getLayoutSetIdFromTaskId', () => { it('should return the layout set id corresponding to the task id', () => { - const result = getLayoutSetIdFromTaskId({ ...mockBpmnDetails, id: 'task1' }, layoutSets); + const result = getLayoutSetIdFromTaskId('task1', layoutSets); expect(result).toBe('layoutSet1'); }); it('should return undefined if task id does not exist in any layout set', () => { - const result = getLayoutSetIdFromTaskId(mockBpmnDetails, layoutSets); + const result = getLayoutSetIdFromTaskId(mockBpmnDetails.id, layoutSets); expect(result).toBeUndefined(); }); it('should return undefined if layout sets are empty', () => { const layoutSets = { sets: [] }; - const result = getLayoutSetIdFromTaskId(mockBpmnDetails, layoutSets); + const result = getLayoutSetIdFromTaskId(mockBpmnDetails.id, layoutSets); expect(result).toBeUndefined(); }); }); From 67e609091438578c69e7d44a3dd0d04125f6cf29 Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Sun, 2 Jun 2024 21:09:24 +0200 Subject: [PATCH 04/15] unit-tests for useBpmnEditor --- .../src/hooks/useBpmnEditor.test.tsx | 29 ++++--------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx b/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx index 7f371251920..89935df6da8 100644 --- a/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx +++ b/frontend/packages/process-editor/src/hooks/useBpmnEditor.test.tsx @@ -76,6 +76,7 @@ jest.mock('./useBpmnModeler', () => ({ const setBpmnDetailsMock = jest.fn(); const onProcessTaskAddMock = jest.fn(); const onProcessTaskRemoveMock = jest.fn(); + const overrideUseBpmnContext = () => { (useBpmnContext as jest.Mock).mockReturnValue({ getUpdatedXml: jest.fn(), @@ -132,29 +133,11 @@ describe('useBpmnEditor', () => { await waitFor(() => expect(onProcessTaskAddMock).toHaveBeenCalledTimes(1)); }); - // it('should handle "shape.remove" event', async () => { - // renderUseBpmnEditor(true, 'shape.remove'); - // - // const handleTaskRemoveMock = jest.fn(); - // (RemoveProcessTaskManager as jest.Mock).mockImplementation(() => ({ - // handleTaskRemove: () => handleTaskRemoveMock(), - // })); - // - // await waitFor(() => expect(handleTaskRemoveMock).toHaveBeenCalledTimes(1)); - // await waitFor(() => expect(setBpmnDetailsMock).toHaveBeenCalledWith(null)); - // }); - // - // it('should handle "shape.add" event', async () => { - // renderUseBpmnEditor(true, 'shape.remove'); - // - // const handleTaskRemoveMock = jest.fn(); - // (RemoveProcessTaskManager as jest.Mock).mockImplementation(() => ({ - // handleAdd: () => handleTaskRemoveMock(), - // })); - // - // await waitFor(() => expect(handleTaskRemoveMock).toHaveBeenCalledTimes(1)); - // await waitFor(() => expect(setBpmnDetailsMock).toHaveBeenCalledWith(null)); - // }); + it('should handle "shape.remove" event', async () => { + renderUseBpmnEditor(false, 'shape.remove'); + + await waitFor(() => expect(onProcessTaskRemoveMock).toHaveBeenCalledTimes(1)); + }); it('should call setBpmnDetails when "element.click" event is triggered on eventBus', () => { const currentEventName = 'element.click'; From d2f48a175c683d2e5d38953b90d2a6dbdef4edd1 Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Sun, 2 Jun 2024 21:22:37 +0200 Subject: [PATCH 05/15] testing and eslint cleanups --- .../handlers/OnProcessTaskAddHandler.test.ts | 23 ++++++++++++++++++- .../OnProcessTaskRemoveHandler.test.ts | 7 ++++-- .../handlers/OnProcessTaskRemoveHandler.ts | 2 +- .../process-editor/src/types/OnProcessTask.ts | 4 ++-- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts index 6275aa88a4b..b3236fd9b59 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts @@ -2,7 +2,8 @@ import type { Policy } from 'app-shared/types/Policy'; import type { OnProcessTaskEvent } from '@altinn/process-editor/types/OnProcessTask'; import { OnProcessTaskAddHandler } from './OnProcessTaskAddHandler'; import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; -import { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; +import type { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; +import type { BpmnTaskType } from '@altinn/process-editor/types/BpmnTaskType'; const orgMock = 'testOrg'; const appMock = 'testApp'; @@ -54,6 +55,8 @@ describe('OnProcessTaskAddHandler', () => { layoutSetConfig: { id: 'testId', tasks: ['testId'] }, layoutSetIdToUpdate: 'testId', }); + expect(addDataTypeToAppMetadataMock).not.toHaveBeenCalled(); + expect(mutateApplicationPolicyMock).not.toHaveBeenCalled(); }); it('should add layoutSet, dataType and default policy when payment task is added', () => { @@ -136,5 +139,23 @@ describe('OnProcessTaskAddHandler', () => { expect(addDataTypeToAppMetadataMock).toHaveBeenCalledWith({ dataTypeId: 'signingInformation', }); + expect(addLayoutSetMock).not.toHaveBeenCalled(); + expect(mutateApplicationPolicyMock).not.toHaveBeenCalled(); }); + + it.each(['confirmation', 'feedback'])( + 'should not add layoutSet, dataType or default policy when task type is %s', + (task) => { + const onProcessTaskAddHandler = createOnProcessTaskHandler(); + + onProcessTaskAddHandler.handleOnProcessTaskAdd({ + taskEvent: createTaskEvent(), + taskType: task as BpmnTaskType, + }); + + expect(addLayoutSetMock).not.toHaveBeenCalled(); + expect(addDataTypeToAppMetadataMock).not.toHaveBeenCalled(); + expect(mutateApplicationPolicyMock).not.toHaveBeenCalled(); + }, + ); }); diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts index c9bfd8d8401..461470717aa 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts @@ -3,7 +3,7 @@ import type { OnProcessTaskEvent } from '@altinn/process-editor/types/OnProcessT import { OnProcessTaskRemoveHandler } from './OnProcessTaskRemoveHandler'; import type { LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; -import { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; +import type { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; const orgMock = 'testOrg'; const appMock = 'testApp'; @@ -62,6 +62,8 @@ describe('OnProcessTaskRemoveHandler', () => { onProcessTaskRemoveHandler.handleOnProcessTaskRemove(taskMetadata); expect(deletelayoutSetMock).toHaveBeenCalledWith({ layoutSetIdToUpdate: 'testLayoutSetId' }); + expect(mutateApplicationPolicyMock).not.toHaveBeenCalled(); + expect(deleteDataTypeFromAppMetadataMock).not.toHaveBeenCalled(); }); it('should remove payment policy from current policy when task type is payment', () => { @@ -146,7 +148,7 @@ describe('OnProcessTaskRemoveHandler', () => { expect(deletelayoutSetMock).toHaveBeenCalledWith({ layoutSetIdToUpdate: 'testLayoutSetId' }); }); - it('should remove datatype from app metadata and delete layoutSet when the signing Task is deleted', () => { + it('should remove datatype from app metadata and delete layoutSet when the signing task is deleted', () => { const layoutSets: LayoutSets = { sets: [{ id: 'testLayoutSetId', dataType: 'signing', tasks: ['testElementId'] }], }; @@ -172,5 +174,6 @@ describe('OnProcessTaskRemoveHandler', () => { onProcessTaskRemoveHandler.handleOnProcessTaskRemove(taskMetadata); expect(deleteDataTypeFromAppMetadataMock).toHaveBeenCalled(); expect(deletelayoutSetMock).toHaveBeenCalledWith({ layoutSetIdToUpdate: 'testLayoutSetId' }); + expect(mutateApplicationPolicyMock).not.toHaveBeenCalled(); }); }); diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts index ab1740f0a7d..326286b572d 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts @@ -5,7 +5,7 @@ import { getDataTypeIdFromBusinessObject, getLayoutSetIdFromTaskId, } from '@altinn/process-editor/utils/hookUtils/hookUtils'; -import { LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; +import type { LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; export class OnProcessTaskRemoveHandler { constructor( diff --git a/frontend/packages/process-editor/src/types/OnProcessTask.ts b/frontend/packages/process-editor/src/types/OnProcessTask.ts index 0ff993eac6a..8ad0591956c 100644 --- a/frontend/packages/process-editor/src/types/OnProcessTask.ts +++ b/frontend/packages/process-editor/src/types/OnProcessTask.ts @@ -1,5 +1,5 @@ -import { BpmnTaskType } from '../types/BpmnTaskType'; -import { TaskEvent } from '../types/TaskEvent'; +import type { BpmnTaskType } from '../types/BpmnTaskType'; +import type { TaskEvent } from '../types/TaskEvent'; export type OnProcessTaskEvent = { taskEvent?: TaskEvent; From 8daebe2517d6e13df09f935d2b6079d7939f856c Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Sun, 2 Jun 2024 21:24:45 +0200 Subject: [PATCH 06/15] removed TODO comment, since its fixed --- .../packages/process-editor/src/utils/hookUtils/hookUtils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/frontend/packages/process-editor/src/utils/hookUtils/hookUtils.ts b/frontend/packages/process-editor/src/utils/hookUtils/hookUtils.ts index e3e913ec2ff..a36f5e2fbab 100644 --- a/frontend/packages/process-editor/src/utils/hookUtils/hookUtils.ts +++ b/frontend/packages/process-editor/src/utils/hookUtils/hookUtils.ts @@ -65,7 +65,6 @@ export const getDataTypeIdFromBusinessObject = ( return businessObject?.extensionElements?.values[0][configNode][dataTypeName]; }; -// TODO remember to update every places where getLayoutSetIdFromTaskId is used to match elementId argument export const getLayoutSetIdFromTaskId = (elementId: string, layoutSets: LayoutSets) => { const layoutSet = layoutSets.sets.find((set) => set.tasks[0] === elementId); return layoutSet?.id; From 8016718fbb2e0f19f4446b904ee88c85616e3558 Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Sun, 2 Jun 2024 21:29:17 +0200 Subject: [PATCH 07/15] eslint --- .../features/processEditor/handlers/OnProcessTaskAddHandler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts index 0edba3a29af..605b3ae8b07 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts @@ -3,7 +3,7 @@ import type { OnProcessTaskEvent } from '@altinn/process-editor/types/OnProcessT import type { Policy } from 'app-shared/types/Policy'; import type { LayoutSetConfig } from 'app-shared/types/api/LayoutSetsResponse'; import { getDataTypeIdFromBusinessObject } from '@altinn/process-editor/utils/hookUtils/hookUtils'; -import { AddLayoutSetMutation } from '../../../hooks/mutations/useAddLayoutSetMutation'; +import type { AddLayoutSetMutation } from '../../../hooks/mutations/useAddLayoutSetMutation'; export class OnProcessTaskAddHandler { constructor( From ec2228d41628e0537c699bb5b0dbdb2898aa475a Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Mon, 3 Jun 2024 15:16:01 +0200 Subject: [PATCH 08/15] deleted class that re-appeared after merge from main --- .../AddProcessTaskManager.test.ts | 109 ------------------ .../AddProcessTaskManager.ts | 99 ---------------- 2 files changed, 208 deletions(-) delete mode 100644 frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.test.ts delete mode 100644 frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.ts diff --git a/frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.test.ts b/frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.test.ts deleted file mode 100644 index 33abd80c6cb..00000000000 --- a/frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.test.ts +++ /dev/null @@ -1,109 +0,0 @@ -import { AddProcessTaskManager } from './AddProcessTaskManager'; -import { BpmnTypeEnum } from '../../enum/BpmnTypeEnum'; -import { type TaskEvent } from '../ProcessTaskManager/types'; -import { type BpmnDetails } from '../../types/BpmnDetails'; -import { type BpmnTaskType } from '../../types/BpmnTaskType'; - -describe('AddProcessTaskManager', () => { - const createBpmnDetails = (id: string, name: string, taskType: BpmnTaskType): BpmnDetails => ({ - id, - name, - taskType, - type: BpmnTypeEnum.Task, - }); - - const createTaskEvent = (taskType: string, extensionConfig?: object): TaskEvent => - ({ - element: { - businessObject: { - id: 'testEventId', - $type: BpmnTypeEnum.Task, - extensionElements: extensionConfig ? { values: [extensionConfig] } : undefined, - }, - }, - }) as TaskEvent; - - const addLayoutSet = jest.fn(); - const addDataTypeToAppMetadata = jest.fn(); - const onProcessTaskAdd = jest.fn(); - - beforeEach(() => { - jest.resetAllMocks(); - }); - - const createAddProcessTaskManager = (bpmnDetails: BpmnDetails) => - new AddProcessTaskManager( - addLayoutSet, - addDataTypeToAppMetadata, - bpmnDetails, - onProcessTaskAdd, - ); - - it('should add layoutSet when data-task is added', () => { - const bpmnDetails = createBpmnDetails('testId', 'dataTask', 'data'); - const addProcessTaskManager = createAddProcessTaskManager(bpmnDetails); - - addProcessTaskManager.handleTaskAdd(createTaskEvent('data')); - - expect(addLayoutSet).toHaveBeenCalledWith({ - layoutSetConfig: { id: 'testId', tasks: ['testId'] }, - layoutSetIdToUpdate: 'testId', - }); - }); - - it('should add layoutSet and dataType when PaymentTask is added', () => { - const bpmnDetails = createBpmnDetails('testId', 'paymentTask', 'payment'); - const addProcessTaskManager = createAddProcessTaskManager(bpmnDetails); - - addProcessTaskManager.handleTaskAdd( - createTaskEvent('payment', { - taskType: 'payment', - paymentConfig: { paymentDataType: 'paymentInformation' }, - }), - ); - - expect(addLayoutSet).toHaveBeenCalledWith({ - layoutSetConfig: { id: 'testId', tasks: ['testId'] }, - layoutSetIdToUpdate: 'testId', - }); - - expect(addDataTypeToAppMetadata).toHaveBeenCalledWith({ - dataTypeId: 'paymentInformation', - taskId: 'testId', - }); - }); - - it('should add layoutSet and datatype when signing task is added', () => { - const bpmnDetails = createBpmnDetails('testId', 'signingTask', 'signing'); - const addProcessTaskManager = createAddProcessTaskManager(bpmnDetails); - - addProcessTaskManager.handleTaskAdd( - createTaskEvent('signing', { - taskType: 'signing', - signatureConfig: { signatureDataType: 'signingInformation' }, - }), - ); - - expect(addDataTypeToAppMetadata).toHaveBeenCalledWith({ - dataTypeId: 'signingInformation', - taskId: 'testId', - }); - }); - - it('should inform the consumer of the package that a task has been added with the taskEvent and taskType', () => { - const bpmnDetails = createBpmnDetails('testId', 'signingTask', 'signing'); - const addProcessTaskManager = createAddProcessTaskManager(bpmnDetails); - - const taskEvent = createTaskEvent('signing', { - taskType: 'signing', - signatureConfig: { signatureDataType: 'signingInformation' }, - }); - - addProcessTaskManager.handleTaskAdd(taskEvent); - - expect(onProcessTaskAdd).toHaveBeenCalledWith({ - taskEvent, - taskType: 'signing', - }); - }); -}); diff --git a/frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.ts b/frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.ts deleted file mode 100644 index 2aeac737cf3..00000000000 --- a/frontend/packages/process-editor/src/utils/ProcessTaskManager/AddProcessTaskManager.ts +++ /dev/null @@ -1,99 +0,0 @@ -import { type LayoutSetConfig } from 'app-shared/types/api/LayoutSetsResponse'; -import { getDataTypeIdFromBusinessObject } from '../../utils/hookUtils/hookUtils'; -import { type BpmnApiContextProps } from '../../contexts/BpmnApiContext'; -import { type BpmnDetails } from '../../types/BpmnDetails'; -import { type TaskEvent } from './types'; - -export class AddProcessTaskManager { - constructor( - private readonly addLayoutSet: BpmnApiContextProps['addLayoutSet'], - private readonly addDataTypeToAppMetadata: BpmnApiContextProps['addDataTypeToAppMetadata'], - private readonly bpmnDetails: BpmnDetails, - private readonly onProcessTaskAdd: BpmnApiContextProps['onProcessTaskAdd'], - ) {} - - /** - * Handles the task add event and delegates the handling to the specific task type - * @param taskEvent - */ - public handleTaskAdd(taskEvent: TaskEvent): void { - if (this.bpmnDetails.taskType === 'data') { - this.handleDataTaskAdd(); - } - - if (this.bpmnDetails.taskType === 'payment') { - this.handlePaymentTaskAdd(taskEvent); - } - - if (this.bpmnDetails.taskType === 'signing') { - this.handleSigningTaskAdd(taskEvent); - } - - // Informs the consumer of this package that a task with the given taskEvent and taskType has been added - this.onProcessTaskAdd({ - taskEvent, - taskType: this.bpmnDetails.taskType, - }); - } - - /** - * Adds a layout set to the added data task - * @private - */ - private handleDataTaskAdd(): void { - this.addLayoutSet(this.createLayoutSetConfig()); - } - - /** - * Adds a dataType and layoutSet to the added payment task - * @param taskEvent - * @private - */ - private handlePaymentTaskAdd(taskEvent: TaskEvent): void { - // Add layout set to the task - this.addLayoutSet(this.createLayoutSetConfig()); - - // Add dataType - const dataTypeId = getDataTypeIdFromBusinessObject( - this.bpmnDetails.taskType, - taskEvent.element.businessObject, - ); - - this.addDataTypeToAppMetadata({ - dataTypeId, - taskId: this.bpmnDetails.id, - }); - } - - /** - * Adds a dataType and layout set to the added signing task - * @param taskEvent - * @private - */ - private handleSigningTaskAdd(taskEvent: TaskEvent): void { - const dataTypeId = getDataTypeIdFromBusinessObject( - this.bpmnDetails.taskType, - taskEvent.element.businessObject, - ); - - this.addDataTypeToAppMetadata({ - dataTypeId, - taskId: this.bpmnDetails.id, - }); - } - - /** - * Creates the layout set config for the task - * @returns {{layoutSetIdToUpdate: string, layoutSetConfig: LayoutSetConfig}} - * @private - */ - private createLayoutSetConfig(): { - layoutSetIdToUpdate: string; - layoutSetConfig: LayoutSetConfig; - } { - return { - layoutSetIdToUpdate: this.bpmnDetails.id, - layoutSetConfig: { id: this.bpmnDetails.id, tasks: [this.bpmnDetails.id] }, - }; - } -} From 537fc06cbd60d60f209d2f907529540e524a04bc Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Wed, 5 Jun 2024 00:39:17 +0200 Subject: [PATCH 09/15] PR feedback --- .../handlers/OnProcessTaskAddHandler.test.ts | 3 +- .../handlers/OnProcessTaskAddHandler.ts | 13 +-- .../OnProcessTaskRemoveHandler.test.ts | 83 ++++++++----------- .../mutations/useAddLayoutSetMutation.ts | 19 ++--- 4 files changed, 48 insertions(+), 70 deletions(-) diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts index 6c22d32165b..9a130806fe0 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts @@ -4,9 +4,8 @@ import { OnProcessTaskAddHandler } from './OnProcessTaskAddHandler'; import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; import type { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; import type { BpmnTaskType } from '@altinn/process-editor/types/BpmnTaskType'; +import { app as appMock, org } from '@studio/testing/testids'; -const orgMock = 'testOrg'; -const appMock = 'testApp'; const currentPolicyMock: Policy = { requiredAuthenticationLevelOrg: '3', requiredAuthenticationLevelEndUser: '3', diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts index 450421c0d42..47552b04d5c 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts @@ -1,9 +1,11 @@ import { PaymentPolicyBuilder } from '../../../utils/policy'; import type { OnProcessTaskEvent } from '@altinn/process-editor/types/OnProcessTask'; import type { Policy } from 'app-shared/types/Policy'; -import type { LayoutSetConfig } from 'app-shared/types/api/LayoutSetsResponse'; import { getDataTypeIdFromBusinessObject } from '@altinn/process-editor/utils/hookUtils/hookUtils'; -import type { AddLayoutSetMutation } from '../../../hooks/mutations/useAddLayoutSetMutation'; +import type { + AddLayoutSetMutation, + AddLayoutSetMutationPayload, +} from '../../../hooks/mutations/useAddLayoutSetMutation'; export class OnProcessTaskAddHandler { constructor( @@ -101,10 +103,9 @@ export class OnProcessTaskAddHandler { * @returns {{layoutSetIdToUpdate: string, layoutSetConfig: LayoutSetConfig}} * @private */ - private createLayoutSetConfig(taskEvent: OnProcessTaskEvent['taskEvent']): { - layoutSetIdToUpdate: string; - layoutSetConfig: LayoutSetConfig; - } { + private createLayoutSetConfig( + taskEvent: OnProcessTaskEvent['taskEvent'], + ): AddLayoutSetMutationPayload { const elementId = taskEvent.element.id; return { layoutSetIdToUpdate: elementId, diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts index 461470717aa..c09334dd7ce 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts @@ -4,6 +4,7 @@ import { OnProcessTaskRemoveHandler } from './OnProcessTaskRemoveHandler'; import type { LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; import type { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; +import { BpmnBusinessObjectEditor } from '@altinn/process-editor/types/BpmnBusinessObjectEditor'; const orgMock = 'testOrg'; const appMock = 'testApp'; @@ -20,6 +21,21 @@ const mutateApplicationPolicyMock = jest.fn(); const deleteDataTypeFromAppMetadataMock = jest.fn(); const deletelayoutSetMock = jest.fn(); +const createTaskMetadataMock = ( + taskType: string, + businessObject?: BpmnBusinessObjectEditor, +): OnProcessTaskEvent => ({ + taskType: taskType, + taskEvent: { + element: { + id: 'testElementId', + businessObject: { + ...(businessObject || {}), + }, + }, + } as TaskEvent, +}); + const createOnRemoveProcessTaskHandler = ({ currentPolicy, layoutSets }: any) => { return new OnProcessTaskRemoveHandler( orgMock, @@ -42,19 +58,11 @@ describe('OnProcessTaskRemoveHandler', () => { sets: [{ id: 'testLayoutSetId', dataType: 'data', tasks: ['testElementId'] }], }; - const taskMetadata: OnProcessTaskEvent = { - taskType: 'data', - taskEvent: { - element: { - id: 'testElementId', - businessObject: { - id: 'testEventId', - $type: BpmnTypeEnum.Task, - extensionElements: undefined, - }, - }, - } as TaskEvent, - }; + const taskMetadata = createTaskMetadataMock('data', { + id: 'testEventId', + $type: BpmnTypeEnum.Task, + extensionElements: undefined, + }); const onProcessTaskRemoveHandler = createOnRemoveProcessTaskHandler({ layoutSets, @@ -102,22 +110,13 @@ describe('OnProcessTaskRemoveHandler', () => { ], }; - const taskMetadata: OnProcessTaskEvent = { - taskType: 'payment', - taskEvent: { - element: { - id: 'testElementId', - businessObject: {}, - }, - } as TaskEvent, - }; - const onProcessTaskRemoveHandler = createOnRemoveProcessTaskHandler({ currentPolicy, }); - onProcessTaskRemoveHandler.handleOnProcessTaskRemove(taskMetadata); + onProcessTaskRemoveHandler.handleOnProcessTaskRemove(createTaskMetadataMock('payment')); expect(mutateApplicationPolicyMock).toHaveBeenCalledWith(expectedResponse); + expect(mutateApplicationPolicyMock).toHaveBeenCalledTimes(1); expect(deletelayoutSetMock).not.toHaveBeenCalled(); }); @@ -126,19 +125,11 @@ describe('OnProcessTaskRemoveHandler', () => { sets: [{ id: 'testLayoutSetId', dataType: 'payment', tasks: ['testElementId'] }], }; - const taskMetadata: OnProcessTaskEvent = { - taskType: 'payment', - taskEvent: { - element: { - id: 'testElementId', - businessObject: { - id: 'testEventId', - $type: BpmnTypeEnum.Task, - extensionElements: undefined, - }, - }, - } as TaskEvent, - }; + const taskMetadata = createTaskMetadataMock('payment', { + id: 'testEventId', + $type: BpmnTypeEnum.Task, + extensionElements: undefined, + }); const onProcessTaskRemoveHandler = createOnRemoveProcessTaskHandler({ layoutSets, @@ -153,19 +144,11 @@ describe('OnProcessTaskRemoveHandler', () => { sets: [{ id: 'testLayoutSetId', dataType: 'signing', tasks: ['testElementId'] }], }; - const taskMetadata: OnProcessTaskEvent = { - taskType: 'signing', - taskEvent: { - element: { - id: 'testElementId', - businessObject: { - id: 'testEventId', - $type: BpmnTypeEnum.Task, - extensionElements: undefined, - }, - }, - } as TaskEvent, - }; + const taskMetadata = createTaskMetadataMock('signing', { + id: 'testEventId', + $type: BpmnTypeEnum.Task, + extensionElements: undefined, + }); const onProcessTaskRemoveHandler = createOnRemoveProcessTaskHandler({ layoutSets, diff --git a/frontend/app-development/hooks/mutations/useAddLayoutSetMutation.ts b/frontend/app-development/hooks/mutations/useAddLayoutSetMutation.ts index 320c8ff8127..093d82eba13 100644 --- a/frontend/app-development/hooks/mutations/useAddLayoutSetMutation.ts +++ b/frontend/app-development/hooks/mutations/useAddLayoutSetMutation.ts @@ -8,13 +8,14 @@ import type { LayoutSetsResponse, } from 'app-shared/types/api/AddLayoutSetResponse'; +export type AddLayoutSetMutationPayload = { + layoutSetIdToUpdate: string; + layoutSetConfig: LayoutSetConfig; +}; export type AddLayoutSetMutation = UseMutateFunction< - { - layoutSets: AddLayoutSetResponse; - layoutSetConfig: LayoutSetConfig; - }, + AddLayoutSetResponse, Error, - { layoutSetIdToUpdate: string; layoutSetConfig: LayoutSetConfig } + AddLayoutSetMutationPayload >; const isLayoutSets = (obj: LayoutSetsResponse): obj is LayoutSets => { @@ -28,13 +29,7 @@ export const useAddLayoutSetMutation = (org: string, app: string) => { const [_, setSelectedLayoutSet] = useLocalStorage('layoutSet/' + app, null); return useMutation({ - mutationFn: ({ - layoutSetIdToUpdate, - layoutSetConfig, - }: { - layoutSetIdToUpdate: string; - layoutSetConfig: LayoutSetConfig; - }) => + mutationFn: ({ layoutSetIdToUpdate, layoutSetConfig }: AddLayoutSetMutationPayload) => addLayoutSet(org, app, layoutSetIdToUpdate, layoutSetConfig).then((layoutSets) => ({ layoutSets, layoutSetConfig, From f490663f7e844aeb8dc6fed29c7bd2cc2cd7086f Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Wed, 5 Jun 2024 00:45:50 +0200 Subject: [PATCH 10/15] revert some changes that should not be committed --- .../processEditor/handlers/OnProcessTaskAddHandler.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts index 9a130806fe0..6c22d32165b 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts @@ -4,8 +4,9 @@ import { OnProcessTaskAddHandler } from './OnProcessTaskAddHandler'; import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; import type { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; import type { BpmnTaskType } from '@altinn/process-editor/types/BpmnTaskType'; -import { app as appMock, org } from '@studio/testing/testids'; +const orgMock = 'testOrg'; +const appMock = 'testApp'; const currentPolicyMock: Policy = { requiredAuthenticationLevelOrg: '3', requiredAuthenticationLevelEndUser: '3', From dc747c334c4d66e89d6cf2f9ddb78ab7aa89c6d5 Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Thu, 6 Jun 2024 09:52:50 +0200 Subject: [PATCH 11/15] PR feedback --- .../handlers/OnProcessTaskAddHandler.test.ts | 8 ++++---- .../processEditor/handlers/OnProcessTaskAddHandler.ts | 4 ---- .../handlers/OnProcessTaskRemoveHandler.test.ts | 7 +++---- .../processEditor/handlers/OnProcessTaskRemoveHandler.ts | 5 ----- frontend/testing/testids.js | 4 ++-- 5 files changed, 9 insertions(+), 19 deletions(-) diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts index 6c22d32165b..53e05202c71 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.test.ts @@ -4,9 +4,8 @@ import { OnProcessTaskAddHandler } from './OnProcessTaskAddHandler'; import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; import type { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; import type { BpmnTaskType } from '@altinn/process-editor/types/BpmnTaskType'; +import { app, org } from '@studio/testing/testids'; -const orgMock = 'testOrg'; -const appMock = 'testApp'; const currentPolicyMock: Policy = { requiredAuthenticationLevelOrg: '3', requiredAuthenticationLevelEndUser: '3', @@ -18,8 +17,8 @@ const addDataTypeToAppMetadataMock = jest.fn(); const createOnProcessTaskHandler = () => new OnProcessTaskAddHandler( - orgMock, - appMock, + org, + app, currentPolicyMock, addLayoutSetMock, mutateApplicationPolicyMock, @@ -55,6 +54,7 @@ describe('OnProcessTaskAddHandler', () => { layoutSetConfig: { id: 'testId', tasks: ['testId'] }, layoutSetIdToUpdate: 'testId', }); + expect(addLayoutSetMock).toHaveBeenCalledTimes(1); expect(addDataTypeToAppMetadataMock).not.toHaveBeenCalled(); expect(mutateApplicationPolicyMock).not.toHaveBeenCalled(); }); diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts index 47552b04d5c..d8d9b02dc7b 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskAddHandler.ts @@ -53,10 +53,8 @@ export class OnProcessTaskAddHandler { * @private */ private handlePaymentTaskAdd(taskMetadata: OnProcessTaskEvent): void { - // Add layoutSet for the task this.addLayoutSet(this.createLayoutSetConfig(taskMetadata.taskEvent)); - // Add dataType const dataTypeId = getDataTypeIdFromBusinessObject( taskMetadata.taskType, taskMetadata.taskEvent.element.businessObject, @@ -67,7 +65,6 @@ export class OnProcessTaskAddHandler { taskId: taskMetadata.taskEvent.element.id, }); - // Add default policy const paymentPolicyBuilder = new PaymentPolicyBuilder(this.org, this.app); const defaultPaymentPolicy = paymentPolicyBuilder.getDefaultPaymentPolicy( taskMetadata.taskEvent.element.id, @@ -86,7 +83,6 @@ export class OnProcessTaskAddHandler { * @private */ private handleSigningTaskAdd(taskMetadata: OnProcessTaskEvent): void { - // Add DataType const dataTypeId = getDataTypeIdFromBusinessObject( taskMetadata.taskType, taskMetadata.taskEvent.element.businessObject, diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts index c09334dd7ce..f9365fdc7c4 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts @@ -5,9 +5,8 @@ import type { LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; import type { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; import { BpmnBusinessObjectEditor } from '@altinn/process-editor/types/BpmnBusinessObjectEditor'; +import { app, org } from '@studio/testing/testids'; -const orgMock = 'testOrg'; -const appMock = 'testApp'; const currentPolicyMock: Policy = { requiredAuthenticationLevelOrg: '3', requiredAuthenticationLevelEndUser: '3', @@ -38,8 +37,8 @@ const createTaskMetadataMock = ( const createOnRemoveProcessTaskHandler = ({ currentPolicy, layoutSets }: any) => { return new OnProcessTaskRemoveHandler( - orgMock, - appMock, + org, + app, currentPolicy || currentPolicyMock, layoutSets || layoutSetsMock, mutateApplicationPolicyMock, diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts index 326286b572d..05f4d32578e 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.ts @@ -59,7 +59,6 @@ export class OnProcessTaskRemoveHandler { * @private */ private handlePaymentTaskRemove(taskMetadata: OnProcessTaskEvent): void { - // Delete dataType const dataTypeId = getDataTypeIdFromBusinessObject( taskMetadata.taskType, taskMetadata.taskEvent.element.businessObject, @@ -69,7 +68,6 @@ export class OnProcessTaskRemoveHandler { dataTypeId, }); - // Delete policy const paymentPolicyBuilder = new PaymentPolicyBuilder(this.org, this.app); const currentPaymentRuleId = paymentPolicyBuilder.getPolicyRuleId( taskMetadata.taskEvent.element.id, @@ -83,7 +81,6 @@ export class OnProcessTaskRemoveHandler { this.mutateApplicationPolicy(updatedPolicy); - // Delete layoutSet const layoutSetId = getLayoutSetIdFromTaskId( taskMetadata.taskEvent.element.id, this.layoutSets, @@ -102,7 +99,6 @@ export class OnProcessTaskRemoveHandler { * @private */ private handleSigningTaskRemove(taskMetadata: OnProcessTaskEvent): void { - // Delete dataType const dataTypeId = getDataTypeIdFromBusinessObject( taskMetadata.taskType, taskMetadata.taskEvent.element.businessObject, @@ -112,7 +108,6 @@ export class OnProcessTaskRemoveHandler { dataTypeId, }); - // Delete layoutSet const layoutSetId = getLayoutSetIdFromTaskId( taskMetadata.taskEvent.element.id, this.layoutSets, diff --git a/frontend/testing/testids.js b/frontend/testing/testids.js index d30cafbaa5b..26b3047fb7a 100644 --- a/frontend/testing/testids.js +++ b/frontend/testing/testids.js @@ -11,5 +11,5 @@ export const typeItemId = (pointer) => `type-item-${pointer}`; export const userMenuItemId = 'user-menu-item'; export const pageAccordionContentId = (pageName) => `page-accordion-content-${pageName}`; export const profileButtonId = 'profileButton'; -export const org = 'org'; -export const app = 'app'; +export const org = 'testOrg'; +export const app = 'testApp'; From 853a4dc26e6d16d7f1c2fa20a514f644ef39a64c Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Thu, 6 Jun 2024 09:53:47 +0200 Subject: [PATCH 12/15] fixed ts error --- .../processEditor/handlers/OnProcessTaskRemoveHandler.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts index f9365fdc7c4..eeebb9b478e 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts @@ -6,6 +6,7 @@ import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; import type { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; import { BpmnBusinessObjectEditor } from '@altinn/process-editor/types/BpmnBusinessObjectEditor'; import { app, org } from '@studio/testing/testids'; +import { BpmnTaskType } from '@altinn/process-editor/types/BpmnTaskType'; const currentPolicyMock: Policy = { requiredAuthenticationLevelOrg: '3', @@ -24,7 +25,7 @@ const createTaskMetadataMock = ( taskType: string, businessObject?: BpmnBusinessObjectEditor, ): OnProcessTaskEvent => ({ - taskType: taskType, + taskType: taskType as BpmnTaskType, taskEvent: { element: { id: 'testElementId', From e5a43f93d99899a44a1901d3dc31debc6da376e8 Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Thu, 6 Jun 2024 10:01:24 +0200 Subject: [PATCH 13/15] Fixed broken test --- .../RedirectToCreatePageButton.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/packages/process-editor/src/components/ConfigPanel/ConfigEndEvent/CustomReceiptContent/RedirectToCreatePageButton/RedirectToCreatePageButton.test.tsx b/frontend/packages/process-editor/src/components/ConfigPanel/ConfigEndEvent/CustomReceiptContent/RedirectToCreatePageButton/RedirectToCreatePageButton.test.tsx index ddbecfe6da1..1699cd3103c 100644 --- a/frontend/packages/process-editor/src/components/ConfigPanel/ConfigEndEvent/CustomReceiptContent/RedirectToCreatePageButton/RedirectToCreatePageButton.test.tsx +++ b/frontend/packages/process-editor/src/components/ConfigPanel/ConfigEndEvent/CustomReceiptContent/RedirectToCreatePageButton/RedirectToCreatePageButton.test.tsx @@ -19,7 +19,7 @@ describe('RedirectToCreatePageButton', () => { const navigationButton = screen.getByRole('link', { name: textMock('process_editor.configuration_panel_custom_receipt_navigate_to_lage_button'), }); - expect(navigationButton).toHaveAttribute('href', '/editor/org/app/ui-editor'); + expect(navigationButton).toHaveAttribute('href', '/editor/testOrg/testApp/ui-editor'); }); }); From c0f75889a93facdb015830af949fe52c6ea08ef9 Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Thu, 6 Jun 2024 10:11:32 +0200 Subject: [PATCH 14/15] eslint --- .../processEditor/handlers/OnProcessTaskRemoveHandler.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts index eeebb9b478e..a133d408898 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts @@ -6,7 +6,7 @@ import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; import type { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; import { BpmnBusinessObjectEditor } from '@altinn/process-editor/types/BpmnBusinessObjectEditor'; import { app, org } from '@studio/testing/testids'; -import { BpmnTaskType } from '@altinn/process-editor/types/BpmnTaskType'; +import type { BpmnTaskType } from '@altinn/process-editor/types/BpmnTaskType'; const currentPolicyMock: Policy = { requiredAuthenticationLevelOrg: '3', From 62b87afa0ee7f892bee7ba1e12b044d926aad5d5 Mon Sep 17 00:00:00 2001 From: "davidovrelid.com" Date: Thu, 6 Jun 2024 10:16:50 +0200 Subject: [PATCH 15/15] eslint --- .../processEditor/handlers/OnProcessTaskRemoveHandler.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts index a133d408898..fc6fbb85560 100644 --- a/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts +++ b/frontend/app-development/features/processEditor/handlers/OnProcessTaskRemoveHandler.test.ts @@ -4,7 +4,7 @@ import { OnProcessTaskRemoveHandler } from './OnProcessTaskRemoveHandler'; import type { LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; import { BpmnTypeEnum } from '@altinn/process-editor/enum/BpmnTypeEnum'; import type { TaskEvent } from '@altinn/process-editor/types/TaskEvent'; -import { BpmnBusinessObjectEditor } from '@altinn/process-editor/types/BpmnBusinessObjectEditor'; +import type { BpmnBusinessObjectEditor } from '@altinn/process-editor/types/BpmnBusinessObjectEditor'; import { app, org } from '@studio/testing/testids'; import type { BpmnTaskType } from '@altinn/process-editor/types/BpmnTaskType';