2014-08-25 09:02:49 +00:00
|
|
|
/**
|
|
|
|
* A helper file that may be used in test cases for bpmn-js and extensions.
|
|
|
|
*
|
2018-04-02 19:01:53 +00:00
|
|
|
* Provides the globals
|
2014-08-25 09:02:49 +00:00
|
|
|
*
|
|
|
|
* * 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.
|
|
|
|
*
|
|
|
|
* ```
|
2018-04-02 19:01:53 +00:00
|
|
|
* export * from 'bpmn-js/test/helper';
|
|
|
|
*
|
|
|
|
* import {
|
|
|
|
* insertCSS
|
|
|
|
* } from 'bpmn-js/test/helper';
|
2014-08-25 09:02:49 +00:00
|
|
|
*
|
|
|
|
* var fs = require('fs');
|
|
|
|
*
|
|
|
|
* // insert diagram.css
|
2018-04-02 19:01:53 +00:00
|
|
|
* insertCSS('diagram.css', fs.readFileSync('some-css.css', 'utf8'));
|
2014-08-25 09:02:49 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
isFunction,
|
|
|
|
forEach,
|
|
|
|
merge
|
|
|
|
} from 'min-dash';
|
2014-08-25 09:02:49 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import TestContainer from 'mocha-test-container-support';
|
2015-07-15 15:22:19 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import Modeler from '../../lib/Modeler';
|
2020-11-03 12:44:40 +00:00
|
|
|
import NavigatedViewer from '../../lib/NavigatedViewer';
|
2018-04-02 19:01:53 +00:00
|
|
|
import Viewer from '../../lib/Viewer';
|
2014-08-25 09:02:49 +00:00
|
|
|
|
|
|
|
var OPTIONS, BPMN_JS;
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import translationModule from './TranslationCollector';
|
2016-05-03 14:23:31 +00:00
|
|
|
|
2020-11-03 12:44:40 +00:00
|
|
|
export var collectTranslations = window.__env__ && window.__env__.TRANSLATIONS === 'enabled';
|
|
|
|
|
|
|
|
// inject logging translation module into default modules
|
|
|
|
if (collectTranslations) {
|
|
|
|
|
|
|
|
[ Modeler, Viewer, NavigatedViewer ].forEach(function(constructor) {
|
|
|
|
constructor.prototype._modules.push(translationModule);
|
|
|
|
});
|
|
|
|
}
|
2016-05-03 14:23:31 +00:00
|
|
|
|
2018-04-05 11:01:21 +00:00
|
|
|
export function bootstrapBpmnJS(BpmnJS, diagram, options, locals) {
|
2014-08-25 09:02:49 +00:00
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
return function() {
|
2015-07-15 15:22:19 +00:00
|
|
|
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 {
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2015-07-15 15:22:19 +00:00
|
|
|
// 'this' is the current test context
|
|
|
|
testContainer = TestContainer.get(this);
|
|
|
|
} catch (e) {
|
|
|
|
testContainer = document.createElement('div');
|
|
|
|
document.body.appendChild(testContainer);
|
|
|
|
}
|
2014-08-25 09:02:49 +00:00
|
|
|
|
2015-07-15 15:22:19 +00:00
|
|
|
testContainer.classList.add('test-container');
|
2014-11-20 11:55:15 +00:00
|
|
|
|
2015-07-15 15:22:19 +00:00
|
|
|
var _options = options,
|
|
|
|
_locals = locals;
|
2014-08-25 09:02:49 +00:00
|
|
|
|
2015-07-15 15:22:19 +00:00
|
|
|
if (_locals === undefined && isFunction(_options)) {
|
|
|
|
_locals = _options;
|
|
|
|
_options = null;
|
|
|
|
}
|
2014-08-25 09:02:49 +00:00
|
|
|
|
2015-07-15 15:22:19 +00:00
|
|
|
if (isFunction(_options)) {
|
|
|
|
_options = _options();
|
|
|
|
}
|
2014-08-25 09:02:49 +00:00
|
|
|
|
2015-07-15 15:22:19 +00:00
|
|
|
if (isFunction(_locals)) {
|
|
|
|
_locals = _locals();
|
|
|
|
}
|
2014-08-25 09:02:49 +00:00
|
|
|
|
2016-03-16 22:44:18 +00:00
|
|
|
_options = merge({
|
|
|
|
container: testContainer,
|
|
|
|
canvas: {
|
|
|
|
deferUpdate: false
|
|
|
|
}
|
|
|
|
}, OPTIONS, _options);
|
2014-08-25 09:02:49 +00:00
|
|
|
|
2015-07-15 15:22:19 +00:00
|
|
|
if (_locals) {
|
|
|
|
var mockModule = {};
|
2014-08-25 09:02:49 +00:00
|
|
|
|
2015-07-15 15:22:19 +00:00
|
|
|
forEach(_locals, function(v, k) {
|
|
|
|
mockModule[k] = ['value', v];
|
|
|
|
});
|
2014-08-25 09:02:49 +00:00
|
|
|
|
2015-07-15 15:22:19 +00:00
|
|
|
_options.modules = [].concat(_options.modules || [], [ mockModule ]);
|
|
|
|
}
|
2014-08-25 09:02:49 +00:00
|
|
|
|
2018-03-22 09:22:37 +00:00
|
|
|
if (_options.modules && !_options.modules.length) {
|
2015-07-15 15:22:19 +00:00
|
|
|
_options.modules = undefined;
|
|
|
|
}
|
2014-08-25 09:02:49 +00:00
|
|
|
|
2016-05-03 14:23:31 +00:00
|
|
|
// used to extract translations used during tests
|
2020-11-03 12:44:40 +00:00
|
|
|
if (collectTranslations) {
|
2018-03-22 09:22:37 +00:00
|
|
|
_options.additionalModules = [].concat(
|
|
|
|
_options.additionalModules || [],
|
|
|
|
[ translationModule ]
|
|
|
|
);
|
2016-05-03 14:23:31 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 11:48:28 +00:00
|
|
|
clearBpmnJS();
|
|
|
|
|
|
|
|
var instance = new BpmnJS(_options);
|
2016-06-26 21:28:53 +00:00
|
|
|
|
2019-07-19 11:48:28 +00:00
|
|
|
setBpmnJS(instance);
|
2014-08-25 09:02:49 +00:00
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
return instance.importXML(diagram).then(function(result) {
|
|
|
|
return { error: null, warnings: result.warnings };
|
|
|
|
}).catch(function(err) {
|
|
|
|
return { error: err, warnings: err.warnings };
|
|
|
|
});
|
2015-07-15 15:22:19 +00:00
|
|
|
};
|
2014-08-25 09:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* };
|
|
|
|
* }));
|
|
|
|
*
|
|
|
|
* });
|
|
|
|
*
|
2020-04-03 13:51:45 +00:00
|
|
|
* @param {string} xml document to display
|
2014-08-25 09:02:49 +00:00
|
|
|
* @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
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function bootstrapModeler(diagram, options, locals) {
|
2015-07-15 15:22:19 +00:00
|
|
|
return bootstrapBpmnJS(Modeler, diagram, options, locals);
|
2014-08-25 09:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* };
|
|
|
|
* }));
|
|
|
|
*
|
|
|
|
* });
|
|
|
|
*
|
2020-04-03 13:51:45 +00:00
|
|
|
* @param {string} xml document to display
|
2014-08-25 09:02:49 +00:00
|
|
|
* @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
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function bootstrapViewer(diagram, options, locals) {
|
2015-07-15 15:22:19 +00:00
|
|
|
return bootstrapBpmnJS(Viewer, diagram, options, locals);
|
2014-08-25 09:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2015-07-15 15:22:19 +00:00
|
|
|
* expect(events).to.eql(mockEvents);
|
2014-08-25 09:02:49 +00:00
|
|
|
* }));
|
|
|
|
*
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* @param {Function} fn the function to inject to
|
|
|
|
* @return {Function} a function that can be passed to it to carry out the injection
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function inject(fn) {
|
2014-08-25 09:02:49 +00:00
|
|
|
return function() {
|
|
|
|
|
|
|
|
if (!BPMN_JS) {
|
2018-04-02 19:01:53 +00:00
|
|
|
throw new Error(
|
|
|
|
'no bootstraped bpmn-js instance, ' +
|
|
|
|
'ensure you created it via #boostrap(Modeler|Viewer)'
|
|
|
|
);
|
2014-08-25 09:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BPMN_JS.invoke(fn);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-16 07:11:04 +00:00
|
|
|
/**
|
|
|
|
* Returns the current active BpmnJS instance.
|
|
|
|
*
|
|
|
|
* @return {BpmnJS}
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function getBpmnJS() {
|
2015-04-16 07:11:04 +00:00
|
|
|
return BPMN_JS;
|
2018-04-02 19:01:53 +00:00
|
|
|
}
|
2015-04-16 07:11:04 +00:00
|
|
|
|
2019-07-19 11:48:28 +00:00
|
|
|
export function clearBpmnJS() {
|
|
|
|
|
|
|
|
// clean up old bpmn-js instance
|
|
|
|
if (BPMN_JS) {
|
|
|
|
BPMN_JS.destroy();
|
|
|
|
|
|
|
|
BPMN_JS = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
// This method always resolves.
|
|
|
|
// It helps us to do done(err) within the same block.
|
|
|
|
export function createViewer(container, viewerInstance, xml, diagramId) {
|
|
|
|
|
|
|
|
clearBpmnJS();
|
|
|
|
|
|
|
|
var viewer = new viewerInstance({ container: container });
|
|
|
|
|
|
|
|
setBpmnJS(viewer);
|
|
|
|
|
|
|
|
return viewer.importXML(xml, diagramId).then(function(result) {
|
|
|
|
return { warnings: result.warnings, viewer: viewer };
|
|
|
|
}).catch(function(err) {
|
|
|
|
return { error: err, viewer: viewer, warnings: err.warnings };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-19 11:48:28 +00:00
|
|
|
export function setBpmnJS(instance) {
|
|
|
|
BPMN_JS = instance;
|
|
|
|
}
|
2015-04-16 07:11:04 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
export function insertCSS(name, css) {
|
2014-08-25 09:02:49 +00:00
|
|
|
if (document.querySelector('[data-css-file="' + name + '"]')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var head = document.head || document.getElementsByTagName('head')[0],
|
|
|
|
style = document.createElement('style');
|
2016-06-07 06:46:45 +00:00
|
|
|
style.setAttribute('data-css-file', name);
|
2014-08-25 09:02:49 +00:00
|
|
|
|
|
|
|
style.type = 'text/css';
|
|
|
|
if (style.styleSheet) {
|
|
|
|
style.styleSheet.cssText = css;
|
|
|
|
} else {
|
|
|
|
style.appendChild(document.createTextNode(css));
|
|
|
|
}
|
|
|
|
|
|
|
|
head.appendChild(style);
|
2020-11-03 12:44:40 +00:00
|
|
|
}
|