This commit is contained in:
hanjian
2024-08-14 15:17:51 +08:00
parent 20a221c1a2
commit b610f94b2e
3483 changed files with 650965 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
import translate from 'diagram-js/lib/i18n/translate/translate';
function collectTranslations(template, replacements) {
var log = {
type: 'translations',
msg: template
};
console.log(JSON.stringify(log));
return translate(template, replacements);
}
export default {
translate: [ 'value', collectTranslations ]
};

View File

@@ -0,0 +1,247 @@
/**
* A helper file that may be used in test cases for bpmn-js and extensions.
*
* Provides the globals
*
* * bootstrapModeler(): bootstrap a modeler instance
* * bootstrapViewer(): bootstrap a viewer instance
* * inject(function(a, b) {}): inject the bpmn-js services in the given function
*
*
* In addition it provides the utilities
*
* * insertCSS(name, css): add a CSS file to be used in test cases
*
*
* It is recommended to expose the helper through a per-project utility and
* and perform custom bootstrapping (CSS, ...) in that utility.
*
* ```
* export * from 'bpmn-js/test/helper';
*
* import {
* insertCSS
* } from 'bpmn-js/test/helper';
*
* var fs = require('fs');
*
* // insert diagram.css
* insertCSS('diagram.css', fs.readFileSync('some-css.css', 'utf8'));
* ```
*/
import {
isFunction,
forEach,
merge
} from 'min-dash';
import TestContainer from 'mocha-test-container-support';
import Modeler from '../../lib/Modeler';
import Viewer from '../../lib/Viewer';
var OPTIONS, BPMN_JS;
import translationModule from './TranslationCollector';
export function bootstrapBpmnJS(BpmnJS, diagram, options, locals) {
return function(done) {
var testContainer;
// Make sure the test container is an optional dependency and we fall back
// to an empty <div> if it does not exist.
//
// This is needed if other libraries rely on this helper for testing
// while not adding the mocha-test-container-support as a dependency.
try {
// 'this' is the current test context
testContainer = TestContainer.get(this);
} catch (e) {
testContainer = document.createElement('div');
document.body.appendChild(testContainer);
}
testContainer.classList.add('test-container');
var _options = options,
_locals = locals;
if (_locals === undefined && isFunction(_options)) {
_locals = _options;
_options = null;
}
if (isFunction(_options)) {
_options = _options();
}
if (isFunction(_locals)) {
_locals = _locals();
}
_options = merge({
container: testContainer,
canvas: {
deferUpdate: false
}
}, OPTIONS, _options);
if (_locals) {
var mockModule = {};
forEach(_locals, function(v, k) {
mockModule[k] = ['value', v];
});
_options.modules = [].concat(_options.modules || [], [ mockModule ]);
}
if (_options.modules && !_options.modules.length) {
_options.modules = undefined;
}
// used to extract translations used during tests
if (window.__env__ && window.__env__.TRANSLATIONS === 'enabled') {
_options.additionalModules = [].concat(
_options.additionalModules || [],
[ translationModule ]
);
}
// clean up old bpmn-js instance
if (BPMN_JS) {
BPMN_JS.destroy();
}
BPMN_JS = new BpmnJS(_options);
BPMN_JS.importXML(diagram, done);
};
}
/**
* Bootstrap the Modeler given the specified options and a number of locals (i.e. services)
*
* @example
*
* describe(function() {
*
* var mockEvents;
*
* beforeEach(bootstrapModeler('some-xml', function() {
* mockEvents = new Events();
*
* return {
* events: mockEvents
* };
* }));
*
* });
*
* @param {String} xml document to display
* @param {Object} (options) optional options to be passed to the diagram upon instantiation
* @param {Object|Function} locals the local overrides to be used by the diagram or a function that produces them
* @return {Function} a function to be passed to beforeEach
*/
export function bootstrapModeler(diagram, options, locals) {
return bootstrapBpmnJS(Modeler, diagram, options, locals);
}
/**
* Bootstrap the Viewer given the specified options and a number of locals (i.e. services)
*
* @example
*
* describe(function() {
*
* var mockEvents;
*
* beforeEach(bootstrapViewer('some-xml', function() {
* mockEvents = new Events();
*
* return {
* events: mockEvents
* };
* }));
*
* });
*
* @param {String} xml document to display
* @param {Object} (options) optional options to be passed to the diagram upon instantiation
* @param {Object|Function} locals the local overrides to be used by the diagram or a function that produces them
* @return {Function} a function to be passed to beforeEach
*/
export function bootstrapViewer(diagram, options, locals) {
return bootstrapBpmnJS(Viewer, diagram, options, locals);
}
/**
* Injects services of an instantiated diagram into the argument.
*
* Use it in conjunction with {@link #bootstrapModeler} or {@link #bootstrapViewer}.
*
* @example
*
* describe(function() {
*
* var mockEvents;
*
* beforeEach(bootstrapViewer(...));
*
* it('should provide mocked events', inject(function(events) {
* expect(events).to.eql(mockEvents);
* }));
*
* });
*
* @param {Function} fn the function to inject to
* @return {Function} a function that can be passed to it to carry out the injection
*/
export function inject(fn) {
return function() {
if (!BPMN_JS) {
throw new Error(
'no bootstraped bpmn-js instance, ' +
'ensure you created it via #boostrap(Modeler|Viewer)'
);
}
BPMN_JS.invoke(fn);
};
}
/**
* Returns the current active BpmnJS instance.
*
* @return {BpmnJS}
*/
export function getBpmnJS() {
return BPMN_JS;
}
export function insertCSS(name, css) {
if (document.querySelector('[data-css-file="' + name + '"]')) {
return;
}
var head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');
style.setAttribute('data-css-file', name);
style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
}

