Skip to content

Commit

Permalink
chore: refactor with code review results in mind
Browse files Browse the repository at this point in the history
  • Loading branch information
barmac committed Dec 10, 2024
1 parent 8b9c3a7 commit dc78601
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 123 deletions.
74 changes: 44 additions & 30 deletions lib/camunda-cloud/CreateZeebeUserTaskBehavior.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createElement } from '../util/ElementUtil';
import { getZeebeUserTaskElement } from './util/ZeebeUserTaskUtil';
import { getBusinessObject, is } from 'bpmn-js/lib/util/ModelUtil';
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';

import { createElement } from '../util/ElementUtil';
import { getExtensionElementsList } from '../util/ExtensionElementsUtil';

const HIGH_PRIORITY = 5000;

/**
Expand All @@ -20,48 +21,61 @@ export default class CreateZeebeUserTaskBehavior extends CommandInterceptor {
HIGH_PRIORITY,
function(context) {
const shape = context.shape || context.newShape;
const isCopy = context.hints && context.hints.createElementsBehavior === false;
const explicitlyDisabled = context.hints && context.hints.createElementsBehavior === false;

if (!is(shape, 'bpmn:UserTask') || isCopy) {
if (!is(shape, 'bpmn:UserTask') || explicitlyDisabled) {
return;
}

const businessObject = getBusinessObject(shape);

// Use getZeebeUserTaskElement to check if zeebe:userTask already exists
let userTaskElement = getZeebeUserTaskElement(businessObject);

if (!userTaskElement) {
let extensionElements = businessObject.get('extensionElements');

if (!extensionElements) {
extensionElements = createElement(
'bpmn:ExtensionElements',
{
values: [],
},
businessObject,
bpmnFactory
);
let userTaskElement = getZeebeUserTask(shape);
if (userTaskElement) {
return;
}

modeling.updateProperties(shape, { extensionElements });
}
const businessObject = getBusinessObject(shape);
let extensionElements = businessObject.get('extensionElements');

userTaskElement = createElement(
'zeebe:UserTask',
{},
extensionElements,
if (!extensionElements) {
extensionElements = createElement(
'bpmn:ExtensionElements',
{
values: [],
},
businessObject,
bpmnFactory
);

modeling.updateModdleProperties(shape, extensionElements, {
values: [ ...(extensionElements.values || []), userTaskElement ],
});
modeling.updateProperties(shape, { extensionElements });
}

userTaskElement = createElement(
'zeebe:UserTask',
{},
extensionElements,
bpmnFactory
);

modeling.updateModdleProperties(shape, extensionElements, {
values: [ ...(extensionElements.values || []), userTaskElement ],
});
},
true
);
}
}

CreateZeebeUserTaskBehavior.$inject = [ 'bpmnFactory', 'eventBus', 'modeling' ];

/**
* Get zeebe:userTask extension.
*
* @param {djs.model.Base|ModdleElement} element
*
* @returns {ModdleElement|null}
*/
function getZeebeUserTask(element) {
const businessObject = getBusinessObject(element);
const userTaskElements = getExtensionElementsList(businessObject, 'zeebe:UserTask');

return userTaskElements[0] || null;
}
42 changes: 0 additions & 42 deletions lib/camunda-cloud/util/ZeebeUserTaskUtil.js

This file was deleted.

140 changes: 105 additions & 35 deletions test/camunda-cloud/CreateZeebeUserTaskBehaviorSpec.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { bootstrapCamundaCloudModeler, inject } from 'test/TestHelper';

import { getBusinessObject, is } from 'bpmn-js/lib/util/ModelUtil';
import { find } from 'min-dash';

import {
getZeebeUserTaskElement,
getZeebeUserTaskElements,
} from '../../lib/camunda-cloud/util/ZeebeUserTaskUtil';
import { getExtensionElementsList } from 'lib/util/ExtensionElementsUtil';

import { getBusinessObject, is } from 'bpmn-js/lib/util/ModelUtil';

import emptyProcessDiagramXML from './process-empty.bpmn';
import userTasksXML from './process-user-tasks.bpmn';

