From a42ba5cd90fdc438156e262680921605feba992a Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Sat, 11 Dec 2021 15:00:30 +0100 Subject: [PATCH] feat(util/ModelUtil): expose `isAny` It is a core utility that will be tree-shaken if not used. We'll continue to expose it via `ModelingUtil` for backwards compatibility. --- lib/features/modeling/util/ModelingUtil.js | 22 +---- lib/util/ModelUtil.js | 19 +++++ test/spec/util/ModelUtilSpec.js | 93 +++++++++++++++------- 3 files changed, 85 insertions(+), 49 deletions(-) diff --git a/lib/features/modeling/util/ModelingUtil.js b/lib/features/modeling/util/ModelingUtil.js index 92d14780..98875d08 100644 --- a/lib/features/modeling/util/ModelingUtil.js +++ b/lib/features/modeling/util/ModelingUtil.js @@ -1,24 +1,6 @@ -import { - some -} from 'min-dash'; - -import { is } from '../../../util/ModelUtil'; - - -/** - * Return true if element has any of the given types. - * - * @param {djs.model.Base} element - * @param {Array} types - * - * @return {boolean} - */ -export function isAny(element, types) { - return some(types, function(t) { - return is(element, t); - }); -} +export { is, isAny } from '../../../util/ModelUtil'; +import { isAny } from '../../../util/ModelUtil'; /** * Return the parent of the element with any of the given types. diff --git a/lib/util/ModelUtil.js b/lib/util/ModelUtil.js index 1d4e450b..3a75fc40 100644 --- a/lib/util/ModelUtil.js +++ b/lib/util/ModelUtil.js @@ -1,3 +1,8 @@ +import { + some +} from 'min-dash'; + + /** * Is an element of the given BPMN type? * @@ -13,6 +18,20 @@ export function is(element, type) { } +/** + * Return true if element has any of the given types. + * + * @param {djs.model.Base} element + * @param {Array} types + * + * @return {boolean} + */ +export function isAny(element, types) { + return some(types, function(t) { + return is(element, t); + }); +} + /** * Return the business object for a given element. * diff --git a/test/spec/util/ModelUtilSpec.js b/test/spec/util/ModelUtilSpec.js index e2ac7a73..5168bb67 100644 --- a/test/spec/util/ModelUtilSpec.js +++ b/test/spec/util/ModelUtilSpec.js @@ -8,6 +8,7 @@ import modelingModule from 'lib/features/modeling'; import { is, + isAny, getDi } from 'lib/util/ModelUtil'; @@ -24,51 +25,85 @@ describe('util/ModelUtil', function() { })); - it('should work with diagram element', inject(function(elementFactory) { + describe('#is', function() { - // given - var messageFlowConnection = elementFactory.createConnection({ type: 'bpmn:MessageFlow' }); + it('should work with diagram element', inject(function(elementFactory) { - // then - expect(is(messageFlowConnection, 'bpmn:MessageFlow')).to.be.true; - expect(is(messageFlowConnection, 'bpmn:BaseElement')).to.be.true; + // given + var messageFlowConnection = elementFactory.createConnection({ type: 'bpmn:MessageFlow' }); - expect(is(messageFlowConnection, 'bpmn:SequenceFlow')).to.be.false; - expect(is(messageFlowConnection, 'bpmn:Task')).to.be.false; - })); + // then + expect(is(messageFlowConnection, 'bpmn:MessageFlow')).to.be.true; + expect(is(messageFlowConnection, 'bpmn:BaseElement')).to.be.true; + + expect(is(messageFlowConnection, 'bpmn:SequenceFlow')).to.be.false; + expect(is(messageFlowConnection, 'bpmn:Task')).to.be.false; + })); - it('should work with business object', inject(function(bpmnFactory) { + it('should work with business object', inject(function(bpmnFactory) { - // given - var gateway = bpmnFactory.create('bpmn:Gateway'); + // given + var gateway = bpmnFactory.create('bpmn:Gateway'); - // then - expect(is(gateway, 'bpmn:Gateway')).to.be.true; - expect(is(gateway, 'bpmn:BaseElement')).to.be.true; + // then + expect(is(gateway, 'bpmn:Gateway')).to.be.true; + expect(is(gateway, 'bpmn:BaseElement')).to.be.true; - expect(is(gateway, 'bpmn:SequenceFlow')).to.be.false; - })); + expect(is(gateway, 'bpmn:SequenceFlow')).to.be.false; + })); - it('should work with untyped business object', inject(function() { + it('should work with untyped business object', inject(function() { - // given - var foo = { businessObject: 'BAR' }; + // given + var foo = { businessObject: 'BAR' }; - // then - expect(is(foo, 'FOO')).to.be.false; - })); + // then + expect(is(foo, 'FOO')).to.be.false; + })); - it('should work with untyped diagram element', inject(function() { + it('should work with untyped diagram element', inject(function() { - // given - var foo = { }; + // given + var foo = { }; - // then - expect(is(foo, 'FOO')).to.be.false; - })); + // then + expect(is(foo, 'FOO')).to.be.false; + })); + + }); + + + describe('isAny', function() { + + it('should work on shape', inject(function(bpmnFactory, elementFactory) { + + // given + var element = elementFactory.createShape({ type: 'bpmn:Gateway' }); + + // then + expect(isAny(element, [ 'bpmn:Gateway' ])).to.be.true; + expect(isAny(element, [ 'bpmn:SequenceFlow', 'bpmn:Gateway' ])).to.be.true; + expect(isAny(element, [ 'bpmn:BaseElement' ])).to.be.true; + expect(isAny(element, [ 'bpmn:SequenceFlow' ])).to.be.false; + })); + + + it('should work on businessObject', inject(function(bpmnFactory, elementFactory) { + + // given + var businessObject = bpmnFactory.create('bpmn:Gateway'); + + // then + expect(isAny(businessObject, [ 'bpmn:Gateway' ])).to.be.true; + expect(isAny(businessObject, [ 'bpmn:SequenceFlow', 'bpmn:Gateway' ])).to.be.true; + expect(isAny(businessObject, [ 'bpmn:BaseElement' ])).to.be.true; + expect(isAny(businessObject, [ 'bpmn:SequenceFlow' ])).to.be.false; + })); + + }); describe('#getDi', function() {