2014-06-17 09:49:15 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('lodash');
|
2014-08-01 05:16:59 +00:00
|
|
|
var BpmnJS = require('../');
|
2014-06-17 09:49:15 +00:00
|
|
|
|
2014-06-18 09:45:30 +00:00
|
|
|
// enhance jasmine with test container API
|
|
|
|
require('jasmine-test-container-support').extend(jasmine);
|
|
|
|
|
|
|
|
|
2014-06-17 09:49:15 +00:00
|
|
|
var OPTIONS, BPMN_JS;
|
|
|
|
|
|
|
|
function options(opts) {
|
|
|
|
if (_.isFunction(opts)) {
|
|
|
|
opts = opts();
|
|
|
|
}
|
|
|
|
|
|
|
|
OPTIONS = opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bootstrap BpmnJS given the specified options and a number of locals (i.e. services)
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* describe(function() {
|
|
|
|
*
|
|
|
|
* var mockEvents;
|
|
|
|
*
|
|
|
|
* beforeEach(bootstrapBpmnJS(function() {
|
|
|
|
* mockEvents = new Events();
|
|
|
|
*
|
|
|
|
* return {
|
|
|
|
* events: mockEvents
|
|
|
|
* };
|
|
|
|
* }));
|
|
|
|
*
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
function bootstrapBpmnJS(diagram, options, locals) {
|
|
|
|
|
|
|
|
return function(done) {
|
|
|
|
|
2014-06-18 09:45:30 +00:00
|
|
|
var testContainer = jasmine.getEnv().getTestContainer();
|
|
|
|
|
2014-06-17 09:49:15 +00:00
|
|
|
var _diagram = diagram,
|
|
|
|
_options = options,
|
|
|
|
_locals = locals;
|
|
|
|
|
2014-06-18 09:45:30 +00:00
|
|
|
if (_locals === undefined && _.isFunction(_options)) {
|
2014-06-17 09:49:15 +00:00
|
|
|
_locals = _options;
|
|
|
|
_options = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isFunction(_options)) {
|
|
|
|
_options = _options();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isFunction(_locals)) {
|
|
|
|
_locals = _locals();
|
|
|
|
}
|
|
|
|
|
2014-06-18 09:45:30 +00:00
|
|
|
_options = _.extend({ container: testContainer }, OPTIONS || {}, _options || {});
|
2014-06-17 09:49:15 +00:00
|
|
|
|
2014-06-18 09:45:30 +00:00
|
|
|
if (_locals) {
|
|
|
|
var mockModule = {};
|
2014-06-17 09:49:15 +00:00
|
|
|
|
2014-06-18 09:45:30 +00:00
|
|
|
_.forEach(_locals, function(v, k) {
|
|
|
|
mockModule[k] = ['value', v];
|
|
|
|
});
|
2014-06-17 09:49:15 +00:00
|
|
|
|
2014-06-18 09:45:30 +00:00
|
|
|
_options.modules = [].concat(_options.modules || [], [ mockModule ]);
|
|
|
|
}
|
|
|
|
|
|
|
|
_options.modules = _.unique(_options.modules);
|
|
|
|
|
|
|
|
if (_options.modules.length == 0) {
|
|
|
|
_options.modules = undefined;
|
|
|
|
}
|
2014-06-17 09:49:15 +00:00
|
|
|
|
|
|
|
BPMN_JS = new BpmnJS(_options);
|
|
|
|
|
|
|
|
// import diagram
|
|
|
|
BPMN_JS.importXML(_diagram, done);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Injects services of an instantiated diagram into the argument.
|
|
|
|
*
|
|
|
|
* Use it in conjunction with {@link #bootstrapBpmnJS}.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* describe(function() {
|
|
|
|
*
|
|
|
|
* var mockEvents;
|
|
|
|
*
|
|
|
|
* beforeEach(bootstrapBpmnJS(...));
|
|
|
|
*
|
|
|
|
* it('should provide mocked events', inject(function(events) {
|
|
|
|
* expect(events).toBe(mockEvents);
|
|
|
|
* }));
|
|
|
|
*
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* @param {Function} fn the function to inject to
|
|
|
|
* @return {Function} a function that can be passed to it to carry out the injection
|
|
|
|
*/
|
|
|
|
function inject(fn) {
|
|
|
|
return function() {
|
|
|
|
|
|
|
|
if (!BPMN_JS) {
|
|
|
|
throw new Error('no bootstraped bpmn-js instance, ensure you created it via #bootstrapBpmnJS');
|
|
|
|
}
|
|
|
|
|
|
|
|
BPMN_JS.invoke(fn);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.bootstrapBpmnJS = (window || global).bootstrapBpmnJS = bootstrapBpmnJS;
|
|
|
|
module.exports.inject = (window || global).inject = inject;
|