2018-04-23 06:52:35 +00:00
|
|
|
import BpmnTreeWalker from 'lib/import/BpmnTreeWalker';
|
|
|
|
|
|
|
|
import BpmnModdle from 'bpmn-moddle';
|
|
|
|
|
|
|
|
import { find } from 'min-dash';
|
|
|
|
|
|
|
|
import simpleXML from 'test/fixtures/bpmn/simple.bpmn';
|
|
|
|
|
|
|
|
|
|
|
|
describe('import - BpmnTreeWalker', function() {
|
|
|
|
|
|
|
|
it('should expose functions', function() {
|
|
|
|
|
|
|
|
// when
|
2018-07-13 13:48:34 +00:00
|
|
|
var walker = createWalker();
|
2018-04-23 06:52:35 +00:00
|
|
|
|
|
|
|
// then
|
|
|
|
expect(walker.handleDeferred).to.exist;
|
|
|
|
expect(walker.handleDefinitions).to.exist;
|
|
|
|
expect(walker.handleSubProcess).to.exist;
|
|
|
|
expect(walker.registerDi).to.exist;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
it('should walk bpmn:Definitions', function() {
|
2018-04-23 06:52:35 +00:00
|
|
|
|
|
|
|
// given
|
2018-07-13 13:48:34 +00:00
|
|
|
var elementSpy = sinon.spy(),
|
|
|
|
rootSpy = sinon.spy(),
|
|
|
|
errorSpy = sinon.spy();
|
2018-04-23 06:52:35 +00:00
|
|
|
|
2018-07-13 13:48:34 +00:00
|
|
|
var walker = createWalker({
|
2018-04-23 06:52:35 +00:00
|
|
|
element: elementSpy,
|
|
|
|
root: rootSpy,
|
|
|
|
error: errorSpy
|
|
|
|
});
|
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
return createModdle(simpleXML).then(function(result) {
|
2020-04-16 05:49:59 +00:00
|
|
|
|
|
|
|
var definitions = result.rootElement;
|
2018-04-23 06:52:35 +00:00
|
|
|
|
|
|
|
// when
|
|
|
|
walker.handleDefinitions(definitions);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(elementSpy.callCount).to.equal(8);
|
|
|
|
expect(rootSpy.calledOnce).to.be.true;
|
|
|
|
expect(errorSpy.notCalled).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
it('should walk bpmn:SubProcess', function() {
|
2018-04-23 06:52:35 +00:00
|
|
|
|
|
|
|
// given
|
2018-07-13 13:48:34 +00:00
|
|
|
var elementSpy = sinon.spy(),
|
|
|
|
rootSpy = sinon.spy(),
|
|
|
|
errorSpy = sinon.spy();
|
2018-04-23 06:52:35 +00:00
|
|
|
|
2018-07-13 13:48:34 +00:00
|
|
|
var walker = createWalker({
|
2018-04-23 06:52:35 +00:00
|
|
|
element: elementSpy,
|
|
|
|
root: rootSpy,
|
|
|
|
error: errorSpy
|
|
|
|
});
|
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
return createModdle(simpleXML).then(function(result) {
|
2020-04-16 05:49:59 +00:00
|
|
|
|
|
|
|
var definitions = result.rootElement;
|
|
|
|
|
2018-07-13 13:48:34 +00:00
|
|
|
var subProcess = findElementWithId(definitions, 'SubProcess_1');
|
2018-04-23 06:52:35 +00:00
|
|
|
|
2018-07-13 13:48:34 +00:00
|
|
|
var plane = definitions.diagrams[0].plane,
|
|
|
|
planeElements = plane.planeElement;
|
2018-04-23 06:52:35 +00:00
|
|
|
|
|
|
|
// register DI
|
|
|
|
planeElements.forEach(walker.registerDi);
|
|
|
|
|
|
|
|
// when
|
|
|
|
walker.handleSubProcess(subProcess);
|
|
|
|
|
|
|
|
walker.handleDeferred();
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(elementSpy.callCount).to.equal(3);
|
|
|
|
expect(rootSpy.notCalled).to.be.true;
|
|
|
|
expect(errorSpy.notCalled).to.be.true;
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
it('should error', function() {
|
2018-04-23 06:52:35 +00:00
|
|
|
|
|
|
|
// given
|
2018-07-13 13:48:34 +00:00
|
|
|
var elementSpy = sinon.spy(),
|
|
|
|
rootSpy = sinon.spy(),
|
|
|
|
errorSpy = sinon.spy();
|
2018-04-23 06:52:35 +00:00
|
|
|
|
2018-07-13 13:48:34 +00:00
|
|
|
var walker = createWalker({
|
2018-04-23 06:52:35 +00:00
|
|
|
element: elementSpy,
|
|
|
|
root: rootSpy,
|
|
|
|
error: errorSpy
|
|
|
|
});
|
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
return createModdle(simpleXML).then(function(result) {
|
2020-04-16 05:49:59 +00:00
|
|
|
|
|
|
|
var definitions = result.rootElement;
|
2018-04-23 06:52:35 +00:00
|
|
|
|
2018-07-13 13:48:34 +00:00
|
|
|
var element = findElementWithId(definitions, 'SubProcess_1');
|
2018-04-23 06:52:35 +00:00
|
|
|
|
|
|
|
// will error
|
|
|
|
element.di = 'DI';
|
|
|
|
|
|
|
|
// when
|
|
|
|
walker.handleDefinitions(definitions);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(elementSpy.callCount).to.equal(8);
|
|
|
|
expect(rootSpy.calledOnce).to.be.true;
|
|
|
|
expect(errorSpy.calledOnce).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// helpers //////////
|
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
function createModdle(xml) {
|
2018-07-13 13:48:34 +00:00
|
|
|
var moddle = new BpmnModdle();
|
2018-04-23 06:52:35 +00:00
|
|
|
|
2020-04-16 05:49:59 +00:00
|
|
|
return moddle.fromXML(xml, 'bpmn:Definitions');
|
2018-04-23 06:52:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 13:48:34 +00:00
|
|
|
function createWalker(listeners) {
|
|
|
|
|
|
|
|
listeners = listeners || {};
|
|
|
|
|
|
|
|
var visitor = {
|
|
|
|
element: function(element, parent) {
|
2018-04-23 06:52:35 +00:00
|
|
|
listeners.element && listeners.element(element, parent);
|
|
|
|
},
|
2018-07-13 13:48:34 +00:00
|
|
|
root: function(root) {
|
2018-04-23 06:52:35 +00:00
|
|
|
listeners.root && listeners.root(root);
|
|
|
|
},
|
2018-07-13 13:48:34 +00:00
|
|
|
error: function(message, context) {
|
2018-04-23 06:52:35 +00:00
|
|
|
listeners.error && listeners.error(message, context);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return new BpmnTreeWalker(visitor, function() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
function findElementWithId(definitions, id) {
|
|
|
|
|
|
|
|
function findElement(element) {
|
|
|
|
if (element.id === id) {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element.flowElements) {
|
2018-07-13 13:48:34 +00:00
|
|
|
return find(element.flowElements, function(flowElement) {
|
|
|
|
var foundElement = findElement(flowElement);
|
2018-04-23 06:52:35 +00:00
|
|
|
|
|
|
|
return foundElement && foundElement.id === id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 13:48:34 +00:00
|
|
|
return definitions.rootElements.reduce(function(foundElement, rootElement) {
|
2018-04-23 06:52:35 +00:00
|
|
|
if (rootElement.id === id) {
|
|
|
|
return rootElement;
|
|
|
|
} else {
|
|
|
|
return findElement(rootElement) || foundElement;
|
|
|
|
}
|
|
|
|
}, null);
|
2020-04-16 05:49:59 +00:00
|
|
|
}
|