'use strict'; var TestContainer = require('mocha-test-container-support'); var Diagram = require('diagram-js/lib/Diagram'); var Viewer = require('lib/Viewer'); var inherits = require('inherits'); describe('Viewer', function() { var container; beforeEach(function() { container = TestContainer.get(this); }); function createViewer(xml, done) { var viewer = new Viewer({ container: container }); viewer.importXML(xml, function(err, warnings) { done(err, warnings, viewer); }); } it('should import simple process', function(done) { var xml = require('../fixtures/bpmn/simple.bpmn'); // when createViewer(xml, function(err, warnings, viewer) { // then expect(err).not.to.exist; expect(warnings).to.be.empty; var definitions = viewer.getDefinitions(); expect(definitions).to.exist; expect(definitions).to.eql(viewer._definitions); done(); }); }); it('should re-import simple process', function(done) { var xml = require('../fixtures/bpmn/simple.bpmn'); // given createViewer(xml, function(err, warnings, viewer) { // when // mimic re-import of same diagram viewer.importXML(xml, function(err, warnings) { // then expect(warnings.length).to.equal(0); done(); }); }); }); it('should be instance of Diagram', function() { // when var viewer = new Viewer({ container: container }); // then expect(viewer).to.be.instanceof(Diagram); }); describe('overlay support', function() { it('should allow to add overlays', function(done) { var xml = require('../fixtures/bpmn/simple.bpmn'); createViewer(xml, function(err, warnings, viewer) { // when var overlays = viewer.get('overlays'), elementRegistry = viewer.get('elementRegistry'); // then expect(overlays).to.exist; expect(elementRegistry).to.exist; // when overlays.add('SubProcess_1', { position: { bottom: 0, right: 0 }, html: '
YUP GREAT STUFF!
' }); // then expect(overlays.get({ element: 'SubProcess_1' }).length).to.equal(1); done(err); }); }); }); describe('error handling', function() { function expectMessage(e, expectedMessage) { expect(e).to.exist; if (expectedMessage instanceof RegExp) { expect(e.message).to.match(expectedMessage); } else { expect(e.message).to.equal(expectedMessage); } } function expectWarnings(warnings, expected) { expect(warnings.length).to.equal(expected.length); warnings.forEach(function(w, idx) { expectMessage(w, expected[idx]); }); } it('should handle non-bpmn input', function(done) { var xml = 'invalid stuff'; createViewer(xml, function(err) { expect(err).to.exist; expectMessage(err, /missing start tag/); done(); }); }); it('should handle invalid BPMNPlane#bpmnElement', function(done) { var xml = require('../fixtures/bpmn/error/di-plane-no-bpmn-element.bpmn'); // when createViewer(xml, function(err, warnings) { // then expect(err).not.to.exist; expectWarnings(warnings, [ 'unresolved reference ', 'no bpmnElement referenced in ', 'correcting missing bpmnElement ' + 'on ' + 'to ' ]); done(); }); }); it('should handle invalid namespaced element', function(done) { var xml = require('../fixtures/bpmn/error/categoryValue.bpmn'); // when createViewer(xml, function(err, warnings) { // then expect(err).not.to.exist; expectWarnings(warnings, [ /unparsable content detected/, 'unresolved reference ' ]); done(); }); }); it('should handle missing namespace', function(done) { var xml = require('../fixtures/bpmn/error/missing-namespace.bpmn'); // when createViewer(xml, function(err, warnings) { // then expect(err).to.exist; expect(err.message).to.eql('failed to parse document as '); expect(warnings).to.have.length(1); expect(warnings[0].message).to.match(/unparsable content detected/); done(); }); }); it('should handle duplicate ids', function(done) { var xml = require('../fixtures/bpmn/error/duplicate-ids.bpmn'); // when createViewer(xml, function(err, warnings) { // then expect(err).not.to.exist; expectWarnings(warnings, [ /duplicate ID / ]); done(); }); }); }); describe('dependency injection', function() { it('should provide self as ', function(done) { var xml = require('../fixtures/bpmn/simple.bpmn'); createViewer(xml, function(err, warnings, viewer) { expect(viewer.get('bpmnjs')).to.equal(viewer); done(err); }); }); it('should allow Diagram#get before import', function() { // when var viewer = new Viewer({ container: container }); // then var eventBus = viewer.get('eventBus'); expect(eventBus).to.exist; }); it('should keep references to services across re-import', function(done) { // given var someXML = require('../fixtures/bpmn/simple.bpmn'), otherXML = require('../fixtures/bpmn/basic.bpmn'); var viewer = new Viewer({ container: container }); var eventBus = viewer.get('eventBus'), canvas = viewer.get('canvas'); // when viewer.importXML(someXML, function() { // then expect(viewer.get('canvas')).to.equal(canvas); expect(viewer.get('eventBus')).to.equal(eventBus); viewer.importXML(otherXML, function() { // then expect(viewer.get('canvas')).to.equal(canvas); expect(viewer.get('eventBus')).to.equal(eventBus); done(); }); }); }); }); describe('export', function() { function currentTime() { return new Date().getTime(); } function validSVG(svg) { var expectedStart = ''; var expectedEnd = ''; 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')).to.equal(-1); // expect header to be written only once expect(svg.indexOf('')).to.equal(-1); expect(svg.indexOf(', svg] expect(svgNode.childNodes).to.have.length(3); // no error body expect(svgNode.body).not.to.exist; // FIXME(nre): make matcher return true; } it('should export XML', function(done) { // given var xml = require('../fixtures/bpmn/simple.bpmn'); createViewer(xml, function(err, warnings, viewer) { // when viewer.saveXML({ format: true }, function(err, xml) { // then expect(xml).to.contain(''); expect(xml).to.contain(' events', function(done) { // given var viewer = new Viewer({ container: container }); var xml = require('../fixtures/bpmn/simple.bpmn'); var events = []; viewer.on([ 'import.parse.start', 'import.parse.complete', 'import.render.start', 'import.render.complete', 'import.done' ], function(e) { // log event type + event arguments events.push([ e.type, Object.keys(e).filter(function(key) { return key !== 'type'; }) ]); }); // when viewer.importXML(xml, function(err) { // then expect(events).to.eql([ [ 'import.parse.start', [ 'xml' ] ], [ 'import.parse.complete', ['error', 'definitions', 'context' ] ], [ 'import.render.start', [ 'definitions' ] ], [ 'import.render.complete', [ 'error', 'warnings' ] ], [ 'import.done', [ 'error', 'warnings' ] ] ]); done(err); }); }); it('should work without callback', function(done) { // given var viewer = new Viewer({ container: container }); var xml = require('../fixtures/bpmn/simple.bpmn'); // when viewer.importXML(xml); // then viewer.on('import.done', function(event) { done(); }); }); }); describe('#on', function() { it('should fire with given three', function(done) { // given var viewer = new Viewer({ container: container }); var xml = require('../fixtures/bpmn/simple.bpmn'); // when viewer.on('foo', 1000, function() { return 'bar'; }, viewer); // then viewer.importXML(xml, function(err) { var eventBus = viewer.get('eventBus'); var result = eventBus.fire('foo'); expect(result).to.equal('bar'); done(); }); }); }); describe('#off', function() { var xml = require('../fixtures/bpmn/simple.bpmn'); it('should remove listener permanently', function(done) { // given var viewer = new Viewer({ container: container }); var handler = function() { return 'bar'; }; viewer.on('foo', 1000, handler); // when viewer.off('foo'); // then viewer.importXML(xml, function(err) { var eventBus = viewer.get('eventBus'); var result = eventBus.fire('foo'); expect(result).not.to.exist; done(); }); }); it('should remove listener on existing diagram instance', function(done) { // given var viewer = new Viewer({ container: container }); var handler = function() { return 'bar'; }; viewer.on('foo', 1000, handler); // when viewer.importXML(xml, function(err) { var eventBus = viewer.get('eventBus'); // when viewer.off('foo', handler); var result = eventBus.fire('foo'); expect(result).not.to.exist; done(); }); }); }); describe('#destroy', function() { it('should remove traces in document tree', function() { // given var viewer = new Viewer({ container: container }); // when viewer.destroy(); // then expect(viewer._container.parentNode).not.to.exist; }); }); describe('#attachTo', function() { it('should attach the viewer', function(done) { var xml = require('../fixtures/bpmn/simple.bpmn'); var viewer = new Viewer(); viewer.importXML(xml, function(err, warnings) { // assume expect(viewer._container.parentNode).not.to.exist; /* global sinon */ var resizedSpy = sinon.spy(); viewer.on('canvas.resized', resizedSpy); // when viewer.attachTo(container); // then expect(viewer._container.parentNode).to.equal(container); // should trigger resized expect(resizedSpy).to.have.been.called; done(err, warnings); }); }); }); describe('#detach', function() { it('should detach the viewer', function(done) { var xml = require('../fixtures/bpmn/simple.bpmn'); var viewer = new Viewer({ container: container }); viewer.importXML(xml, function(err, warnings) { // assume expect(viewer._container.parentNode).to.equal(container); // when viewer.detach(); // then expect(viewer._container.parentNode).not.to.exist; done(err, warnings); }); }); }); });