Skip to content

Commit

Permalink
feat(test): add tests to cover the dia.Cell.toJSON, dia.Graph.toJSON …
Browse files Browse the repository at this point in the history
…method and objectDifference util function
  • Loading branch information
MartinKanera committed Jul 29, 2024
1 parent 904edc0 commit c3ad952
Show file tree
Hide file tree
Showing 3 changed files with 361 additions and 0 deletions.
60 changes: 60 additions & 0 deletions packages/joint-core/test/jointjs/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,66 @@ QUnit.module('cell', function(hooks) {

assert.deepEqual(el.toJSON(), { id: 'el1', attrs: { test2: { prop2: true }}});
});

QUnit.test('should take in account `opt.ignoreDefaults` = false', function(assert) {
const rect = new joint.shapes.standard.Rectangle();

assert.deepEqual(rect.toJSON({ ignoreDefaults: false }), {
id: rect.id,
...joint.shapes.standard.Rectangle.prototype.defaults,
});
});

QUnit.test('should take in account `opt.ignoreDefaults` = false, `opt.ignoreEmptyAttributes` = true', function(assert) {
const El = joint.dia.Element.extend({
defaults: {
type: 'test.Element'
}
});

const el = new El({
foo: {
bar: {}
}
});

const expected = joint.util.cloneDeep(el.attributes);
delete expected.foo;

assert.deepEqual(el.toJSON({ ignoreDefaults: false, ignoreEmptyAttributes: true }), expected);
});

QUnit.test('should take in account `opt.ignoreDefaults` = true', function(assert) {
const rect = new joint.shapes.standard.Rectangle();

assert.deepEqual(rect.toJSON({ ignoreDefaults: true }), {
type: joint.shapes.standard.Rectangle.prototype.defaults.type,
id: rect.id,
size: {},
position: {},
attrs: {}
});
});

QUnit.test('should take in account `opt.ignoreDefaults` = true, `opt.ignoreEmptyAttributes` = true', function(assert) {
const rect = new joint.shapes.standard.Rectangle();

assert.deepEqual(rect.toJSON({ ignoreDefaults: true, ignoreEmptyAttributes: true }), {
type: joint.shapes.standard.Rectangle.prototype.defaults.type,
id: rect.id
});
});

QUnit.test('`opt.ignoreDefaults` should accept an array of keys to differentiate', function(assert) {
const rect = new joint.shapes.standard.Rectangle();

assert.deepEqual(rect.toJSON({ ignoreDefaults: ['attrs', 'size'] }), {
id: rect.id,
...joint.shapes.standard.Rectangle.prototype.defaults,
attrs: {},
size: {}
});
});
});

QUnit.module('relative vs absolute points', function() {
Expand Down
226 changes: 226 additions & 0 deletions packages/joint-core/test/jointjs/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1599,4 +1599,230 @@ QUnit.module('util', function(hooks) {
});
});

