Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaMan123 committed Jan 15, 2023
1 parent 0456ecb commit 6cbf2a8
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 145 deletions.
8 changes: 4 additions & 4 deletions test/unit/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@
var upperCanvasEl = canvas.upperCanvasEl;
var lowerCanvasEl = canvas.lowerCanvasEl;

function makeRect(options) {
function makeRect(options = {}) {
var defaultOptions = { width: 10, height: 10 };
return new fabric.Rect(fabric.util.object.extend(defaultOptions, options || { }));
return new fabric.Rect({ ...defaultOptions, ...options });
}

function makeTriangle(options) {
function makeTriangle(options = {}) {
var defaultOptions = { width: 30, height: 30 };
return new fabric.Triangle(fabric.util.object.extend(defaultOptions, options || { }));
return new fabric.Triangle({ ...defaultOptions, ...options });
}

let ORIGINAL_DPR;
Expand Down
4 changes: 2 additions & 2 deletions test/unit/canvas_static.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@

var lowerCanvasEl = canvas.lowerCanvasEl;

function makeRect(options) {
function makeRect(options={}) {
var defaultOptions = { width: 10, height: 10 };
return new fabric.Rect(fabric.util.object.extend(defaultOptions, options || { }));
return new fabric.Rect({ ...defaultOptions, ...options });
}

QUnit.module('fabric.StaticCanvas', {
Expand Down
17 changes: 9 additions & 8 deletions test/unit/circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,18 @@
assert.ok(typeof circle.toObject === 'function');
assert.deepEqual(circle.toObject(), defaultProperties);

circle.set('left', 100).set('top', 200).set('radius', 15);

var augmentedProperties = fabric.util.object.extend(fabric.util.object.clone(defaultProperties), {
left: 100,
top: 200,
width: 30,
circle.set('left', 100);
circle.set('top', 200);
circle.set('radius', 15);

assert.deepEqual(circle.toObject(), {
...defaultProperties,
left: 100,
top: 200,
width: 30,
height: 30,
radius: 15
});

assert.deepEqual(circle.toObject(), augmentedProperties);
});

QUnit.test('toSVG with full circle', function(assert) {
Expand Down
13 changes: 8 additions & 5 deletions test/unit/ellipse.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@
assert.ok(typeof ellipse.toObject === 'function');
assert.deepEqual(ellipse.toObject(), defaultProperties);

ellipse.set('left', 100).set('top', 200).set('rx', 15).set('ry', 25);
ellipse.set('left', 100);
ellipse.set('top', 200);
ellipse.set('rx', 15);
ellipse.set('ry', 25);

var augmentedProperties = fabric.util.object.extend(fabric.util.object.clone(defaultProperties), {
assert.deepEqual(ellipse.toObject(), {
...defaultProperties,
left: 100,
top: 200,
rx: 15,
Expand All @@ -72,8 +76,6 @@
height: 50
});

assert.deepEqual(ellipse.toObject(), augmentedProperties);

ellipse.set('rx', 30);
assert.deepEqual(ellipse.width, ellipse.rx * 2);

Expand All @@ -83,7 +85,8 @@

QUnit.test('isNotVisible', function(assert) {
var ellipse = new fabric.Ellipse();
ellipse.set('rx', 0).set('ry', 0);
ellipse.set('rx', 0);
ellipse.set('ry', 0);

assert.equal(ellipse.isNotVisible(), false, 'isNotVisible false when rx/ry are 0 because strokeWidth is > 0');

Expand Down
17 changes: 7 additions & 10 deletions test/unit/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,7 @@
QUnit.test('fromObject', function(assert) {
var done = assert.async();
assert.ok(typeof fabric.Image.fromObject === 'function');

// should not throw error when no callback is given
var obj = fabric.util.object.extend(fabric.util.object.clone(REFERENCE_IMG_OBJECT), {
src: IMG_SRC
});
fabric.Image.fromObject(obj).then(function(instance){
fabric.Image.fromObject({ ...REFERENCE_IMG_OBJECT, src: IMG_SRC }).then(function (instance) {
assert.ok(instance instanceof fabric.Image);
done();
});
Expand All @@ -480,7 +475,8 @@
QUnit.test('fromObject with clipPath and filters', function(assert) {
var done = assert.async();
// should not throw error when no callback is given
var obj = fabric.util.object.extend(fabric.util.object.clone(REFERENCE_IMG_OBJECT), {
var obj = {
...REFERENCE_IMG_OBJECT,
src: IMG_SRC,
clipPath: (new fabric.Rect({ width: 100, height: 100 })).toObject(),
filters: [{
Expand All @@ -490,7 +486,7 @@
resizeFilter: {
type: 'Resize',
}
});
};
fabric.Image.fromObject(obj).then(function(instance){
assert.ok(instance instanceof fabric.Image);
assert.ok(instance.clipPath instanceof fabric.Rect);
Expand All @@ -506,9 +502,10 @@
var done = assert.async();
assert.ok(typeof fabric.Image.fromObject === 'function');

var obj = fabric.util.object.extend(fabric.util.object.clone(REFERENCE_IMG_OBJECT), {
var obj = {
...REFERENCE_IMG_OBJECT,
src: IMG_SRC
});
};
var brightness = {
type: 'Brightness',
brightness: 0.1
Expand Down
10 changes: 4 additions & 6 deletions test/unit/object_clipPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,12 @@
assert.deepEqual(emptyObjectRepr, cObj.toObject());

cObj.clipPath = new fabric.Object();
var expected = fabric.util.object.clone(emptyObjectRepr);
var expectedClipPath = fabric.util.object.clone(emptyObjectRepr);
expectedClipPath = fabric.util.object.extend(expectedClipPath, {
expected.clipPath = {
...emptyObjectRepr,
inverted: cObj.clipPath.inverted,
absolutePositioned: cObj.clipPath.absolutePositioned,
});
expected.clipPath = expectedClipPath;
assert.deepEqual(expected, cObj.toObject());
};
assert.deepEqual(emptyObjectRepr, cObj.toObject());
cObj.clipPath.excludeFromExport = true;
assert.ok(cObj.toObject().clipPath === undefined);
});
Expand Down
9 changes: 5 additions & 4 deletions test/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@
return el;
}

function getOptions(options) {
return fabric.util.object.extend(fabric.util.object.clone({
left: 10, top: 20, width: 30, height: 40
}), options || { });
function getOptions(options = {}) {
return {
left: 10, top: 20, width: 30, height: 40,
...options,
}
}

var elements = [
Expand Down
9 changes: 5 additions & 4 deletions test/unit/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
updatePath(path, REFERENCE_PATH_OBJECT.path, true);
assert.deepEqual(path.toObject(), REFERENCE_PATH_OBJECT);
updatePath(path, REFERENCE_PATH_OBJECT.path, false);
var opts = fabric.util.object.clone(REFERENCE_PATH_OBJECT);
var opts = { ...REFERENCE_PATH_OBJECT };
delete opts.path;
path.set(opts);
updatePath(path, 'M 100 100 L 300 100 L 200 300 z', true);
Expand Down Expand Up @@ -226,7 +226,7 @@
makePathObject(function(path) {
var src = 'http://example.com/';
path.sourcePath = src;
var clonedRef = fabric.util.object.clone(REFERENCE_PATH_OBJECT);
var clonedRef = { ...REFERENCE_PATH_OBJECT };
clonedRef.sourcePath = src;
delete clonedRef.path;
assert.deepEqual(path.toDatalessObject(), clonedRef, 'if sourcePath the object looses path');
Expand Down Expand Up @@ -285,12 +285,13 @@
fabric.Path.fromElement(elPath, function(path) {
assert.ok(path instanceof fabric.Path);

assert.deepEqual(path.toObject(), fabric.util.object.extend(REFERENCE_PATH_OBJECT, {
assert.deepEqual(path.toObject(), {
...REFERENCE_PATH_OBJECT,
strokeDashArray: [5, 2],
strokeLineCap: 'round',
strokeLineJoin: 'bevel',
strokeMiterLimit: 5
}));
});

var ANGLE_DEG = 90;
elPath.setAttributeNS(namespace, 'transform', 'rotate(' + ANGLE_DEG + ')');
Expand Down
34 changes: 14 additions & 20 deletions test/unit/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,10 @@
var polygon = new fabric.Polygon(getPoints());
assert.ok(typeof polygon.toObject === 'function');

var objectWithOriginalPoints = fabric.util.object.extend(polygon.toObject(), {
assert.deepEqual({
...polygon.toObject(),
points: getPoints()
});

assert.deepEqual(objectWithOriginalPoints, REFERENCE_OBJECT);
}, REFERENCE_OBJECT);
});

QUnit.test('toSVG', function(assert) {
Expand All @@ -176,24 +175,19 @@
QUnit.test('fromElement without points', function(assert) {
assert.ok(typeof fabric.Polygon.fromElement === 'function');

var empty_object = fabric.util.object.extend({}, REFERENCE_OBJECT);
empty_object = fabric.util.object.extend(empty_object, REFERENCE_EMPTY_OBJECT);

var elPolygonWithoutPoints = fabric.getDocument().createElementNS('http://www.w3.org/2000/svg', 'polygon');

fabric.Polygon.fromElement(elPolygonWithoutPoints, function(polygon) {
assert.deepEqual(polygon.toObject(), empty_object);
assert.deepEqual(polygon.toObject(), { ...REFERENCE_OBJECT, ...REFERENCE_EMPTY_OBJECT });
});
});

QUnit.test('fromElement with empty points', function(assert) {
var namespace = 'http://www.w3.org/2000/svg';
var elPolygonWithEmptyPoints = fabric.getDocument().createElementNS(namespace, 'polygon');
elPolygonWithEmptyPoints.setAttributeNS(namespace, 'points', '');
var empty_object = fabric.util.object.extend({}, REFERENCE_OBJECT);
empty_object = fabric.util.object.extend(empty_object, REFERENCE_EMPTY_OBJECT);
fabric.Polygon.fromElement(elPolygonWithEmptyPoints, function(polygon) {
assert.deepEqual(polygon.toObject(), empty_object);
assert.deepEqual(polygon.toObject(), { ...REFERENCE_OBJECT, ...REFERENCE_EMPTY_OBJECT });
});
});

Expand All @@ -203,13 +197,12 @@
elPolygon.setAttributeNS(namespace, 'points', '10,12 20,22');
fabric.Polygon.fromElement(elPolygon, function(polygon) {
assert.ok(polygon instanceof fabric.Polygon);
var expected = fabric.util.object.extend(
fabric.util.object.clone(REFERENCE_OBJECT), {
points: [{ x: 10, y: 12 }, { x: 20, y: 22 }],
left: 10,
top: 12
});
assert.deepEqual(polygon.toObject(), expected);
assert.deepEqual(polygon.toObject(), {
...REFERENCE_OBJECT,
points: [{ x: 10, y: 12 }, { x: 20, y: 22 }],
left: 10,
top: 12
});
});
});

Expand All @@ -233,7 +226,8 @@
{ x: 30, y: 30 },
{ x: 10, y: 10 }
];
assert.deepEqual(polygonWithAttrs.toObject(), fabric.util.object.extend(REFERENCE_OBJECT, {
assert.deepEqual(polygonWithAttrs.toObject(), {
...REFERENCE_OBJECT,
width: 20,
height: 20,
fill: 'rgb(255,255,255)',
Expand All @@ -247,7 +241,7 @@
points: expectedPoints,
top: 10,
left: 10,
}));
});
});
});
QUnit.test('fromElement with null', function(assert) {
Expand Down
30 changes: 14 additions & 16 deletions test/unit/polyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@
QUnit.test('toObject', function(assert) {
var polyline = new fabric.Polyline(getPoints());
assert.ok(typeof polyline.toObject === 'function');
var objectWithOriginalPoints = fabric.util.object.extend(polyline.toObject(), {
points: getPoints()
});

assert.deepEqual(objectWithOriginalPoints, REFERENCE_OBJECT);
assert.deepEqual({
...polyline.toObject(),
points: getPoints()
}, REFERENCE_OBJECT);
});

QUnit.test('toSVG', function(assert) {
Expand All @@ -99,10 +99,8 @@
QUnit.test('fromElement without points', function(assert) {
assert.ok(typeof fabric.Polyline.fromElement === 'function');
var elPolylineWithoutPoints = fabric.getDocument().createElementNS('http://www.w3.org/2000/svg', 'polyline');
var empty_object = fabric.util.object.extend({}, REFERENCE_OBJECT);
empty_object = fabric.util.object.extend(empty_object, REFERENCE_EMPTY_OBJECT);
fabric.Polyline.fromElement(elPolylineWithoutPoints, function(polyline) {
assert.deepEqual(polyline.toObject(), empty_object);
assert.deepEqual(polyline.toObject(), { ...REFERENCE_OBJECT, ...REFERENCE_EMPTY_OBJECT });
});
});

Expand All @@ -111,9 +109,7 @@
var elPolylineWithEmptyPoints = fabric.getDocument().createElementNS(namespace, 'polyline');
elPolylineWithEmptyPoints.setAttributeNS(namespace, 'points', '');
fabric.Polyline.fromElement(elPolylineWithEmptyPoints, function(polyline) {
var empty_object = fabric.util.object.extend({}, REFERENCE_OBJECT);
empty_object = fabric.util.object.extend(empty_object, REFERENCE_EMPTY_OBJECT);
assert.deepEqual(polyline.toObject(), empty_object);
assert.deepEqual(polyline.toObject(), { ...REFERENCE_OBJECT, ...REFERENCE_EMPTY_OBJECT });
});
});

Expand All @@ -124,10 +120,11 @@
elPolyline.setAttributeNS(namespace, 'stroke-width', 1);
fabric.Polyline.fromElement(elPolyline, function(polyline) {
assert.ok(polyline instanceof fabric.Polyline);
var obj = fabric.util.object.extend({}, REFERENCE_OBJECT);
obj.top = 12;
obj.left = 10;
assert.deepEqual(polyline.toObject(), obj);
assert.deepEqual(polyline.toObject(), {
...REFERENCE_OBJECT,
left: 10,
top: 12
});
});
});

Expand All @@ -147,7 +144,8 @@

fabric.Polyline.fromElement(elPolylineWithAttrs, function(polylineWithAttrs) {
var expectedPoints = [{x: 10, y: 10}, {x: 20, y: 20}, {x: 30, y: 30}, {x: 10, y: 10}];
assert.deepEqual(polylineWithAttrs.toObject(), fabric.util.object.extend(REFERENCE_OBJECT, {
assert.deepEqual(polylineWithAttrs.toObject(), {
...REFERENCE_OBJECT,
width: 20,
height: 20,
fill: 'rgb(255,255,255)',
Expand All @@ -161,7 +159,7 @@
points: expectedPoints,
left: 10,
top: 10,
}));
});
});
});

Expand Down
Loading

0 comments on commit 6cbf2a8

Please sign in to comment.