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

isEqual fails hard. #4949

Merged
merged 10 commits into from
May 7, 2018
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
7 changes: 5 additions & 2 deletions src/mixins/stateful.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
return true;
}
else if (Array.isArray(origValue)) {
if (origValue.length !== currentValue.length) {
if (!Array.isArray(currentValue) || origValue.length !== currentValue.length) {
return false;
}
for (var i = 0, len = origValue.length; i < len; i++) {
Expand All @@ -32,7 +32,10 @@
}
else if (origValue && typeof origValue === 'object') {
var keys = Object.keys(origValue), key;
if (!firstPass && keys.length !== Object.keys(currentValue).length) {
if (!currentValue ||
typeof currentValue !== 'object' ||
(!firstPass && keys.length !== Object.keys(currentValue).length)
) {
return false;
}
for (var i = 0, len = keys.length; i < len; i++) {
Expand Down
44 changes: 42 additions & 2 deletions test/unit/stateful.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(function(){

QUnit.module('fabric.stateful');

QUnit.test('hasStateChanged', function(assert) {
var cObj = new fabric.Object();
assert.ok(typeof cObj.hasStateChanged === 'function');
Expand Down Expand Up @@ -53,6 +52,20 @@
assert.ok(cObj.hasStateChanged(), 'more properties added');
});

QUnit.test('saveState with array to null', function(assert) {
var cObj = new fabric.Text('Hello');
cObj.set('strokeDashArray', [0, 4]);
cObj.setupState();
//eqaul(cObj.underline, cObj._stateProperties.underline, 'textDecoration in state is deepEqual');
//notEqual(cObj.textDecoration, cObj._stateProperties.textDecoration, 'textDecoration in not same Object');
cObj.strokeDashArray = null;
assert.ok(cObj.hasStateChanged(), 'hasStateChanged detects changes in array without throwing');

cObj.saveState();
cObj.strokeDashArray = [2, 3];
assert.ok(cObj.hasStateChanged(), 'back to array');
});

QUnit.test('saveState with fabric class gradient', function(assert) {
var cObj = new fabric.Object();
var gradient = new fabric.Gradient({
Expand Down Expand Up @@ -84,6 +97,34 @@
assert.ok(cObj.hasStateChanged(), 'hasStateChanged detects changes in nested props on third level of nesting');
});

QUnit.test('saveState with fabric class gradient to other types', function(assert) {
var cObj = new fabric.Object();
var gradient = new fabric.Gradient({
type: 'linear',
coords: {
x1: 0,
y1: 10,
x2: 100,
y2: 200,
},
colorStops: [
{ offset: 0, color: 'red', opacity: 0 },
{ offset: 1, color: 'green' }
]
});

cObj.set('fill', gradient);
cObj.setupState();
cObj.set('fill', 'red');
assert.ok(cObj.hasStateChanged(), 'hasStateChanged detects changes in object to string without throwing');
cObj.saveState();
cObj.set('fill', gradient);
assert.ok(cObj.hasStateChanged(), 'back to gradient');
cObj.saveState();
cObj.set('fill', null);
assert.ok(cObj.hasStateChanged(), 'hasStateChanged detects changes in object to null without throwing');
});

QUnit.test('savestate with custom property set', function(assert) {
var cObj = new fabric.Object();
cObj.myProperties = ['a', 'b'];
Expand All @@ -99,5 +140,4 @@
cObj.a = 2;
assert.ok(cObj.hasStateChanged('myProperties'), 'custom state has changed');
});

})();