From c3e4ad3e4088f00f2db1eba38cefdbdac291bf83 Mon Sep 17 00:00:00 2001 From: pedesen Date: Wed, 15 Jul 2015 17:22:19 +0200 Subject: [PATCH] test(project): replace jasmine with mocha Closes #204 --- .jshintrc | 1 + package.json | 9 +- test/Matchers.js | 85 ------------- test/TestHelper.js | 3 +- test/config/karma.unit.js | 5 +- test/helper/index.js | 110 ++++++++--------- test/integration/SimpleModelingSpec.js | 7 +- test/integration/model/BpmnModdleSpec.js | 8 +- test/spec/ModelerSpec.js | 23 ++-- test/spec/NavigatedViewerSpec.js | 10 +- test/spec/ViewerSpec.js | 86 ++++++------- test/spec/draw/BpmnRendererSpec.js | 18 ++- test/spec/environment/MockingSpec.js | 10 +- .../context-pad/ContextPadProviderSpec.js | 10 +- .../label-editing/LabelEditingProviderSpec.js | 28 ++--- .../label-editing/TouchIntegrationSpec.js | 5 +- .../spec/features/modeling/AppendShapeSpec.js | 112 ++++++++--------- test/spec/features/modeling/BendpointsSpec.js | 2 +- .../spec/features/modeling/BpmnFactorySpec.js | 28 ++--- .../features/modeling/CreateConnectionSpec.js | 68 +++++----- .../features/modeling/DeleteConnectionSpec.js | 12 +- .../modeling/DeleteParticipantSpec.js | 30 ++--- .../spec/features/modeling/DeleteShapeSpec.js | 6 +- test/spec/features/modeling/DropSpec.js | 28 ++--- .../features/modeling/LayoutConnectionSpec.js | 33 +++-- .../modeling/LayoutMessageFlowSpec.js | 16 +-- .../features/modeling/MoveConnectionSpec.js | 39 +++--- test/spec/features/modeling/MoveShapeSpec.js | 76 ++++++------ .../spec/features/modeling/ResizeShapeSpec.js | 26 ++-- test/spec/features/modeling/SpaceToolSpec.js | 90 +++++++------- .../features/modeling/UpdatePropertiesSpec.js | 54 ++++---- .../modeling/append/TextAnnotationSpec.js | 44 +++---- .../modeling/behavior/CreateBehaviorSpec.js | 58 ++++----- .../behavior/CreateOnFlowBehaviorSpec.js | 46 +++---- .../modeling/behavior/DropBehaviorSpec.js | 18 +-- .../modeling/behavior/RemoveBehaviorSpec.js | 30 ++--- .../features/modeling/rules/BpmnRulesSpec.js | 14 +-- .../features/palette/PaletteProviderSpec.js | 2 +- .../spec/features/popup-menu/PopupMenuSpec.js | 84 ++++++------- test/spec/features/replace/BpmnReplaceSpec.js | 116 +++++++++--------- .../replace/ReplaceOptionsChooserSpec.js | 6 +- .../spec/features/replace/ReplaceRulesSpec.js | 14 +-- .../features/snapping/BpmnSnappingSpec.js | 44 +++---- test/spec/import/ImporterSpec.js | 71 +++++------ test/spec/import/elements/AssociationSpec.js | 12 +- test/spec/import/elements/CollapsedSpec.js | 42 +++---- test/spec/import/elements/LabelSpec.js | 4 +- 47 files changed, 761 insertions(+), 882 deletions(-) delete mode 100644 test/Matchers.js diff --git a/.jshintrc b/.jshintrc index 3ba65dd5..54894bf5 100644 --- a/.jshintrc +++ b/.jshintrc @@ -4,6 +4,7 @@ "strict": true, "unused": "vars", "maxlen": 120, + "expr": true, "globals": { "describe": false, "it": false, diff --git a/package.json b/package.json index 9d3fef7b..f97857d1 100644 --- a/package.json +++ b/package.json @@ -33,25 +33,30 @@ "browserify": "^8.1.0", "browserify-derequire": "^0.9.1", "bundle-collapser": "^1.1.1", + "chai": "~2.2.0", "grunt": "^0.4.4", "grunt-contrib-copy": "^0.7.0", "grunt-contrib-jshint": "^0.7.2", "grunt-jsdoc": "^0.5.1", "grunt-karma": "^0.8.0", "grunt-release": "^0.7.0", - "jasmine-test-container-support": "^0.1.2", "jsondiffpatch": "^0.1.26", "karma": "^0.12.12", "karma-browserify": "^4.0.0", + "karma-chai": "^0.1.0", "karma-chrome-launcher": "^0.1.2", "karma-firefox-launcher": "^0.1.3", "karma-ie-launcher": "^0.1.4", - "karma-jasmine": "https://github.com/Nikku/karma-jasmine/archive/jasmine-v2.0.0-latest-1.tar.gz", + "karma-mocha": "~0.1.10", "karma-phantomjs-launcher": "^0.1.2", "karma-safari-launcher": "^0.1.1", + "karma-sinon-chai": "~0.3.0", "load-grunt-tasks": "^0.3.0", + "mocha-test-container-support": "~0.1.0", "source-map-concat": "^0.4.0", "stringify": "^3.1.0", + "sinon": "~1.14.1", + "sinon-chai": "~2.7.0", "time-grunt": "^0.3.2", "uglify-js": "^2.4.16" }, diff --git a/test/Matchers.js b/test/Matchers.js deleted file mode 100644 index 639d180b..00000000 --- a/test/Matchers.js +++ /dev/null @@ -1,85 +0,0 @@ -'use strict'; - -/* global jasmine */ - -var cloneDeep = require('lodash/lang/cloneDeep'), - isEqual = require('lodash/lang/isEqual'), - pick = require('lodash/object/pick'); - - -module.exports.addDeepEquals = function() { - var jsondiffpatch = require('jsondiffpatch'); - - var compare = jsondiffpatch.create({ - objectHash: function (obj) { - return JSON.stringify(obj); - } - }); - - function deepEquals(actual, expected) { - var actualClone = cloneDeep(actual); - var expectedClone = cloneDeep(expected); - - var result = { - pass: isEqual(actualClone, expectedClone) - }; - - var message; - - if (!result.pass) { - message = - 'Expected elements to equal but got diff\n' + - JSON.stringify(compare.diff(actualClone, expectedClone), null, ' '); - } else { - message = 'Expected elements not to equal'; - } - - result.message = message; - - return result; - } - - jasmine.addMatchers({ - toDeepEqual: function(util) { - return { - compare: deepEquals - }; - } - }); -}; - -module.exports.addBBoxMatchers = function() { - - jasmine.addMatchers({ - - toFitBBox: function(util) { - - return { - compare: function(actual, expected) { - - var actualBBox = actual.getBBox(); - - var pass = actualBBox.x >= expected.x && - actualBBox.y >= expected.y && - actualBBox.width <= expected.width && - actualBBox.x + actualBBox.width <= expected.x + expected.width && - (expected.height ? actualBBox.height <= expected.height : true) && - (expected.height ? actualBBox.y + actualBBox.height <= expected.y + expected.height : true); - - var message = ''; - - if (!pass) { - message = 'Expected element#' + actual.id + ' with bbox ' + - jasmine.pp(pick(actualBBox, ['x', 'y', 'width', 'height'])) + ' to fit ' + - jasmine.pp(expected); - } - - return { - pass: pass, - message: message - }; - } - }; - } - }); -}; \ No newline at end of file diff --git a/test/TestHelper.js b/test/TestHelper.js index 46008ca4..55f2f3f9 100644 --- a/test/TestHelper.js +++ b/test/TestHelper.js @@ -3,8 +3,9 @@ var TestHelper = module.exports = require('./helper'); TestHelper.insertCSS('diagram-js.css', require('diagram-js/assets/diagram-js.css')); + TestHelper.insertCSS('bpmn-embedded.css', require('../assets/bpmn-font/css/bpmn-embedded.css')); TestHelper.insertCSS('diagram-js-testing.css', '.test-container .result { height: 500px; }' + '.test-container > div' -); \ No newline at end of file +); diff --git a/test/config/karma.unit.js b/test/config/karma.unit.js index 068e7430..8cf80922 100644 --- a/test/config/karma.unit.js +++ b/test/config/karma.unit.js @@ -5,7 +5,10 @@ module.exports = function(karma) { basePath: '../../', - frameworks: [ 'browserify', 'jasmine' ], + frameworks: [ 'browserify', + 'mocha', + 'chai', + 'sinon-chai'], files: [ 'test/spec/**/*Spec.js', diff --git a/test/helper/index.js b/test/helper/index.js index a12a84f0..3e70f119 100644 --- a/test/helper/index.js +++ b/test/helper/index.js @@ -33,69 +33,71 @@ var unique = require('lodash/array/unique'), assign = require('lodash/object/assign'), forEach = require('lodash/collection/forEach'); +var TestContainer = require('mocha-test-container-support'); + var Modeler = require('../../lib/Modeler'), Viewer = require('../../lib/Viewer'); -try { - // enhance jasmine with test container API - require('jasmine-test-container-support').extend(jasmine); -} catch (e) { - // no test container :-( -} - var OPTIONS, BPMN_JS; +function bootstrapBpmnJS(BpmnJS, diagram, options, locals) { -function bootstrapBpmnJS(BpmnJS, options, locals) { + return function(done) { + var testContainer; - var testContainer; + // Make sure the test container is an optional dependency and we fall back + // to an empty
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); + } - try { - testContainer = jasmine.getEnv().getTestContainer(); - } catch (e) { - testContainer = document.createElement('div'); - document.body.appendChild(testContainer); - } + testContainer.classList.add('test-container'); - testContainer.classList.add('test-container'); + var _options = options, + _locals = locals; - var _options = options, - _locals = locals; + if (_locals === undefined && isFunction(_options)) { + _locals = _options; + _options = null; + } - if (_locals === undefined && isFunction(_options)) { - _locals = _options; - _options = null; - } + if (isFunction(_options)) { + _options = _options(); + } - if (isFunction(_options)) { - _options = _options(); - } + if (isFunction(_locals)) { + _locals = _locals(); + } - if (isFunction(_locals)) { - _locals = _locals(); - } + _options = assign({ container: testContainer }, OPTIONS || {}, _options || {}); - _options = assign({ container: testContainer, width: '100%', height: '100%' }, OPTIONS || {}, _options || {}); + if (_locals) { + var mockModule = {}; - if (_locals) { - var mockModule = {}; + forEach(_locals, function(v, k) { + mockModule[k] = ['value', v]; + }); - forEach(_locals, function(v, k) { - mockModule[k] = ['value', v]; - }); + _options.modules = [].concat(_options.modules || [], [ mockModule ]); + } - _options.modules = [].concat(_options.modules || [], [ mockModule ]); - } + _options.modules = unique(_options.modules); - _options.modules = unique(_options.modules); + if (!_options.modules.length) { + _options.modules = undefined; + } - if (!_options.modules.length) { - _options.modules = undefined; - } + BPMN_JS = new BpmnJS(_options); - BPMN_JS = new BpmnJS(_options); - - return BPMN_JS; + BPMN_JS.importXML(diagram, done); + }; } @@ -124,14 +126,7 @@ function bootstrapBpmnJS(BpmnJS, options, locals) { * @return {Function} a function to be passed to beforeEach */ function bootstrapModeler(diagram, options, locals) { - - return function(done) { - // bootstrap - var modeler = bootstrapBpmnJS(Modeler, options, locals); - - // import diagram - modeler.importXML(diagram, done); - }; + return bootstrapBpmnJS(Modeler, diagram, options, locals); } /** @@ -159,14 +154,7 @@ function bootstrapModeler(diagram, options, locals) { * @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); - }; + return bootstrapBpmnJS(Viewer, diagram, options, locals); } @@ -184,7 +172,7 @@ function bootstrapViewer(diagram, options, locals) { * beforeEach(bootstrapViewer(...)); * * it('should provide mocked events', inject(function(events) { - * expect(events).toBe(mockEvents); + * expect(events).to.eql(mockEvents); * })); * * }); @@ -238,4 +226,4 @@ function insertCSS(name, css) { head.appendChild(style); } -module.exports.insertCSS = insertCSS; \ No newline at end of file +module.exports.insertCSS = insertCSS; diff --git a/test/integration/SimpleModelingSpec.js b/test/integration/SimpleModelingSpec.js index 8cb54428..05dfafac 100644 --- a/test/integration/SimpleModelingSpec.js +++ b/test/integration/SimpleModelingSpec.js @@ -1,9 +1,8 @@ 'use strict'; -var TestHelper = require('../TestHelper'); - var Modeler = require('../../lib/Modeler'); +var TestContainer = require('mocha-test-container-support'); describe('scenario - simple modeling', function() { @@ -11,7 +10,7 @@ describe('scenario - simple modeling', function() { var container; beforeEach(function() { - container = jasmine.getEnv().getTestContainer(); + container = TestContainer.get(this); }); @@ -27,4 +26,4 @@ describe('scenario - simple modeling', function() { }); }); -}); \ No newline at end of file +}); diff --git a/test/integration/model/BpmnModdleSpec.js b/test/integration/model/BpmnModdleSpec.js index f20f9c22..1a714d41 100644 --- a/test/integration/model/BpmnModdleSpec.js +++ b/test/integration/model/BpmnModdleSpec.js @@ -31,11 +31,11 @@ describe('bpmn-moddle', function() { } // then - expect(definitions.id).toBe('simple'); - expect(definitions.targetNamespace).toBe('http://bpmn.io/schema/bpmn'); + expect(definitions.id).to.equal('simple'); + expect(definitions.targetNamespace).to.equal('http://bpmn.io/schema/bpmn'); - expect(definitions.rootElements.length).toBe(1); - expect(definitions.rootElements[0].id).toBe('Process_1'); + expect(definitions.rootElements.length).to.equal(1); + expect(definitions.rootElements[0].id).to.equal('Process_1'); done(); }); diff --git a/test/spec/ModelerSpec.js b/test/spec/ModelerSpec.js index 20de89e8..25147a74 100644 --- a/test/spec/ModelerSpec.js +++ b/test/spec/ModelerSpec.js @@ -1,16 +1,15 @@ 'use strict'; -var TestHelper = require('../TestHelper'); - var Modeler = require('../../lib/Modeler'); +var TestContainer = require('mocha-test-container-support'); describe('Modeler', function() { var container; beforeEach(function() { - container = jasmine.getEnv().getTestContainer(); + container = TestContainer.get(this); }); @@ -61,7 +60,7 @@ describe('Modeler', function() { } // then - expect(warnings.length).toBe(0); + expect(warnings.length).to.equal(0); done(); }); @@ -80,7 +79,7 @@ describe('Modeler', function() { modeler.importXML(xml, function(err, warnings) { - expect(modeler.container.parentNode).toBe(document.body); + expect(modeler.container.parentNode).to.equal(document.body); done(err, warnings); }); @@ -102,8 +101,8 @@ describe('Modeler', function() { elementRegistry = viewer.get('elementRegistry'); // assume - expect(overlays).toBeDefined(); - expect(elementRegistry).toBeDefined(); + expect(overlays).to.be.defined; + expect(elementRegistry).to.be.defined; // when @@ -124,8 +123,8 @@ describe('Modeler', function() { }); // then - expect(overlays.get({ element: 'SubProcess_1', type: 'badge' }).length).toBe(1); - expect(overlays.get({ element: 'StartEvent_1', type: 'badge' }).length).toBe(1); + expect(overlays.get({ element: 'SubProcess_1', type: 'badge' }).length).to.equal(1); + expect(overlays.get({ element: 'StartEvent_1', type: 'badge' }).length).to.equal(1); done(err); }); @@ -152,7 +151,7 @@ describe('Modeler', function() { createEvent = Events.scopedCreate(viewer.get('canvas')); // assume - expect(bendpointMove).toBeDefined(); + expect(bendpointMove).to.be.defined; // when bendpointMove.start(createEvent({ x: 0, y: 0 }), elementRegistry.get('SequenceFlow_1'), 1); @@ -174,7 +173,7 @@ describe('Modeler', function() { modeler.importXML(xml, function(err) { - expect(err).toBeDefined(); + expect(err).to.be.defined; done(); }); @@ -195,7 +194,7 @@ describe('Modeler', function() { createModeler(xml, function(err, warnings, modeler) { - expect(modeler.get('bpmnjs')).toBe(modeler); + expect(modeler.get('bpmnjs')).to.equal(modeler); done(err); }); diff --git a/test/spec/NavigatedViewerSpec.js b/test/spec/NavigatedViewerSpec.js index 3bd23c71..e4106125 100644 --- a/test/spec/NavigatedViewerSpec.js +++ b/test/spec/NavigatedViewerSpec.js @@ -10,10 +10,12 @@ describe('NavigatedViewer', function() { var container; beforeEach(function() { - container = jasmine.getEnv().getTestContainer(); + container = document.createElement('div'); + document.body.appendChild(container); }); + function createViewer(xml, done) { var viewer = new NavigatedViewer({ container: container }); @@ -36,7 +38,7 @@ describe('NavigatedViewer', function() { it('should include zoomScroll', function(done) { createViewer(xml, function(err, warnings, viewer) { - expect(viewer.get('zoomScroll')).toBeDefined(); + expect(viewer.get('zoomScroll')).to.be.defined; done(err); }); @@ -45,7 +47,7 @@ describe('NavigatedViewer', function() { it('should include moveCanvas', function(done) { createViewer(xml, function(err, warnings, viewer) { - expect(viewer.get('moveCanvas')).toBeDefined(); + expect(viewer.get('moveCanvas')).to.be.defined; done(err); }); @@ -53,4 +55,4 @@ describe('NavigatedViewer', function() { }); -}); \ No newline at end of file +}); diff --git a/test/spec/ViewerSpec.js b/test/spec/ViewerSpec.js index 3be58b59..300c78f6 100644 --- a/test/spec/ViewerSpec.js +++ b/test/spec/ViewerSpec.js @@ -1,7 +1,6 @@ 'use strict'; -var TestHelper = require('../TestHelper'); - +var TestContainer = require('mocha-test-container-support'); var Viewer = require('../../lib/Viewer'); @@ -11,7 +10,7 @@ describe('Viewer', function() { var container; beforeEach(function() { - container = jasmine.getEnv().getTestContainer(); + container = TestContainer.get(this); }); @@ -52,8 +51,8 @@ describe('Viewer', function() { viewer.importXML(xml, function(err, warnings) { // then - expect(err).toBeFalsy(); - expect(warnings.length).toBe(0); + expect(err).to.not.be.ok; + expect(warnings.length).to.equal(0); done(); }); @@ -72,7 +71,7 @@ describe('Viewer', function() { viewer.importXML(xml, function(err, warnings) { - expect(viewer.container.parentNode).toBe(document.body); + expect(viewer.container.parentNode).to.equal(document.body); done(err, warnings); }); @@ -108,7 +107,7 @@ describe('Viewer', function() { viewer.importXML(xml, function(err) { // then - expect(events).toEqual([ + expect(events).to.eql([ 'import.start', 'import.success' ]); @@ -133,8 +132,8 @@ describe('Viewer', function() { elementRegistry = viewer.get('elementRegistry'); // then - expect(overlays).toBeDefined(); - expect(elementRegistry).toBeDefined(); + expect(overlays).to.be.defined; + expect(elementRegistry).to.be.defined; // when overlays.add('SubProcess_1', { @@ -146,7 +145,7 @@ describe('Viewer', function() { }); // then - expect(overlays.get({ element: 'SubProcess_1' }).length).toBe(1); + expect(overlays.get({ element: 'SubProcess_1' }).length).to.equal(1); done(err); }); @@ -161,18 +160,18 @@ describe('Viewer', function() { function expectMessage(e, expectedMessage) { - expect(e).toBeDefined(); + expect(e).to.be.defined; if (expectedMessage instanceof RegExp) { - expect(e.message).toMatch(expectedMessage); + expect(e.message).to.match(expectedMessage); } else { - expect(e.message).toEqual(expectedMessage); + expect(e.message).to.equal(expectedMessage); } } function expectWarnings(warnings, expected) { - expect(warnings.length).toBe(expected.length); + expect(warnings.length).to.equal(expected.length); warnings.forEach(function(w, idx) { expectMessage(w, expected[idx]); @@ -186,7 +185,7 @@ describe('Viewer', function() { createViewer(xml, function(err) { - expect(err).toBeTruthy(); + expect(err).to.be.ok; expectMessage(err, /Text data outside of root node./); @@ -203,7 +202,7 @@ describe('Viewer', function() { createViewer(xml, function(err, warnings) { // then - expect(err).toBeFalsy(); + expect(err).to.not.be.ok; expectWarnings(warnings, [ 'unresolved reference ', @@ -218,7 +217,8 @@ describe('Viewer', function() { }); - it('should handle invalid namespaced element', function(done) { + it + ('should handle invalid namespaced element', function(done) { var xml = require('../fixtures/bpmn/error/categoryValue.bpmn'); @@ -226,7 +226,7 @@ describe('Viewer', function() { createViewer(xml, function(err, warnings) { // then - expect(err).toBeFalsy(); + expect(err).to.not.be.ok; expectWarnings(warnings, [ /unparsable content detected/, @@ -246,7 +246,7 @@ describe('Viewer', function() { createViewer(xml, function(err, warnings) { // then - expect(err).toBeFalsy(); + expect(err).to.not.be.ok; expectWarnings(warnings, [ /unparsable content detected/, @@ -271,7 +271,7 @@ describe('Viewer', function() { createViewer(xml, function(err, warnings, viewer) { - expect(viewer.get('bpmnjs')).toBe(viewer); + expect(viewer.get('bpmnjs')).to.equal(viewer); done(err); }); @@ -290,15 +290,15 @@ describe('Viewer', function() { var expectedStart = ''; var expectedEnd = ''; - expect(svg.indexOf(expectedStart)).toEqual(0); - expect(svg.indexOf(expectedEnd)).toEqual(svg.length - expectedEnd.length); + expect(svg.indexOf(expectedStart)).to.equal(0); + expect(svg.indexOf(expectedEnd)).to.equal(svg.length - expectedEnd.length); // ensure correct rendering of SVG contents - expect(svg.indexOf('undefined')).toBe(-1); + expect(svg.indexOf('undefined')).to.equal(-1); // expect header to be written only once - expect(svg.indexOf('')).toBe(-1); - expect(svg.indexOf('')).to.equal(-1); + expect(svg.indexOf('