From 9cc91a4d4567968ae03108f55d0fa9a5d63d57b9 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 | 110 ++++++++++++++++-- 2 files changed, 118 insertions(+), 14 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..0567e05b2 100755 --- a/test/spec/features/context-pad/ContextPadSpec.js +++ b/test/spec/features/context-pad/ContextPadSpec.js @@ -1467,30 +1467,122 @@ describe('features/context-pad', function() { })); - afterEach(function() { - console.warn.restore(); - }); + it('should return pad', inject(function(canvas, contextPad) { + // given + var shape = canvas.addShape({ id: 's1', width: 100, height: 100, x: 10, y: 10 }); - it('should return pad', inject(function(canvas, contextPad) { + // when + const pad = contextPad.getPad(shape); + + // then + expect(pad).to.exist; + expect(pad.html).to.exist; + })); + + + 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 }); - var warnSpy = sinon.spy(console, 'warn'); + contextPad.open(shape); + + var spy = sinon.spy(contextPad, '_createHtml'); // when const pad = contextPad.getPad(shape); // then expect(pad).to.exist; - expect(pad.html).to.exist; + expect(spy).not.to.have.been.called; + })); - 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/); + + 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; + })); + + + describe('deprecation warning', function() { + + var warnSpy; + + beforeEach(function() { + warnSpy = sinon.spy(console, 'warn'); + }); + + afterEach(function() { + console.warn.restore(); + }); + + it('should log deprecation warning', inject(function(canvas, contextPad) { + + // given + var shape = canvas.addShape({ id: 's1', width: 100, height: 100, x: 10, y: 10 }); + + // when + const pad = contextPad.getPad(shape); + + // then + expect(pad).to.exist; + + 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/); + })); + }); + }); });