bpmn-js/test/spec/features/modeling/behavior/RootElementReferenceBehaviorSpec.js
Philipp Fromme 477217c891 feat(copy-paste): copy error, escalation, message and signal references
* copy references when copying element
* add referenced root element if it doesn't exist
* do NOT add referenced root element if root element with same ID exists

Related to camunda/camunda-modeler#1049.
Related to camunda/camunda-modeler#1463.
2019-12-11 23:08:14 +01:00

265 lines
6.6 KiB
JavaScript

import {
bootstrapModeler,
getBpmnJS,
inject
} from 'test/TestHelper';
import coreModule from 'lib/core';
import modelingModule from 'lib/features/modeling';
import {
getBusinessObject,
is
} from 'lib/util/ModelUtil';
import {
remove as collectionRemove
} from 'diagram-js/lib/util/Collections';
import {
filter,
find,
forEach,
matchPattern
} from 'min-dash';
var testModules = [
coreModule,
modelingModule
];
describe('features/modeling - root element reference behavior', function() {
var diagramXML = require('./RootElementReferenceBehavior.bpmn');
beforeEach(bootstrapModeler(diagramXML, { modules: testModules }));
describe('add root element', function() {
forEach([
'error',
'escalation',
'message',
'signal'
], function(type) {
describe(type, function() {
var id = capitalizeFirstChar(type) + 'BoundaryEvent_1';
var boundaryEvent,
host,
rootElement;
describe('should add', function() {
beforeEach(inject(function(bpmnjs, copyPaste, elementRegistry, modeling) {
// given
boundaryEvent = elementRegistry.get(id);
host = elementRegistry.get('Task_2');
var businessObject = getBusinessObject(boundaryEvent),
eventDefinitions = businessObject.get('eventDefinitions'),
eventDefinition = eventDefinitions[ 0 ];
rootElement = getRootElementReferenced(eventDefinition);
// when
copyPaste.copy(boundaryEvent);
modeling.removeShape(boundaryEvent);
collectionRemove(bpmnjs.getDefinitions().get('rootElements'), rootElement);
expect(hasRootElement(rootElement)).to.be.false;
boundaryEvent = copyPaste.paste({
element: host,
point: {
x: host.x,
y: host.y
},
hints: {
attach: 'attach'
}
})[0];
}));
it('<do>', function() {
// then
expect(hasRootElement(rootElement)).to.be.true;
});
it('<undo>', inject(function(commandStack) {
// when
commandStack.undo();
// then
expect(hasRootElement(rootElement)).to.be.false;
}));
it('<redo>', inject(function(commandStack) {
// given
commandStack.undo();
// when
commandStack.redo();
// then
expect(hasRootElement(rootElement)).to.be.true;
}));
});
it('should NOT add', inject(function(bpmnFactory, bpmnjs, copyPaste, elementRegistry, moddleCopy, modeling) {
// given
boundaryEvent = elementRegistry.get(id);
host = elementRegistry.get('Task_2');
var businessObject = getBusinessObject(boundaryEvent),
eventDefinitions = businessObject.get('eventDefinitions'),
eventDefinition = eventDefinitions[ 0 ],
rootElements = bpmnjs.getDefinitions().get('rootElements');
rootElement = getRootElementReferenced(eventDefinition);
copyPaste.copy(boundaryEvent);
modeling.removeShape(boundaryEvent);
collectionRemove(rootElements, rootElement);
expect(hasRootElement(rootElement)).to.be.false;
var rootElementWithSameId = bpmnFactory.create(rootElement.$type);
moddleCopy.copyElement(rootElement, rootElementWithSameId);
collectionRemove(rootElements, rootElementWithSameId);
// when
boundaryEvent = copyPaste.paste({
element: host,
point: {
x: host.x,
y: host.y
},
hints: {
attach: 'attach'
}
})[0];
// then
var rootElementsOfType = filter(rootElements, matchPattern({ $type: rootElement.$type }));
expect(rootElementsOfType).to.have.lengthOf(1);
}));
});
});
});
describe('copy root element reference', function() {
forEach([
'error',
'escalation',
'message',
'signal'
], function(type) {
describe(type, function() {
var id = capitalizeFirstChar(type) + 'BoundaryEvent_1';
var boundaryEvent,
host,
rootElement;
beforeEach(inject(function(copyPaste, elementRegistry) {
// given
boundaryEvent = elementRegistry.get(id);
host = elementRegistry.get('Task_2');
var businessObject = getBusinessObject(boundaryEvent),
eventDefinitions = businessObject.get('eventDefinitions'),
eventDefinition = eventDefinitions[ 0 ];
rootElement = getRootElementReferenced(eventDefinition);
copyPaste.copy(boundaryEvent);
// when
boundaryEvent = copyPaste.paste({
element: host,
point: {
x: host.x,
y: host.y
},
hints: {
attach: 'attach'
}
})[0];
}));
it('should copy root element reference', function() {
// then
var businessObject = getBusinessObject(boundaryEvent),
eventDefinitions = businessObject.get('eventDefinitions'),
eventDefinition = eventDefinitions[ 0 ];
expect(getRootElementReferenced(eventDefinition)).to.equal(rootElement);
});
});
});
});
});
// helpers //////////
function getRootElementReferenced(eventDefinition) {
if (is(eventDefinition, 'bpmn:ErrorEventDefinition')) {
return eventDefinition.get('errorRef');
} else if (is(eventDefinition, 'bpmn:EscalationEventDefinition')) {
return eventDefinition.get('escalationRef');
} else if (is(eventDefinition, 'bpmn:MessageEventDefinition')) {
return eventDefinition.get('messageRef');
} else if (is(eventDefinition, 'bpmn:SignalEventDefinition')) {
return eventDefinition.get('signalRef');
}
}
function hasRootElement(rootElement) {
var definitions = getBpmnJS().getDefinitions(),
rootElements = definitions.get('rootElements');
return !!find(rootElements, matchPattern({ id: rootElement.id }));
}
function capitalizeFirstChar(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}