Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(context-pad): remove return statement #895

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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