From 3b9662527ed9a5c5e5ac196ce373441fe0d3c8cb Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Tue, 17 Jun 2014 11:49:15 +0200 Subject: [PATCH] feat(test-environment): add diagram-js like test env --- test/spec/TestHelper.js | 112 +++++++++++++++++++ test/spec/browser/environment/MockingSpec.js | 51 +++++++++ 2 files changed, 163 insertions(+) create mode 100644 test/spec/TestHelper.js create mode 100644 test/spec/browser/environment/MockingSpec.js diff --git a/test/spec/TestHelper.js b/test/spec/TestHelper.js new file mode 100644 index 00000000..0435c694 --- /dev/null +++ b/test/spec/TestHelper.js @@ -0,0 +1,112 @@ +'use strict'; + +var _ = require('lodash'); +var BpmnJS = require('../../'); + +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) { + + var _diagram = diagram, + _options = options, + _locals = locals; + + if (!_locals && _.isFunction(_options)) { + _locals = _options; + _options = null; + } + + if (_.isFunction(_options)) { + _options = _options(); + } + + if (_.isFunction(_locals)) { + _locals = _locals(); + } + + _options = _.extend({}, OPTIONS || {}, _options || {}); + + var mockModule = {}; + + _.forEach(_locals, function(v, k) { + mockModule[k] = ['value', v]; + }); + + _options.modules = _.unique([].concat(_options.modules || [], [ mockModule ])); + + 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; \ No newline at end of file diff --git a/test/spec/browser/environment/MockingSpec.js b/test/spec/browser/environment/MockingSpec.js new file mode 100644 index 00000000..088df79f --- /dev/null +++ b/test/spec/browser/environment/MockingSpec.js @@ -0,0 +1,51 @@ +'use strict'; + +var TestHelper = require('../../TestHelper'); + +/* global bootstrapBpmnJS, inject */ + +var fs = require('fs'); + +var Events = require('diagram-js/lib/core/EventBus'); + + +describe('environment/Mocking', function() { + + var diagramXML = fs.readFileSync('test/fixtures/bpmn/simple.bpmn', 'utf8'); + + var mockEvents, bootstrapCalled; + + beforeEach(bootstrapBpmnJS(diagramXML, function() { + mockEvents = new Events(); + + bootstrapCalled = true; + + return { + eventBus: mockEvents + }; + })); + + afterEach(function() { + bootstrapCalled = false; + }); + + + it('should use spy', inject(function(eventBus) { + + expect(eventBus).toEqual(mockEvents); + expect(bootstrapCalled).toBe(true); + })); + + + it('should reparse bootstrap code', inject(function(eventBus) { + + expect(bootstrapCalled).toBe(true); + })); + + + it('should inject bpmnjs', inject(function(bpmnjs) { + + expect(bpmnjs).toBeDefined(); + })); + +}); \ No newline at end of file