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 7f87f70
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 14 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);
});
}
110 changes: 101 additions & 9 deletions test/spec/features/context-pad/ContextPadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
}));
});

});

});
Expand Down

0 comments on commit 7f87f70

Please sign in to comment.