View File

@@ -0,0 +1,166 @@
import {
pick
} from 'min-dash';
var BOUNDS_ATTRS = [ 'x', 'y', 'width', 'height' ],
POSITION_ATTRS = [ 'x', 'y' ],
DIMENSION_ATTRS = [ 'width', 'height' ];
function getBounds(s) {
if ('bounds' in s) {
s = s.bounds;
}
// TLBR object
if ('top' in s) {
return {
x: s.left,
y: s.top,
width: s.right - s.left,
height: s.bottom - s.top
};
}
// { x, y, width, height } object
else {
return pick(s, BOUNDS_ATTRS);
}
}
function getDimensions(s) {
return pick(getBounds(s), DIMENSION_ATTRS);
}
function getPosition(s) {
return pick(getBounds(s), POSITION_ATTRS);
}
export default function(chai, utils) {
var Assertion = chai.Assertion;
function inspect(obj) {
return utils.inspect(obj).replace(/\n /g, '');
}
/**
* A simple bounds matcher, that verifies an element
* has the correct { x, y, width, height }.
*
* @example
*
* expect(di.label).to.have.bounds({ x: 100, y: 100, width: 10, height: 20 });
* expect(shape).to.have.bounds({ top: 100, left: 0, right: 200, bottom: 50 });
*
* @param {Bounds|TLBR} exp
*/
Assertion.addMethod('bounds', function(exp) {
var obj = this._obj;
var objectBounds = getBounds(obj),
expectedBounds = getBounds(exp);
var matches = utils.eql(objectBounds, expectedBounds);
var objectBoundsStr = inspect(objectBounds),
expectedBoundsStr = inspect(expectedBounds);
var theAssert = new Assertion(objectBounds);
// transfer flags
utils.transferFlags(this, theAssert, false);
theAssert.assert(
matches,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> bounds ' +
'to equal \n ' + expectedBoundsStr +
'\nbut got\n ' + objectBoundsStr,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> bounds ' +
'not to equal \n ' + expectedBoundsStr
);
});
/**
* A simple dimensions matcher, that verifies an element
* has the correct { width, height }.
*
* Unwraps `element.bounds` (BPMNDI) if present.
*
* @example
*
* expect(di.label).to.have.dimensions({ width: 10, height: 20 });
*
* @param {Dimensions} exp
*/
Assertion.addMethod('dimensions', function(exp) {
var obj = this._obj;
var objectDimensions = getDimensions(obj),
expectedDimensions = getDimensions(exp);
var matches = utils.eql(objectDimensions, expectedDimensions);
var objectDimensionsStr = inspect(objectDimensions),
expectedDimensionsStr = inspect(expectedDimensions);
var theAssert = new Assertion(objectDimensions);
// transfer flags
utils.transferFlags(this, theAssert, false);
theAssert.assert(
matches,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> dimensions ' +
'to equal \n ' + expectedDimensionsStr +
'\nbut got\n ' + objectDimensionsStr,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> dimensions ' +
'not to equal \n ' + expectedDimensionsStr
);
});
/**
* A simple position matcher, that verifies an element
* has the correct { x, y }.
*
* Unwraps `element.bounds` (BPMNDI) if present.
*
* @example
*
* expect(taskShape).to.have.position({ x: 100, y: 150 });
*
* @param {Point} exp
*/
Assertion.addMethod('position', function(exp) {
var obj = this._obj;
var objectPosition = getPosition(obj),
expectedPosition = getPosition(exp);
var matches = utils.eql(objectPosition, expectedPosition);
var objectPositionStr = inspect(objectPosition),
expectedPositionStr = inspect(expectedPosition);
var theAssert = new Assertion(objectPosition);
// transfer flags
utils.transferFlags(this, theAssert, false);
theAssert.assert(
matches,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> position ' +
'to equal \n ' + expectedPositionStr +
'\nbut got\n ' + objectPositionStr,
'expected <' + (obj.id ? '#' + obj.id : obj) + '> position ' +
'not to equal \n ' + expectedPositionStr
);
});
}

