chore(tests): instantiate modeler only when neccessary

This commit replaces the test helper #bootstrapBpmnJS with either

This ensures we can have different Modeler/Viewer specific bootstrap
code being called based on whether we test modeling or viewing only
features.
This commit is contained in:
Nico Rehwaldt 2014-08-05 08:17:22 +02:00
parent 198ac665f9
commit 911e991bc8
18 changed files with 154 additions and 110 deletions

View File

@ -1,7 +1,9 @@
'use strict';
var _ = require('lodash');
var BpmnJS = require('../lib/Modeler');
var Modeler = require('../lib/Modeler'),
Viewer = require('../lib/Viewer');
// enhance jasmine with test container API
require('jasmine-test-container-support').extend(jasmine);
@ -17,8 +19,53 @@ function options(opts) {
OPTIONS = opts;
}
function bootstrapBpmnJS(BpmnJS, options, locals) {
var testContainer = jasmine.getEnv().getTestContainer();
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 = _.extend({ container: testContainer }, OPTIONS || {}, _options || {});
if (_locals) {
var mockModule = {};
_.forEach(_locals, function(v, k) {
mockModule[k] = ['value', v];
});
_options.modules = [].concat(_options.modules || [], [ mockModule ]);
}
_options.modules = _.unique(_options.modules);
if (!_options.modules.length) {
_options.modules = undefined;
}
BPMN_JS = new BpmnJS(_options);
return BPMN_JS;
}
/**
* Bootstrap BpmnJS given the specified options and a number of locals (i.e. services)
* Bootstrap the Modeler given the specified options and a number of locals (i.e. services)
*
* @example
*
@ -26,7 +73,7 @@ function options(opts) {
*
* var mockEvents;
*
* beforeEach(bootstrapBpmnJS(function() {
* beforeEach(bootstrapModeler('some-xml', function() {
* mockEvents = new Events();
*
* return {
@ -36,62 +83,24 @@ function options(opts) {
*
* });
*
* @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
*/
function bootstrapBpmnJS(diagram, options, locals) {
function bootstrapModeler(diagram, options, locals) {
return function(done) {
var testContainer = jasmine.getEnv().getTestContainer();
var _diagram = diagram,
_options = options,
_locals = locals;
if (_locals === undefined && _.isFunction(_options)) {
_locals = _options;
_options = null;
}
if (_.isFunction(_options)) {
_options = _options();
}
if (_.isFunction(_locals)) {
_locals = _locals();
}
_options = _.extend({ container: testContainer }, OPTIONS || {}, _options || {});
if (_locals) {
var mockModule = {};
_.forEach(_locals, function(v, k) {
mockModule[k] = ['value', v];
});
_options.modules = [].concat(_options.modules || [], [ mockModule ]);
}
_options.modules = _.unique(_options.modules);
if (_options.modules.length == 0) {
_options.modules = undefined;
}
BPMN_JS = new BpmnJS(_options);
// bootstrap
var modeler = bootstrapBpmnJS(Modeler, options, locals);
// import diagram
BPMN_JS.importXML(_diagram, done);
modeler.importXML(diagram, done);
};
}
/**
* Injects services of an instantiated diagram into the argument.
*
* Use it in conjunction with {@link #bootstrapBpmnJS}.
* Bootstrap the Viewer given the specified options and a number of locals (i.e. services)
*
* @example
*
@ -99,7 +108,45 @@ function bootstrapBpmnJS(diagram, options, locals) {
*
* var mockEvents;
*
* beforeEach(bootstrapBpmnJS(...));
* 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
*/
function bootstrapViewer(diagram, options, locals) {
return function(done) {
// bootstrap
var viewer = bootstrapBpmnJS(Viewer, options, locals);
// import diagram
viewer.importXML(diagram, done);
};
}
/**
* 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).toBe(mockEvents);
@ -114,7 +161,7 @@ function inject(fn) {
return function() {
if (!BPMN_JS) {
throw new Error('no bootstraped bpmn-js instance, ensure you created it via #bootstrapBpmnJS');
throw new Error('no bootstraped bpmn-js instance, ensure you created it via #boostrap(Modeler|Viewer)');
}
BPMN_JS.invoke(fn);
@ -122,5 +169,6 @@ function inject(fn) {
}
module.exports.bootstrapBpmnJS = (window || global).bootstrapBpmnJS = bootstrapBpmnJS;
module.exports.bootstrapModeler = (window || global).bootstrapModeler = bootstrapModeler;
module.exports.bootstrapViewer = (window || global).bootstrapViewer = bootstrapViewer;
module.exports.inject = (window || global).inject = inject;

View File

@ -3,8 +3,6 @@
var Matchers = require('../Matchers'),
TestHelper = require('../TestHelper');
/* global bootstrapBpmnJS, inject */
var fs = require('fs');

View File

@ -3,7 +3,7 @@
var Matchers = require('../Matchers'),
TestHelper = require('../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapViewer, inject */
var fs = require('fs');

View File

@ -3,7 +3,7 @@
var Matchers = require('../../Matchers'),
TestHelper = require('../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapViewer, inject */
var fs = require('fs');
@ -16,135 +16,135 @@ describe('draw - bpmn renderer', function() {
it('should render activity markers', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/activity-markers.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render activity markers (combination)', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/activity-markers-combination.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render conditional flows', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/conditional-flow.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render conditional default flows', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/conditional-flow-default.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render NO conditional flow (gateway)', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/conditional-flow-gateways.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render conditional flow (typed task)', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/conditional-flow-typed-task.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render data objects', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/data-objects.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render events', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/events.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render events (interrupting)', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/events-interrupting.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render event subprocesses (collapsed)', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/event-subprocesses-collapsed.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render event subprocesses (expanded)', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/event-subprocesses-expanded.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render gateways', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/gateways.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render group', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/group.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render message marker', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/message-marker.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render pools', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/pools.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render pool collection marker', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/pools-with-collection-marker.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render task types', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/task-types.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render text annotations', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/text-annotation.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render flow markers', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/flow-markers.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render xor gateways blank and with X', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/xor.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render boundary events with correct z-index', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/boundary-event-z-index.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render boundary events without flowNodeRef', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/boundary-event-without-refnode.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('should render boundary event only once if referenced incorrectly via flowNodeRef (robustness)', function(done) {
var xml = fs.readFileSync(__dirname + '/../../fixtures/bpmn/draw/boundary-event-with-refnode.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
});

View File

@ -2,7 +2,7 @@
var TestHelper = require('../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapViewer, inject */
var fs = require('fs');
@ -17,7 +17,7 @@ describe('environment - mocking', function() {
var mockEvents, bootstrapCalled;
beforeEach(bootstrapBpmnJS(diagramXML, {
beforeEach(bootstrapViewer(diagramXML, {
modules: Viewer.prototype._modules
}, function() {
mockEvents = new Events();

View File

@ -3,7 +3,7 @@
var Matchers = require('../../../Matchers'),
TestHelper = require('../../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapViewer, inject */
var fs = require('fs');
@ -21,7 +21,7 @@ describe('features - context-pad', function() {
var testModules = [ contextPadModule, bpmnModule ];
beforeEach(bootstrapBpmnJS(diagramXML, { modules: testModules }));
beforeEach(bootstrapViewer(diagramXML, { modules: testModules }));
describe('bootstrap', function() {

View File

@ -3,7 +3,7 @@
var Matchers = require('../../../Matchers'),
TestHelper = require('../../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapViewer, inject */
var fs = require('fs');
@ -26,7 +26,7 @@ describe('features - label-editing', function() {
var testModules = [ labelEditingModule, bpmnModule ];
beforeEach(bootstrapBpmnJS(diagramXML, { modules: testModules }));
beforeEach(bootstrapViewer(diagramXML, { modules: testModules }));
describe('basics', function() {

View File

@ -3,7 +3,7 @@
var Matchers = require('../../../Matchers'),
TestHelper = require('../../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapViewer, inject */
var fs = require('fs');
@ -47,7 +47,7 @@ describe('direct editing - touch integration', function() {
var testModules = [ labelEditingModule, touchModule ];
beforeEach(bootstrapBpmnJS(diagramXML, { modules: testModules }));
beforeEach(bootstrapViewer(diagramXML, { modules: testModules }));
it('should work via dbltap (manual test)', function() { });
});

View File

@ -3,7 +3,7 @@
var Matchers = require('../../../Matchers'),
TestHelper = require('../../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapModeler, inject */
var _ = require('lodash');
@ -25,7 +25,7 @@ describe('features/modeling - append shape', function() {
var testModules = [ drawModule, modelingModule ];
beforeEach(bootstrapBpmnJS(diagramXML, { modules: testModules }));
beforeEach(bootstrapModeler(diagramXML, { modules: testModules }));
describe('shape handling', function() {

View File

@ -3,7 +3,7 @@
var Matchers = require('../../../Matchers'),
TestHelper = require('../../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapModeler, inject */
var fs = require('fs');
@ -20,7 +20,7 @@ describe('features - bpmn-factory', function() {
var testModules = [ modelingModule ];
beforeEach(bootstrapBpmnJS(diagramXML, { modules: testModules }));
beforeEach(bootstrapModeler(diagramXML, { modules: testModules }));
describe('create di', function() {

View File

@ -3,7 +3,7 @@
var Matchers = require('../../../Matchers'),
TestHelper = require('../../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapModeler, inject */
var _ = require('lodash');
@ -22,7 +22,7 @@ describe('features/modeling - create connection', function() {
var testModules = [ drawModule, modelingModule ];
beforeEach(bootstrapBpmnJS(diagramXML, { modules: testModules }));
beforeEach(bootstrapModeler(diagramXML, { modules: testModules }));
describe('connection handling', function() {

View File

@ -3,7 +3,7 @@
var Matchers = require('../../../Matchers'),
TestHelper = require('../../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapModeler, inject */
var _ = require('lodash');
@ -22,7 +22,7 @@ describe('features/modeling - layout connection', function() {
var testModules = [ drawModule, modelingModule ];
beforeEach(bootstrapBpmnJS(diagramXML, { modules: testModules }));
beforeEach(bootstrapModeler(diagramXML, { modules: testModules }));
describe('connection handling', function() {

View File

@ -3,7 +3,7 @@
var Matchers = require('../../../Matchers'),
TestHelper = require('../../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapModeler, inject */
var _ = require('lodash');
@ -22,7 +22,7 @@ describe('features/modeling - move shape', function() {
var testModules = [ drawModule, modelingModule ];
beforeEach(bootstrapBpmnJS(diagramXML, { modules: testModules }));
beforeEach(bootstrapModeler(diagramXML, { modules: testModules }));
describe('shape handling', function() {

View File

@ -3,7 +3,7 @@
var Matchers = require('../../../Matchers'),
TestHelper = require('../../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapViewer, inject */
var fs = require('fs');
@ -22,7 +22,7 @@ describe('features - touch', function() {
var testModules = [ touchModule, bpmnModule ];
beforeEach(bootstrapBpmnJS(diagramXML, { modules: testModules }));
beforeEach(bootstrapViewer(diagramXML, { modules: testModules }));
describe('bootstrap', function() {

View File

@ -3,7 +3,7 @@
var Matchers = require('../../../Matchers'),
TestHelper = require('../../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapViewer, inject */
var fs = require('fs');
@ -24,7 +24,7 @@ describe('features - zoomscroll', function() {
var testModules = [ zoomscrollModule, bpmnModule ];
beforeEach(bootstrapBpmnJS(diagramXML, { modules: testModules }));
beforeEach(bootstrapViewer(diagramXML, { modules: testModules }));
describe('bootstrap', function() {

View File

@ -3,8 +3,6 @@
var Matchers = require('../../Matchers'),
TestHelper = require('../../TestHelper');
/* global bootstrapBpmnJS, inject */
var fs = require('fs');

View File

@ -3,7 +3,7 @@
var Matchers = require('../../../Matchers'),
TestHelper = require('../../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapViewer, inject */
var fs = require('fs');
@ -18,7 +18,7 @@ describe('import - collapsed container', function() {
var diagramXML = fs.readFileSync('test/fixtures/bpmn/import/collapsed.bpmn', 'utf8');
beforeEach(bootstrapBpmnJS(diagramXML));
beforeEach(bootstrapViewer(diagramXML));
it('should import collapsed subProcess', inject(function(elementRegistry) {
@ -83,7 +83,7 @@ describe('import - collapsed container', function() {
var diagramXML = fs.readFileSync('test/fixtures/bpmn/import/collapsed-collaboration.bpmn', 'utf8');
beforeEach(bootstrapBpmnJS(diagramXML));
beforeEach(bootstrapViewer(diagramXML));
it('should import collapsed subProcess in pool', inject(function(elementRegistry) {

View File

@ -3,7 +3,7 @@
var Matchers = require('../../../Matchers'),
TestHelper = require('../../../TestHelper');
/* global bootstrapBpmnJS, inject */
/* global bootstrapViewer, inject */
var fs = require('fs');
@ -18,19 +18,19 @@ describe('import - labels', function() {
it('on flow nodes', function(done) {
var xml = fs.readFileSync('test/fixtures/bpmn/import/labels/embedded.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('on pools and lanes', function(done) {
var xml = fs.readFileSync('test/fixtures/bpmn/import/labels/collaboration.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('on message flows', function(done) {
var xml = fs.readFileSync('test/fixtures/bpmn/import/labels/collaboration-message-flows.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
});
@ -40,13 +40,13 @@ describe('import - labels', function() {
it('with di', function(done) {
var xml = fs.readFileSync('test/fixtures/bpmn/import/labels/external.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
it('without di', function(done) {
var xml = fs.readFileSync('test/fixtures/bpmn/import/labels/external-no-di.bpmn', 'utf8');
bootstrapBpmnJS(xml)(done);
bootstrapViewer(xml)(done);
});
});