bpmn-js/test/spec/features/context-pad/ContextPadProviderSpec.js
2016-01-06 16:37:17 +01:00

225 lines
5.4 KiB
JavaScript

'use strict';
var TestHelper = require('../../../TestHelper');
var TestContainer = require('mocha-test-container-support');
var domQuery = require('min-dom/lib/query');
/* global bootstrapViewer, inject */
var contextPadModule = require('../../../../lib/features/context-pad'),
coreModule = require('../../../../lib/core'),
modelingModule = require('../../../../lib/features/modeling'),
popupModule = require('diagram-js/lib/features/popup-menu'),
replaceMenuProvider = require('../../../../lib/features/popup-menu'),
replaceModule = require('diagram-js/lib/features/replace'),
customRulesModule = require('../../../util/custom-rules');
describe('features - context-pad', function() {
var diagramXML = require('../../../fixtures/bpmn/simple.bpmn');
var testModules = [ contextPadModule, coreModule, modelingModule, popupModule,
replaceModule, customRulesModule, replaceMenuProvider ];
beforeEach(bootstrapViewer(diagramXML, { modules: testModules }));
describe('bootstrap', function() {
it('should bootstrap', inject(function(contextPadProvider, replaceMenuProvider) {
expect(contextPadProvider).to.exist;
}));
});
describe('remove action rules', function () {
var deleteAction;
beforeEach(inject(function (contextPad) {
deleteAction = function(element) {
return padEntry(contextPad.getPad(element).html, 'delete');
};
}));
it('should add delete action by default', inject(function (elementRegistry, contextPad) {
// given
var element = elementRegistry.get('StartEvent_1');
// when
contextPad.open(element);
// then
expect(deleteAction(element)).to.exist;
}));
it('should include delete action when rule returns true',
inject(function (elementRegistry, contextPad, customRules) {
// given
customRules.addRule('elements.delete', function() {
return true;
});
var element = elementRegistry.get('StartEvent_1');
// when
contextPad.open(element);
// then
expect(deleteAction(element)).to.exist;
}));
it('should NOT include delete action when rule returns false',
inject(function(elementRegistry, contextPad, customRules) {
// given
customRules.addRule('elements.delete', function() {
return false;
});
var element = elementRegistry.get('StartEvent_1');
// when
contextPad.open(element);
// then
expect(deleteAction(element)).to.not.exist;
}));
it('should call rules with [ element ]', inject(function(elementRegistry, contextPad, customRules) {
// given
var element = elementRegistry.get('StartEvent_1');
customRules.addRule('elements.delete', function(context) {
// element array is actually passed
expect(context.elements).to.eql([ element ]);
return true;
});
// then
expect(function() {
contextPad.open(element);
}).not.to.throw;
}));
it('should include delete action when [ element ] is returned from rule',
inject(function(elementRegistry, contextPad, customRules) {
// given
customRules.addRule('elements.delete', function(context) {
return context.elements;
});
var element = elementRegistry.get('StartEvent_1');
// when
contextPad.open(element);
// then
expect(deleteAction(element)).to.exist;
}));
it('should NOT include delete action when [ ] is returned from rule',
inject(function(elementRegistry, contextPad, customRules) {
// given
customRules.addRule('elements.delete', function() {
return [];
});
var element = elementRegistry.get('StartEvent_1');
// when
contextPad.open(element);
// then
expect(deleteAction(element)).to.not.exist;
}));
});
describe('replace', function() {
var container;
beforeEach(function() {
container = TestContainer.get(this);
});
it('should show popup menu in the correct position', inject(function(elementRegistry, contextPad) {
// given
var element = elementRegistry.get('StartEvent_1'),
padding = 5,
replaceMenuRect,
padMenuRect;
contextPad.open(element);
padMenuRect = contextPad.getPad(element).html.getBoundingClientRect();
// mock event
var event = {
target: padEntry(container, 'replace'),
preventDefault: function(){}
};
// when
contextPad.trigger('click', event);
replaceMenuRect = domQuery('.bpmn-replace', container).getBoundingClientRect();
// then
expect(replaceMenuRect.left).to.be.at.most(padMenuRect.left);
expect(replaceMenuRect.top).to.be.at.most(padMenuRect.bottom + padding);
}));
it('should not inclide control if replacement is disallowed',
inject(function(elementRegistry, contextPad, customRules) {
// given
var element = elementRegistry.get('StartEvent_1'),
padding = 5,
replaceMenuRect,
padMenuRect;
// disallow replacement
customRules.addRule('shape.replace', function() {
return false;
});
// when
contextPad.open(element);
var padNode = contextPad.getPad(element).html;
// then
expect(padEntry(padNode, 'replace')).not.to.exist;
}));
});
});
function padEntry(element, name) {
return domQuery('[data-action="' + name + '"]', element);
}