View File

@@ -0,0 +1,124 @@
import {
pick
} from 'min-dash';
var POSITION_ATTRS = [ 'x', 'y' ];
function extractPoints(point) {
return pick(point, POSITION_ATTRS);
}
export default function(chai, utils) {
var Assertion = chai.Assertion;
function inspect(obj) {
return utils.inspect(obj).replace(/\n /g, '');
}
/**
* A simple waypoints matcher, that verifies a connection
* consists of the correct connection points.
*
* Does not take the original docking into account.
*
* @example
*
* expect(connection).to.have.waypoints([ { x: 100, y: 100 }, { x: 0, y: 0 } ]);
*
* @param {Connection|Array<Point>} exp
*/
Assertion.addMethod('waypoints', function(exp) {
var obj = this._obj;
var strippedWaypoints = obj.waypoints.map(extractPoints),
strippedExpectedWaypoints = exp.map(extractPoints);
var matches = utils.eql(strippedWaypoints, strippedExpectedWaypoints);
var strippedWaypointsStr = inspect(strippedWaypoints),
strippedExpectedWaypointsStr = inspect(strippedExpectedWaypoints);
var theAssert = new Assertion(strippedWaypoints);
// transfer negate status
utils.transferFlags(this, theAssert, false);
theAssert.assert(
matches,
'expected <' + obj.id + '#waypoints> ' +
'to equal \n ' + strippedExpectedWaypointsStr +
'\nbut got\n ' + strippedWaypointsStr,
'expected <' + obj.id + '#waypoints> ' +
'not to equal \n ' + strippedExpectedWaypoints
);
});
/**
* A simple waypoints matcher, that verifies a connection
* has the given start docking.
*
* @example
*
* expect(connection).to.have.startDocking({ x: 100, y: 100 });
*
* @param {Point} exp
*/
Assertion.addMethod('startDocking', function(exp) {
var obj = this._obj;
var startPoint = obj.waypoints[0],
startDocking = startPoint && startPoint.original;
var matches = utils.eql(startDocking, exp);
var startDockingStr = inspect(startDocking),
expectedStartDockingStr = inspect(exp);
var theAssert = new Assertion(startDocking);
// transfer negate status
utils.transferFlags(this, theAssert, false);
theAssert.assert(
matches,
'expected <' + obj.id + '> to have startDocking ' +
expectedStartDockingStr + ' but got ' + startDockingStr
);
});
/**
* A simple waypoints matcher, that verifies a connection
* has the given start docking.
*
* @example
*
* expect(connection).to.have.endDocking({ x: 100, y: 100 });
*
* @param {Point} exp
*/
Assertion.addMethod('endDocking', function(exp) {
var obj = this._obj;
var endPoint = obj.waypoints[obj.waypoints.length - 1],
endDocking = endPoint && endPoint.original;
var matches = utils.eql(endDocking, exp);
var endDockingStr = inspect(endDocking),
expectedEndDockingStr = inspect(exp);
var theAssert = new Assertion(endDocking);
// transfer negate status
utils.transferFlags(this, theAssert, false);
theAssert.assert(
matches,
'expected <' + obj.id + '> to have endDocking ' +
expectedEndDockingStr + ' but got ' + endDockingStr
);
});
}