QUnit.module('objectDifference', function(assert) {

QUnit.test('should return the difference of values', function(assert) {
const expected = { b: 2 };
const actual = joint.util.objectDifference({ a: 1, b: 2 }, { a: 1, c: 3 });

assert.deepEqual(actual, expected);
});

QUnit.test('should return the difference of multiple values', function(assert) {
const expected = { b: 2, c: 3 };
const actual = joint.util.objectDifference({ a: 1, b: 2, c: 3 }, { a: 1 });

assert.deepEqual(actual, expected);
});

QUnit.test('should return the difference of nested values', function(assert) {
const expected = {
a: {
b: {
c: {
d: 4
}
}
}
};
const actual = joint.util.objectDifference(
{
a: {
b: {
c: {
d: 4
}
}
}
},
{
a: {
b: {
c: {
d: 5
}
}
}
}
);

assert.deepEqual(actual, expected);
});

QUnit.test('should return the difference of multiple nested values', function(assert) {
const expected = {
a: {
b: {
c: {
d: 4
}
}
},
foo: {
bar: {
baz: 5
}
},
x: 'y'
};

const actual = joint.util.objectDifference(
{
a: {
b: {
c: {
d: 4
}
}
},
foo: {
bar: {
baz: 5
}
},
x: 'y'
},
{
a: {
b: {
c: {
d: 5
}
}
},
foo: {
bar: {
baz: 6
}
},
x: 'z'
}
);

assert.deepEqual(actual, expected);
});

QUnit.test('should take into account the keys of the first object', function(assert) {
const expected = { foo: 'bar' };
const actual = joint.util.objectDifference({ foo: 'bar' }, { a: 1, c: 3 });

assert.deepEqual(actual, expected);
});

QUnit.test('should return an empty object when the toplevel properties are the same', function(assert) {
const expected = { foo: {}};
const actual = joint.util.objectDifference(
{
foo: {
x: 10,
y: 10
}
},
{
foo: {
x: 10,
y: 10
}
}
);

assert.deepEqual(actual, expected);
});

QUnit.test('should accept the `ignoreEmptyObjects` argument', function(assert) {
const expected = { foo: 'bar' };
const actual = joint.util.objectDifference(
{
foo: 'bar',
baz: {
x: 10,
y: 10
}
},
{
foo: '',
baz: {
x: 10,
y: 10
}
},
true
);

assert.deepEqual(actual, expected);
});

QUnit.test('should use arrays from the first object', function(assert) {
const expected = { foo: [1, 2, 3] };
const actual = joint.util.objectDifference(
{
foo: [1, 2, 3]
},
{
foo: [1, 2, 4]
}
);

assert.deepEqual(actual, expected);
});

QUnit.test('should respect the `depth` argument', function(assert) {
const expected = {
foo: {
bar: {}
},
x: {
y: {}
}
};
const actual = joint.util.objectDifference(
{
foo: {
bar: {
baz: 5
}
},
x: {
y: {
z: 5
}
}
},
{
foo: {
bar: {
test: 5
}
},
x: {
y: {
test: 5
}
}
},
false,
2
);

assert.deepEqual(actual, expected);
});

QUnit.test('should not throw `Maximum call stack size exceeded`', function(assert) {
const object1 = {};
const object2 = {};
object1.foo = object2;
object2.foo = object1;

const expected = {
foo: {
foo: {}
}
};

const actual = joint.util.objectDifference(object1, object2, false, 2);

assert.deepEqual(actual, expected);
});
});

});
75 changes: 75 additions & 0 deletions packages/joint-core/test/jointjs/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,81 @@ QUnit.module('graph', function(hooks) {
assert.ok(_.isArray(json.cells));
assert.equal(json.cells.length, 3);
});

QUnit.test('should take in account `opt.ignoreDefaults` = false', function(assert) {
const rect = new joint.shapes.standard.Rectangle();

this.graph.resetCells([rect]);
const json = this.graph.toJSON({ ignoreDefaults: false });

assert.deepEqual(json.cells[0], {
id: rect.id,
...joint.shapes.standard.Rectangle.prototype.defaults,
});
});

QUnit.test('should take in account `opt.ignoreDefaults` = false, `opt.ignoreEmptyAttributes` = true', function(assert) {
const El = joint.dia.Element.extend({
defaults: {
type: 'test.Element'
}
});

const el = new El({
foo: {
bar: {}
}
});

this.graph.resetCells([el]);
const json = this.graph.toJSON({ ignoreDefaults: false, ignoreEmptyAttributes: true });

const expected = joint.util.cloneDeep(el.attributes);
delete expected.foo;

assert.deepEqual(json.cells[0], expected);
});

QUnit.test('should take in account `opt.ignoreDefaults` = true', function(assert) {
const rect = new joint.shapes.standard.Rectangle();

this.graph.resetCells([rect]);
const json = this.graph.toJSON({ ignoreDefaults: true });

assert.deepEqual(json.cells[0], {
type: joint.shapes.standard.Rectangle.prototype.defaults.type,
id: rect.id,
size: {},
position: {},
attrs: {}
});
});

QUnit.test('should take in account `opt.ignoreDefaults` = true, `opt.ignoreEmptyAttributes` = true', function(assert) {
const rect = new joint.shapes.standard.Rectangle();

this.graph.resetCells([rect]);
const json = this.graph.toJSON({ ignoreDefaults: true, ignoreEmptyAttributes: true });

assert.deepEqual(json.cells[0], {
type: joint.shapes.standard.Rectangle.prototype.defaults.type,
id: rect.id
});
});

QUnit.test('`opt.ignoreDefaults` should accept an array of keys to differentiate', function(assert) {
const rect = new joint.shapes.standard.Rectangle();

this.graph.resetCells([rect]);
const json = this.graph.toJSON({ ignoreDefaults: ['attrs', 'size'] });

assert.deepEqual(json.cells[0], {
id: rect.id,
...joint.shapes.standard.Rectangle.prototype.defaults,
attrs: {},
size: {}
});
});
});

QUnit.module('graph.hasActiveBatch()', function() {
Expand Down

0 comments on commit c3ad952

Please sign in to comment.