describe('camunda-cloud/features/modeling - CreateZeebeUserTaskBehavior', function() {
describe('when creating new shapes', function() {

describe('when a shape is created', function() {

beforeEach(bootstrapCamundaCloudModeler(emptyProcessDiagramXML));


it('should execute when creating bpmn:UserTask', inject(function(
canvas,
modeling
Expand All @@ -33,15 +33,44 @@ describe('camunda-cloud/features/modeling - CreateZeebeUserTaskBehavior', functi

// then
const businessObject = getBusinessObject(newShape),
extensionElements = businessObject.get('extensionElements'),
zeebeUserTaskExtension = getZeebeUserTaskElement(newShape);
zeebeUserTaskExtensions = getExtensionElementsList(businessObject, 'zeebe:UserTask');

expect(zeebeUserTaskExtension).to.exist;
expect(extensionElements).to.exist;
expect(zeebeUserTaskExtension.$parent).to.equal(extensionElements);
expect(zeebeUserTaskExtensions).to.exist;
expect(zeebeUserTaskExtensions).to.have.lengthOf(1);
}));

it('should not execute when creating bpmn:Task', inject(function(

it('should NOT execute when zeebe:UserTask already present', inject(function(
canvas,
bpmnFactory,
modeling
) {

// given
const rootElement = canvas.getRootElement(),
bo = bpmnFactory.create('bpmn:UserTask', {
extensionElements: bpmnFactory.create('bpmn:ExtensionElements', {
values: [ bpmnFactory.create('zeebe:UserTask') ],
}),
});

// when
const newShape = modeling.createShape(
{ type: 'bpmn:UserTask', businessObject: bo },
{ x: 100, y: 100 },
rootElement
);

// then
const businessObject = getBusinessObject(newShape),
zeebeUserTaskExtensions = getExtensionElementsList(businessObject, 'zeebe:UserTask');

expect(zeebeUserTaskExtensions).to.exist;
expect(zeebeUserTaskExtensions).to.have.lengthOf(1);
}));


it('should NOT execute when creating bpmn:Task', inject(function(
canvas,
modeling
) {
Expand All @@ -57,24 +86,27 @@ describe('camunda-cloud/features/modeling - CreateZeebeUserTaskBehavior', functi
);

// then
const zeebeUserTaskExtension = getZeebeUserTaskElement(newShape);
const zeebeUserTaskExtension = getZeebeUserTask(newShape);

expect(zeebeUserTaskExtension).not.to.exist;
}));
});

describe('when copying bpmn:UserTask', function() {

describe('when a shape is pasted', function() {

beforeEach(bootstrapCamundaCloudModeler(userTasksXML));

it('should re-use existing extensionElement', inject(function(

it('should NOT add zeebe:UserTask', inject(function(
canvas,
copyPaste,
elementRegistry
) {

// given
const rootElement = canvas.getRootElement();
const userTask = elementRegistry.get('withZeebeUserTask');
const userTask = elementRegistry.get('UserTask_1');

// when
copyPaste.copy(userTask);
Expand All @@ -92,25 +124,21 @@ describe('camunda-cloud/features/modeling - CreateZeebeUserTaskBehavior', functi
is(element, 'bpmn:UserTask')
);

const businessObject = getBusinessObject(pastedUserTask),
extensionElements = businessObject.get('extensionElements'),
zeebeUserTaskExtensions = getZeebeUserTaskElements(pastedUserTask);
const zeebeUserTask = getZeebeUserTask(pastedUserTask);

expect(zeebeUserTaskExtensions).to.exist;
expect(extensionElements).to.exist;
expect(zeebeUserTaskExtensions.length).to.equal(1);
expect(zeebeUserTaskExtensions[0].$parent).to.equal(extensionElements);
expect(zeebeUserTask).not.to.exist;
}));

it('should not add zeebe:UserTask if it was not already present', inject(function(

it('should keep existing zeebe:UserTask', inject(function(
canvas,
copyPaste,
elementRegistry
) {

// given
const rootElement = canvas.getRootElement();
const userTask = elementRegistry.get('withEmptyExternalReference');
const userTask = elementRegistry.get('withZeebeUserTask');

// when
copyPaste.copy(userTask);
Expand All @@ -127,21 +155,20 @@ describe('camunda-cloud/features/modeling - CreateZeebeUserTaskBehavior', functi
const pastedUserTask = find(elements, (element) =>
is(element, 'bpmn:UserTask')
);
const zeebeUserTasks = getExtensionElementsList(pastedUserTask, 'zeebe:UserTask');

const businessObject = getBusinessObject(pastedUserTask),
extensionElements = businessObject.get('extensionElements'),
zeebeUserTaskExtensions = getZeebeUserTaskElements(pastedUserTask);

expect(zeebeUserTaskExtensions).to.exist;
expect(extensionElements).to.exist;
expect(zeebeUserTaskExtensions.length).to.equal(0);
expect(zeebeUserTasks).to.exist;
expect(zeebeUserTasks).to.have.lengthOf(1);
}));
});

describe('when replacing bpmn:Task', function() {

describe('when a shape is replaced', function() {

beforeEach(bootstrapCamundaCloudModeler(userTasksXML));

it('should execute when replacing to bpmn:UserTask', inject(function(

it('should add zeebe:UserTask when target is bpmn:UserTask', inject(function(
elementRegistry,
bpmnReplace,
canvas,
Expand All @@ -161,9 +188,52 @@ describe('camunda-cloud/features/modeling - CreateZeebeUserTaskBehavior', functi

// then
const updatedTask = elementRegistry.get(task.id),
zeebeUserTaskExtension = getZeebeUserTaskElement(updatedTask);
zeebeUserTaskExtension = getZeebeUserTask(updatedTask);

expect(zeebeUserTaskExtension).to.exist;
}));


it('should NOT add zeebe:UserTask when target is bpmn:ServiceTask', inject(function(
elementRegistry,
bpmnReplace,
canvas,
modeling
) {

// given
const rootElement = canvas.getRootElement();

// when
const task = modeling.createShape(
{ type: 'bpmn:Task', id: 'simpleTask' },
{ x: 100, y: 100 },
rootElement
);
bpmnReplace.replaceElement(task, { type: 'bpmn:ServiceTask' });

// then
const updatedTask = elementRegistry.get(task.id),
zeebeUserTask = getZeebeUserTask(updatedTask);

expect(zeebeUserTask).not.to.exist;
}));
});
});


// helpers //////////

/**
* Get the first zeebe:userTask element of an element.
*
* @param {djs.model.Base|ModdleElement} element
*
* @returns {ModdleElement|null}
*/
function getZeebeUserTask(element) {
const businessObject = getBusinessObject(element);
const userTaskElements = getExtensionElementsList(businessObject, 'zeebe:UserTask');

return userTaskElements[0] || null;
}
Loading

0 comments on commit dc78601

Please sign in to comment.