Skip to content

Commit

Permalink
fix(context-pad): remove return statement
Browse files Browse the repository at this point in the history
* return statement should have been removed with #888
* account for possibility of target being array (target === [ target ])
  • Loading branch information
philippfromme committed May 7, 2024
1 parent a84d11a commit 1101e4b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 10 deletions.
22 changes: 17 additions & 5 deletions lib/features/context-pad/ContextPad.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div class="djs-context-pad"></div>');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
}
80 changes: 75 additions & 5 deletions test/spec/features/context-pad/ContextPadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1467,11 +1467,6 @@ describe('features/context-pad', function() {
}));


afterEach(function() {
console.warn.restore();
});


it('should return pad', inject(function(canvas, contextPad) {

// given
Expand All @@ -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;
}));

});
Expand Down

0 comments on commit 1101e4b

Please sign in to comment.