'use strict'; var TestHelper = require('../TestHelper'); var Viewer = require('../../lib/Viewer'); describe('Viewer', function() { var container; beforeEach(function() { container = jasmine.getEnv().getTestContainer(); }); 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'); createViewer(xml, done); }); it('should import empty definitions', function(done) { var xml = require('../fixtures/bpmn/empty-definitions.bpmn'); createViewer(xml, done); }); it('should re-import simple process', function(done) { var xml = require('../fixtures/bpmn/simple.bpmn'); // given createViewer(xml, function(err, warnings, viewer) { if (err) { return done(err); } // when // mimic re-import of same diagram viewer.importXML(xml, function(err, warnings) { // then expect(err).toBeFalsy(); expect(warnings.length).toBe(0); done(); }); }); }); describe('defaults', function() { it('should use as default parent', function(done) { var xml = require('../fixtures/bpmn/simple.bpmn'); var viewer = new Viewer(); viewer.importXML(xml, function(err, warnings) { expect(viewer.container.parentNode).toBe(document.body); done(err, warnings); }); }); }); describe('import events', function() { it('should fire events', function(done) { // given var viewer = new Viewer({ container: container }); var xml = require('../fixtures/bpmn/empty-definitions.bpmn'); var events = []; viewer.on('import.start', function() { events.push('import.start'); }); viewer.on('import.success', function() { events.push('import.success'); }); viewer.on('import.error', function() { events.push('import.error'); }); // when viewer.importXML(xml, function(err) { // then expect(events).toEqual([ 'import.start', 'import.success' ]); done(err); }); }); }); 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).toBeDefined(); expect(elementRegistry).toBeDefined(); // when overlays.add('SubProcess_1', { position: { bottom: 0, right: 0 }, html: '
YUP GREAT STUFF!
' }); // then expect(overlays.get({ element: 'SubProcess_1' }).length).toBe(1); done(err); }); }); }); describe('error handling', function() { function expectMessage(e, expectedMessage) { expect(e).toBeDefined(); if (expectedMessage instanceof RegExp) { expect(e.message).toMatch(expectedMessage); } else { expect(e.message).toEqual(expectedMessage); } } function expectWarnings(warnings, expected) { expect(warnings.length).toBe(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).toBeTruthy(); expectMessage(err, /Text data outside of root node./); 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).toBeFalsy(); 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).toBeFalsy(); 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).toBeFalsy(); expectWarnings(warnings, [ /unparsable content detected/, 'unresolved reference ', 'no bpmnElement referenced in ', 'no bpmnElement referenced in ', 'no process or collaboration present to display' ]); done(); }); }); }); describe('dependency injection', function() { it('should be available via di as ', function(done) { var xml = require('../fixtures/bpmn/simple.bpmn'); createViewer(xml, function(err, warnings, viewer) { expect(viewer.get('bpmnjs')).toBe(viewer); done(err); }); }); }); describe('export', function() { function currentTime() { return new Date().getTime(); } function isValid(svg) { var expectedStart = ''; var expectedEnd = ''; expect(svg.indexOf(expectedStart)).toEqual(0); expect(svg.indexOf(expectedEnd)).toEqual(svg.length - expectedEnd.length); // ensure correct rendering of SVG contents expect(svg.indexOf('undefined')).toBe(-1); // expect header to be written only once expect(svg.indexOf('')).toBe(-1); expect(svg.indexOf('