1
0
Fork 0

chore(core): Adding unit tests

This commit is contained in:
joostdecock 2022-07-24 21:34:22 +02:00
parent a357c2e642
commit 293c6bec09
4 changed files with 82 additions and 28 deletions

View file

@ -147,8 +147,6 @@ Part.prototype.inject = function (orig) {
for (let i in orig.points) { for (let i in orig.points) {
if (orig.points[i] === p) return i if (orig.points[i] === p) return i
} }
return false
} }
for (let i in orig.points) this.points[i] = orig.points[i].clone() for (let i in orig.points) this.points[i] = orig.points[i].clone()

View file

@ -82,11 +82,27 @@ it("Should set part attributes", () => {
it("Should inject a part", () => { it("Should inject a part", () => {
let pattern = new freesewing.Pattern(); let pattern = new freesewing.Pattern();
let part = new pattern.Part(); let part = new pattern.Part();
part.points.foo = new part.Point(12, 23); part.points.a = new part.Point(12, 23);
part.points.b = new part.Point(10, 10);
part.points.c = new part.Point(20, 20);
part.paths.bar = new freesewing.Path()
.move(part.points.a)
.line(part.points.b)
.curve(part.points.c, part.points.b, part.points.a)
const { Snippet, snippets } = part.shorthand()
snippets.d = new Snippet('notch', part.points.a)
let test = new pattern.Part(); let test = new pattern.Part();
test.inject(part); test.inject(part);
expect(test.points.foo.x).to.equal(12); expect(test.points.a.x).to.equal(12);
expect(test.points.foo.y).to.equal(23); expect(test.points.a.y).to.equal(23);
expect(test.paths.bar.ops.length).to.equal(3)
for (let i=0;i<3;i++) {
expect(test.paths.bar.ops[i].type).to.equal(part.paths.bar.ops[i].type)
expect(test.paths.bar.ops[i].to.x).to.equal(part.paths.bar.ops[i].to.x)
expect(test.paths.bar.ops[i].to.y).to.equal(part.paths.bar.ops[i].to.y)
}
expect(test.snippets.d.anchor.x).to.equal(part.points.a.x)
expect(test.snippets.d.anchor.y).to.equal(part.points.a.y)
}); });
it("Should return shorthand", () => { it("Should return shorthand", () => {
@ -98,22 +114,31 @@ it("Should return shorthand", () => {
expect(short.complete).to.equal(true); expect(short.complete).to.equal(true);
expect(short.paperless).to.equal(true); expect(short.paperless).to.equal(true);
}); });
/*
it("Should not allow a margin below 10 for paperless patterns", () => { it("Should raise a warning when setting a non-Point value in points", () => {
let pattern = new freesewing.Pattern(); const pattern = new freesewing.Pattern();
pattern.settings.paperless = true; pattern.settings.mode = "draft";
pattern.settings.margin = 2; const part = new pattern.Part();
pattern.parts.a = new pattern.Part(); const { points } = part.shorthand()
let a = pattern.parts.a; points.a = 'banana'
a.points.a = new pattern.Point(0,0); expect(pattern.events.warning.length).to.equal(3)
a.points.b = new pattern.Point(0,100); expect(pattern.events.warning[0]).to.equal('`points.a` was set with a value that is not a `Point` object')
a.paths.a = new pattern.Path() expect(pattern.events.warning[1]).to.equal('`points.a` was set with a `x` parameter that is not a `number`')
.move(a.points.a) expect(pattern.events.warning[2]).to.equal('`points.a` was set with a `y` parameter that is not a `number`')
.line(a.points.b);
pattern.draft();
expect(pattern.width).to.equal(120);
}); });
*/
it("Should raise a warning when setting a non-Snippet value in snippets", () => {
const pattern = new freesewing.Pattern();
pattern.settings.mode = "draft";
const part = new pattern.Part();
const { snippets } = part.shorthand()
snippets.a = 'banana'
expect(pattern.events.warning.length).to.equal(3)
expect(pattern.events.warning[0]).to.equal('`snippets.a` was set with a value that is not a `Snippet` object')
expect(pattern.events.warning[1]).to.equal('`snippets.a` was set with a `def` parameter that is not a `string`')
expect(pattern.events.warning[2]).to.equal('`snippets.a` was set with an `anchor` parameter that is not a `Point`')
});
it("Should calculate the part boundary with default margin", () => { it("Should calculate the part boundary with default margin", () => {
let pattern = new freesewing.Pattern(); let pattern = new freesewing.Pattern();
pattern.settings.mode = "draft"; pattern.settings.mode = "draft";
@ -206,3 +231,23 @@ it("Should only stack a part if needed", () => {
part.stack(); part.stack();
expect(part.attributes.get("transform")).to.equal(false); expect(part.attributes.get("transform")).to.equal(false);
}); });
it("Should run hooks", () => {
let count = 0
const pattern = new freesewing.Pattern()
const part = new pattern.Part();
part.hooks.preDraft = [{ method: function(p) { count++ }} ]
part.runHooks('preDraft')
expect(count).to.equal(1);
});
it("Should get the units closure to raise a debug when passing a non-number", () => {
const pattern = new freesewing.Pattern();
pattern.settings.mode = "draft";
const part = new pattern.Part();
const short = part.shorthand();
short.units('a')
expect(pattern.events.debug.length).to.equal(1)
expect(pattern.events.debug[0]).to.equal('Calling `units(value)` but `value` is not a number (`string`)')
});

View file

@ -1106,19 +1106,26 @@ it("Should raise a warning when calling shiftAlong but distance is not a number"
it("Should raise a warning when calling shiftFractionalong but fraction is not a number", () => { it("Should raise a warning when calling shiftFractionalong but fraction is not a number", () => {
let invalid = false let invalid = false
const raise = { error: () => invalid = true } const raise = {
error: () => invalid = true,
warning: () => invalid = true,
}
expect(invalid).to.equal(false); expect(invalid).to.equal(false);
new freesewing.Path() new freesewing.Path()
.withRaise(raise) .withRaise(raise)
.move(new freesewing.Point(0,0)) .move(new freesewing.Point(0,0).withRaise(raise))
.line(new freesewing.Point(10,10)) .line(new freesewing.Point(10,10).withRaise(raise))
.line(new freesewing.Point(10,20).withRaise(raise))
.shiftFractionAlong() .shiftFractionAlong()
expect(invalid).to.equal(true); expect(invalid).to.equal(true);
}); });
it("Should raise a warning when splitting a path on a non-point", () => { it("Should raise a warning when splitting a path on a non-point", () => {
let invalid = false let invalid = false
const raise = { error: () => invalid = true } const raise = {
error: () => invalid = true,
warning: () => invalid = true,
}
const from = new freesewing.Point(0,0).withRaise(raise) const from = new freesewing.Point(0,0).withRaise(raise)
const cp1 = new freesewing.Point(10,0).withRaise(raise) const cp1 = new freesewing.Point(10,0).withRaise(raise)
const cp2 = new freesewing.Point(90,0).withRaise(raise) const cp2 = new freesewing.Point(90,0).withRaise(raise)
@ -1128,18 +1135,22 @@ it("Should raise a warning when splitting a path on a non-point", () => {
.move(from) .move(from)
.curve(cp1, cp2, to) .curve(cp1, cp2, to)
.line(from) .line(from)
.line(cp1)
try { try {
path.split() path.split()
} }
catch (err) { catch (err) {
console.log(err) expect(''+err).to.contain("TypeError: Cannot read property")
} }
expect(invalid).to.equal(true); expect(invalid).to.equal(true);
}); });
it("Should raise a warning when splitting a path on a non-point", () => { it("Should raise a warning when splitting a path on a non-point", () => {
let invalid = false let invalid = false
const raise = { error: () => invalid = true } const raise = {
error: () => invalid = true,
warning: () => invalid = true,
}
const from = new freesewing.Point(0,0).withRaise(raise) const from = new freesewing.Point(0,0).withRaise(raise)
const cp1 = new freesewing.Point(10,0).withRaise(raise) const cp1 = new freesewing.Point(10,0).withRaise(raise)
const cp2 = new freesewing.Point(90,0).withRaise(raise) const cp2 = new freesewing.Point(90,0).withRaise(raise)
@ -1153,8 +1164,7 @@ it("Should raise a warning when splitting a path on a non-point", () => {
path.split() path.split()
} }
catch (err) { catch (err) {
console.log(err) expect(''+err).to.contain("TypeError: Cannot read property")
} }
expect(invalid).to.equal(true); expect(invalid).to.equal(true);
}); });

View file

@ -435,3 +435,4 @@ it("Should correctly merge settings for existing array", () => {
expect(pattern.settings.only.length).to.equal(4); expect(pattern.settings.only.length).to.equal(4);
expect(pattern.settings.margin).to.equal(5); expect(pattern.settings.margin).to.equal(5);
}); });