From d5dc43ef32ed7682183efec9ea7337001b6e0e71 Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Mon, 10 Aug 2015 15:13:34 +0200 Subject: [PATCH] fix(util): ensure ModelUtil#is never fails --- lib/util/ModelUtil.js | 2 +- test/spec/util/ModelUtilSpec.js | 66 +++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 test/spec/util/ModelUtilSpec.js diff --git a/lib/util/ModelUtil.js b/lib/util/ModelUtil.js index 7dc0056d..1b4b814e 100644 --- a/lib/util/ModelUtil.js +++ b/lib/util/ModelUtil.js @@ -11,7 +11,7 @@ function is(element, type) { var bo = getBusinessObject(element); - return bo && bo.$instanceOf(type); + return bo && (typeof bo.$instanceOf === 'function') && bo.$instanceOf(type); } module.exports.is = is; diff --git a/test/spec/util/ModelUtilSpec.js b/test/spec/util/ModelUtilSpec.js new file mode 100644 index 00000000..6d965636 --- /dev/null +++ b/test/spec/util/ModelUtilSpec.js @@ -0,0 +1,66 @@ +'use strict'; + +require('../../TestHelper'); + +/* global bootstrapModeler, inject */ + +var coreModule = require('../../../lib/core'), + modelingModule = require('../../../lib/features/modeling'); + +var ModelUtil = require('../../../lib/util/ModelUtil'); + + +describe('ModelUtil', function() { + + var diagramXML = require('../../fixtures/bpmn/empty-definitions.bpmn'); + + beforeEach(bootstrapModeler(diagramXML, { modules: [ coreModule, modelingModule ] })); + + + it('should work with diagram element', inject(function(elementFactory) { + + // given + var messageFlowConnection = elementFactory.createConnection({ type: 'bpmn:MessageFlow' }); + + // then + expect(ModelUtil.is(messageFlowConnection, 'bpmn:MessageFlow')).to.be.true; + expect(ModelUtil.is(messageFlowConnection, 'bpmn:BaseElement')).to.be.true; + + expect(ModelUtil.is(messageFlowConnection, 'bpmn:SequenceFlow')).to.be.false; + expect(ModelUtil.is(messageFlowConnection, 'bpmn:Task')).to.be.false; + })); + + + it('should work with business object', inject(function(bpmnFactory) { + + // given + var gateway = bpmnFactory.create('bpmn:Gateway'); + + // then + expect(ModelUtil.is(gateway, 'bpmn:Gateway')).to.be.true; + expect(ModelUtil.is(gateway, 'bpmn:BaseElement')).to.be.true; + + expect(ModelUtil.is(gateway, 'bpmn:SequenceFlow')).to.be.false; + })); + + + it('should work with untyped business object', inject(function() { + + // given + var foo = { businessObject: 'BAR' }; + + // then + expect(ModelUtil.is(foo, 'FOO')).to.be.false; + })); + + + it('should work with untyped diagram element', inject(function() { + + // given + var foo = { }; + + // then + expect(ModelUtil.is(foo, 'FOO')).to.be.false; + })); + +}); \ No newline at end of file