From 1101e4ba4eff952aecc93cd034a8d04d9c91afcb Mon Sep 17 00:00:00 2001 From: Philipp Fromme Date: Tue, 7 May 2024 12:30:57 +0200 Subject: [PATCH] fix(context-pad): remove return statement * return statement should have been removed with https://github.com/bpmn-io/diagram-js/pull/888 * account for possibility of target being array (target === [ target ]) --- lib/features/context-pad/ContextPad.js | 22 +++-- .../features/context-pad/ContextPadSpec.js | 80 +++++++++++++++++-- 2 files changed, 92 insertions(+), 10 deletions(-) diff --git a/lib/features/context-pad/ContextPad.js b/lib/features/context-pad/ContextPad.js index ab3d9a910..e715d855e 100644 --- a/lib/features/context-pad/ContextPad.js +++ b/lib/features/context-pad/ContextPad.js @@ -394,10 +394,6 @@ ContextPad.prototype._updateAndOpen = function(target) { * @return {HTMLElement} */ ContextPad.prototype._createHtml = function(target) { - if (this.isOpen()) { - return this._current.pad; - } - var self = this; var html = domify('
'); @@ -443,7 +439,7 @@ ContextPad.prototype.getPad = function(target) { let html; - if (this.isOpen() && this._current.target === target) { + if (this.isOpen() && targetsEqual(this._current.target, target)) { html = this._current.html; } else { html = this._createHtml(target); @@ -675,4 +671,20 @@ function includes(array, item) { function getLastWaypoint(connection) { return connection.waypoints[connection.waypoints.length - 1]; +} + +/** + * @param {ContextPadTarget} target + * @param {ContextPadTarget} otherTarget + * + * @return {boolean} + */ +function targetsEqual(target, otherTarget) { + target = isArray(target) ? target : [ target ]; + otherTarget = isArray(otherTarget) ? otherTarget : [ otherTarget ]; + + return target.length === otherTarget.length + && every(target, function(element) { + return otherTarget.includes(element); + }); } \ No newline at end of file diff --git a/test/spec/features/context-pad/ContextPadSpec.js b/test/spec/features/context-pad/ContextPadSpec.js index 479401cfc..e0a3a13c2 100755 --- a/test/spec/features/context-pad/ContextPadSpec.js +++ b/test/spec/features/context-pad/ContextPadSpec.js @@ -1467,11 +1467,6 @@ describe('features/context-pad', function() { })); - afterEach(function() { - console.warn.restore(); - }); - - it('should return pad', inject(function(canvas, contextPad) { // given @@ -1489,6 +1484,81 @@ describe('features/context-pad', function() { expect(warnSpy).to.have.been.calledOnce; expect(warnSpy.getCall(0).args[ 0 ]).to.be.instanceOf(Error); expect(warnSpy.getCall(0).args[ 0 ].message).to.match(/is deprecated/); + + console.warn.restore(); + })); + + + it('should return existing if targets equal (target === target)', inject(function(canvas, contextPad) { + + // given + var shape = canvas.addShape({ id: 's1', width: 100, height: 100, x: 10, y: 10 }); + + contextPad.open(shape); + + var spy = sinon.spy(contextPad, '_createHtml'); + + // when + const pad = contextPad.getPad(shape); + + // then + expect(pad).to.exist; + expect(spy).not.to.have.been.called; + })); + + + it('should return existing if targets equal (target === [ target ])', inject(function(canvas, contextPad) { + + // given + var shape = canvas.addShape({ id: 's1', width: 100, height: 100, x: 10, y: 10 }); + + contextPad.open(shape); + + var spy = sinon.spy(contextPad, '_createHtml'); + + // when + const pad = contextPad.getPad([ shape ]); + + // then + expect(pad).to.exist; + expect(spy).not.to.have.been.called; + })); + + + it('should return existing if targets equal ([ target ] === target)', inject(function(canvas, contextPad) { + + // given + var shape = canvas.addShape({ id: 's1', width: 100, height: 100, x: 10, y: 10 }); + + contextPad.open([ shape ]); + + var spy = sinon.spy(contextPad, '_createHtml'); + + // when + const pad = contextPad.getPad(shape); + + // then + expect(pad).to.exist; + expect(spy).not.to.have.been.called; + })); + + + it('should return new if targets not equal (target !== target)', inject(function(canvas, contextPad) { + + // given + var shape = canvas.addShape({ id: 's1', width: 100, height: 100, x: 10, y: 10 }), + shape2 = canvas.addShape({ id: 's2', width: 100, height: 100, x: 10, y: 10 }); + + contextPad.open(shape); + + var spy = sinon.spy(contextPad, '_createHtml'); + + // when + const pad = contextPad.getPad(shape2); + + // then + expect(pad).to.exist; + expect(spy).to.have.been.called; })); });