View File

@@ -0,0 +1,17 @@
export default function(chai, utils) {
utils.addMethod(chai.Assertion.prototype, 'jsonEqual', function(comparison, filter) {
var actual = JSON.stringify(this._obj, filter, ' ');
var expected = JSON.stringify(comparison, filter, ' ');
this.assert(
actual == expected,
'expected #{this} to json equal #{exp} but got #{act}',
'expected #{this} not to json equal #{exp}',
expected, // expected
actual, // actual
true // show diff
);
});
}

View File

@@ -0,0 +1,23 @@
import {
isString,
assign
} from 'min-dash';
/**
* Create a fake key event for testing purposes.
*
* @param {String|Number} key the key or keyCode/charCode
* @param {Object} [attrs]
*
* @return {Event}
*/
export function createKeyEvent(key, attrs) {
var event = document.createEvent('Events') || new document.defaultView.CustomEvent('keyEvent');
// init and mark as bubbles / cancelable
event.initEvent('keydown', false, true);
var keyAttrs = isString(key) ? { key: key } : { keyCode: key, which: key };
return assign(event, keyAttrs, attrs || {});
}

View File

@@ -0,0 +1,53 @@
import {
assign
} from 'min-dash';
import {
getBpmnJS
} from 'test/TestHelper';
/**
* Create an event with global coordinates
* computed based on the loaded diagrams canvas position and the
* specified canvas local coordinates.
*
* @param {Point} point of the event local the canvas (closure)
* @param {Object} data
*
* @return {Event} event, scoped to the given canvas
*/
export function createCanvasEvent(position, data) {
return getBpmnJS().invoke(function(canvas) {
var target = canvas._svg;
var clientRect = canvas._container.getBoundingClientRect();
var absolutePosition = {
x: position.x + clientRect.left,
y: position.y + clientRect.top
};
return createEvent(target, absolutePosition, data);
});
}
export function createEvent(target, position, data) {
return getBpmnJS().invoke(function(eventBus) {
data = assign({
target: target,
x: position.x,
y: position.y,
clientX: position.x,
clientY: position.y,
offsetX: position.x,
offsetY: position.y
}, data || {});
return eventBus.createEvent(data);
});
}

View File

@@ -0,0 +1,16 @@
import inherits from 'inherits';
import RuleProvider from 'diagram-js/lib/features/rules/RuleProvider';
export default function CustomRules(eventBus) {
RuleProvider.call(this, eventBus);
}
CustomRules.$inject = [ 'eventBus' ];
inherits(CustomRules, RuleProvider);
CustomRules.prototype.init = function() {
// placeholder
};

View File

@@ -0,0 +1,6 @@
import CustomRules from './CustomRules';
export default {
__init__: [ 'customRules' ],
customRules: [ 'type', CustomRules ]
};