From 2912245cf7ed1c7c64a0c1b09e9a559fbc5edce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Souchet=20C=C3=A9line?= Date: Mon, 21 Aug 2023 11:01:29 +0200 Subject: [PATCH] Fix other tests & code --- .../converter/EventDefinitionConverter.ts | 6 +- .../json/BpmnJsonParser.flowNode.test.ts | 2 +- .../parser/json/BpmnJsonParser.marker.test.ts | 3 +- test/unit/helpers/JsonBuilder.ts | 62 ++++++++----------- 4 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/component/parser/json/converter/EventDefinitionConverter.ts b/src/component/parser/json/converter/EventDefinitionConverter.ts index f3aa74b582..cf498ff2ae 100644 --- a/src/component/parser/json/converter/EventDefinitionConverter.ts +++ b/src/component/parser/json/converter/EventDefinitionConverter.ts @@ -20,6 +20,8 @@ import type { TEventDefinition } from '../../../../model/bpmn/json/baseElement/r import type { ConvertedElements } from './utils'; import { ensureIsArray } from '../../../helpers/array-utils'; +type EventDefinitions = string | TEventDefinition | (string | TEventDefinition)[]; + /** * @internal */ @@ -28,8 +30,8 @@ export default class EventDefinitionConverter { deserialize(definitions: TDefinitions): void { eventDefinitionKinds.forEach(eventDefinitionKind => { - // sometimes eventDefinition is simple and therefore it is parsed as empty string "", in that case eventDefinition will be converted to an empty object - const eventDefinitions: string | TEventDefinition | (string | TEventDefinition)[] = definitions[eventDefinitionKind + 'EventDefinition']; + // sometimes eventDefinition is simple, and therefore it is parsed as empty string "", in that case eventDefinition will be converted to an empty object + const eventDefinitions = definitions[(eventDefinitionKind + 'EventDefinition') as keyof TDefinitions] as EventDefinitions; ensureIsArray(eventDefinitions, true).forEach(eventDefinition => this.convertedElements.registerEventDefinitionsOfDefinition(eventDefinition.id, eventDefinitionKind), ); diff --git a/test/unit/component/parser/json/BpmnJsonParser.flowNode.test.ts b/test/unit/component/parser/json/BpmnJsonParser.flowNode.test.ts index 5a7d56e901..a414e7319b 100644 --- a/test/unit/component/parser/json/BpmnJsonParser.flowNode.test.ts +++ b/test/unit/component/parser/json/BpmnJsonParser.flowNode.test.ts @@ -38,7 +38,7 @@ describe.each([ ['parallelGateway', ShapeBpmnElementKind.GATEWAY_PARALLEL], ['eventBasedGateway', ShapeBpmnElementKind.GATEWAY_EVENT_BASED], ['complexGateway', ShapeBpmnElementKind.GATEWAY_COMPLEX], -])('parse bpmn as json for %s', (bpmnKind: string, expectedShapeBpmnElementKind: ShapeBpmnElementKind) => { +] as [keyof TProcess, ShapeBpmnElementKind][])('parse bpmn as json for %s', (bpmnKind: keyof TProcess, expectedShapeBpmnElementKind: ShapeBpmnElementKind) => { const processWithFlowNodeAsObject: TProcess = { [bpmnKind]: { id: `${bpmnKind}_id_0`, diff --git a/test/unit/component/parser/json/BpmnJsonParser.marker.test.ts b/test/unit/component/parser/json/BpmnJsonParser.marker.test.ts index 4bb5fa496a..70884beef7 100644 --- a/test/unit/component/parser/json/BpmnJsonParser.marker.test.ts +++ b/test/unit/component/parser/json/BpmnJsonParser.marker.test.ts @@ -18,6 +18,7 @@ import { parseJsonAndExpectOnlyFlowNodes } from '../../../helpers/JsonTestUtils' import { getExpectedMarkers, verifyShape } from '../../../helpers/bpmn-model-expect'; import type { BpmnJsonModel } from '@lib/model/bpmn/json/BPMN20'; +import type { TProcess } from '@lib/model/bpmn/json/baseElement/rootElement/rootElement'; import type { TMultiInstanceLoopCharacteristics, TStandardLoopCharacteristics } from '@lib/model/bpmn/json/baseElement/loopCharacteristics'; import { ShapeBpmnCallActivityKind, ShapeBpmnElementKind, ShapeBpmnMarkerKind } from '@lib/model/bpmn/internal'; @@ -34,7 +35,7 @@ describe.each([ ['manualTask', ShapeBpmnElementKind.TASK_MANUAL], ['scriptTask', ShapeBpmnElementKind.TASK_SCRIPT], ['businessRuleTask', ShapeBpmnElementKind.TASK_BUSINESS_RULE], -])(`parse bpmn as json for '%s'`, (bpmnSemanticType: string, expectedShapeBpmnElementKind: ShapeBpmnElementKind) => { +] as [keyof TProcess, ShapeBpmnElementKind][])(`parse bpmn as json for '%s'`, (bpmnSemanticType: keyof TProcess, expectedShapeBpmnElementKind: ShapeBpmnElementKind) => { describe.each([ ['standardLoopCharacteristics', ShapeBpmnMarkerKind.LOOP], ['multiInstanceLoopCharacteristics', ShapeBpmnMarkerKind.MULTI_INSTANCE_PARALLEL], diff --git a/test/unit/helpers/JsonBuilder.ts b/test/unit/helpers/JsonBuilder.ts index a0c5d609e1..320ea20a10 100644 --- a/test/unit/helpers/JsonBuilder.ts +++ b/test/unit/helpers/JsonBuilder.ts @@ -23,7 +23,6 @@ import type { TBaseElement, TLane, TLaneSet, TMessageFlow } from '@lib/model/bpm import type { TFlowElement } from '@lib/model/bpmn/json/baseElement/flowElement'; import type { TFlowNode } from '@lib/model/bpmn/json/baseElement/flowElement'; import type { TBoundaryEvent, TCatchEvent, TThrowEvent } from '@lib/model/bpmn/json/baseElement/flowNode/event'; -import type { TParticipant } from '@lib/model/bpmn/json/baseElement/participant'; import type { TCollaboration } from '@lib/model/bpmn/json/baseElement/rootElement/collaboration'; import type { TEventDefinition } from '@lib/model/bpmn/json/baseElement/rootElement/eventDefinition'; import type { TProcess } from '@lib/model/bpmn/json/baseElement/rootElement/rootElement'; @@ -290,17 +289,13 @@ function addParticipantProcessAndElements(processParameter: BuildProcessParamete if (processParameter.withParticipant) { addParticipant(id, jsonModel); } - updateBpmnElement( - jsonModel.definitions.process as TProcess | TProcess[], - { id: processParameter.withParticipant ? `process_${id}` : id }, - (value: TProcess | TProcess[]) => (jsonModel.definitions.process = value), - ); + jsonModel.definitions.process = getBpmnElementNewValue(jsonModel.definitions.process as TProcess | TProcess[], { id: processParameter.withParticipant ? `process_${id}` : id }); addElementsOnProcess(processParameter, jsonModel, index); } function addParticipant(id: string, jsonModel: BpmnJsonModel): void { const collaboration: TCollaboration = getElementOfArray(jsonModel.definitions.collaboration as TCollaboration); - updateBpmnElement(collaboration.participant, { id: id, processRef: `process_${id}` }, (value: TParticipant | TParticipant[]) => (collaboration.participant = value)); + collaboration.participant = getBpmnElementNewValue(collaboration.participant, { id: id, processRef: `process_${id}` }); const shape = { id: `shape_${id}`, @@ -314,7 +309,7 @@ function addMessageFlow(messageFlowParameter: BuildMessageFlowParameter, jsonMod const messageFlow: TMessageFlow = messageFlowParameter; const collaboration: TCollaboration = getElementOfArray(jsonModel.definitions.collaboration as TCollaboration); - updateBpmnElement(collaboration.messageFlow, messageFlow, (value: TMessageFlow | TMessageFlow[]) => (collaboration.messageFlow = value)); + collaboration.messageFlow = getBpmnElementNewValue(collaboration.messageFlow, messageFlow); addEdge(jsonModel, { bpmnElement: messageFlow.id, @@ -332,7 +327,7 @@ const addGlobalTask = ({ id, bpmnKind = 'globalTask', ...rest }: BuildGlobalTask }; const definitions = jsonModel.definitions; - updateBpmnElement(definitions[bpmnKind], globalTask, (value: TGlobalTask | TGlobalTask[]) => (definitions[bpmnKind] = value)); + definitions[bpmnKind] = getBpmnElementNewValue(definitions[bpmnKind], globalTask); }; function addElementsOnProcess(processParameter: BuildProcessParameter, json: BpmnJsonModel, processIndex: number): void { @@ -423,15 +418,16 @@ function getElementOfArray(object: T | T[], index = 0): T { } } -function updateBpmnElement(parentElement: T | T[], childElement: T, setValue: (value: T | T[]) => void): void { - if (parentElement) { - if (!Array.isArray(parentElement)) { - setValue([parentElement, childElement]); +function getBpmnElementNewValue(currentValue: T | T[], elementToAdd: T): T | T[] { + if (currentValue) { + if (!Array.isArray(currentValue)) { + return [currentValue, elementToAdd]; } else { - parentElement.push(childElement); + currentValue.push(elementToAdd); + return currentValue; } } else { - setValue(childElement); + return elementToAdd; } } @@ -443,8 +439,9 @@ function addLane(jsonModel: BpmnJsonModel, { id, name, ...rest }: BuildLaneParam if (name) { lane.name = name; } + const laneSet = getElementOfArray(jsonModel.definitions.process as TProcess | TProcess[], processIndex).laneSet as TLaneSet; - updateBpmnElement(laneSet.lane, lane, (value: TLane | TLane[]) => (laneSet.lane = value)); + laneSet.lane = getBpmnElementNewValue(laneSet.lane, lane); const shape = { id: `shape_${lane.id}`, @@ -470,39 +467,34 @@ function addProcessElementWithEdge(jsonModel: BpmnJsonModel, bpmnKind: keyof TPr }); } -function addProcessElement(jsonModel: BpmnJsonModel, bpmnKind: keyof TProcess, { id, index, processIndex, ...rest }: BuildProcessElementParameter): TFlowNode { - const processElement: TFlowNode | TArtifact = { +function addProcessElement(jsonModel: BpmnJsonModel, bpmnKind: keyof TProcess, { id, index, processIndex, ...rest }: BuildProcessElementParameter): TFlowNode | TArtifact { + const processElement: TFlowElement | TArtifact = { id: id ? id : `${bpmnKind}_id_${processIndex}_${index}`, ...rest, }; const process: TProcess = getElementOfArray(jsonModel.definitions.process as TProcess | TProcess[], processIndex); - updateBpmnElement(process[bpmnKind], processElement, (value: TFlowNode | TFlowNode[] | TArtifact | TArtifact[]) => (process[bpmnKind] = value)); + (process[bpmnKind] as TFlowElement | TArtifact | TFlowNode[] | TArtifact[]) = getBpmnElementNewValue( + process[bpmnKind] as TFlowElement | TArtifact | TFlowNode[] | TArtifact[], + processElement, + ); return processElement; } function addShape(jsonModel: BpmnJsonModel, shape: BPMNShape): void { const bpmnPlane: BPMNPlane = getElementOfArray(jsonModel.definitions.BPMNDiagram).BPMNPlane; - updateBpmnElement( - bpmnPlane.BPMNShape, - { - id: `shape_${shape.bpmnElement}`, - ...shape, - }, - (value: BPMNShape | BPMNShape[]) => (bpmnPlane.BPMNShape = value), - ); + bpmnPlane.BPMNShape = getBpmnElementNewValue(bpmnPlane.BPMNShape, { + id: `shape_${shape.bpmnElement}`, + ...shape, + }); } function addEdge(jsonModel: BpmnJsonModel, edge: BPMNEdge): void { const bpmnPlane: BPMNPlane = getElementOfArray(jsonModel.definitions.BPMNDiagram).BPMNPlane; - updateBpmnElement( - bpmnPlane.BPMNEdge, - { - id: `edge_${edge.bpmnElement}`, - ...edge, - }, - (value: BPMNEdge | BPMNEdge[]) => (bpmnPlane.BPMNEdge = value), - ); + bpmnPlane.BPMNEdge = getBpmnElementNewValue(bpmnPlane.BPMNEdge, { + id: `edge_${edge.bpmnElement}`, + ...edge, + }); } function addEventDefinitions(