chore(core): More unit tests
This commit is contained in:
parent
698fb7d942
commit
68c49c64ae
4 changed files with 329 additions and 3 deletions
|
@ -251,3 +251,55 @@ it("Should get the units closure to raise a debug when passing a non-number", ()
|
|||
expect(pattern.events.debug[0]).to.equal('Calling `units(value)` but `value` is not a number (`string`)')
|
||||
});
|
||||
|
||||
it("Should generate the part transforms", () => {
|
||||
let pattern = new freesewing.Pattern();
|
||||
pattern.settings.mode = "draft";
|
||||
let part = new pattern.Part();
|
||||
let short = part.shorthand();
|
||||
part.points.from = new short.Point(2, 2);
|
||||
part.points.to = new short.Point(19, 76);
|
||||
part.paths.test = new short.Path()
|
||||
.move(part.points.from)
|
||||
.line(part.points.to);
|
||||
part.stack();
|
||||
part.generateTransform({
|
||||
move: {
|
||||
x: 10,
|
||||
y: 20
|
||||
}
|
||||
})
|
||||
expect(part.attributes.list.transform.length).to.equal(1)
|
||||
expect(part.attributes.list.transform[0]).to.equal('translate(10,20)')
|
||||
expect(part.attributes.list['transform-origin'][0]).to.equal('10.5 39')
|
||||
});
|
||||
|
||||
it("Should set the part grain", () => {
|
||||
let pattern = new freesewing.Pattern();
|
||||
pattern.settings.mode = "draft";
|
||||
let part = new pattern.Part();
|
||||
part.setGrain(666)
|
||||
expect(part.attributes.list['data-grain'][0]).to.equal(666)
|
||||
part.setGrain()
|
||||
expect(part.attributes.list['data-grain'][0]).to.equal(90)
|
||||
});
|
||||
|
||||
it("Should set the part cut", () => {
|
||||
let pattern = new freesewing.Pattern();
|
||||
pattern.settings.mode = "draft";
|
||||
let part = new pattern.Part();
|
||||
const cut = {
|
||||
count: 4,
|
||||
mirror: false,
|
||||
onFold: true
|
||||
}
|
||||
const dflt = {
|
||||
count: 2,
|
||||
mirror: true,
|
||||
onFold: false
|
||||
}
|
||||
part.setCut(cut)
|
||||
expect(JSON.stringify(part.attributes.list['data-cut'][0])).to.equal(JSON.stringify(cut))
|
||||
part.setCut()
|
||||
expect(JSON.stringify(part.attributes.list['data-cut'][0])).to.equal(JSON.stringify(dflt))
|
||||
});
|
||||
|
||||
|
|
|
@ -139,7 +139,7 @@ it("Should throw an error if per-part draft method is missing", () => {
|
|||
it("Should sample an option", () => {
|
||||
let pattern = new freesewing.Pattern({
|
||||
options: {
|
||||
len: { pct: 30, min: 0, max: 100 },
|
||||
len: { pct: 30, min: 10 },
|
||||
bonus: 10
|
||||
}
|
||||
});
|
||||
|
@ -164,6 +164,122 @@ it("Should sample an option", () => {
|
|||
expect(pattern.parts.b.paths.test_10.ops[1].to.y).to.equal(10);
|
||||
});
|
||||
|
||||
it("Should sample a list option", () => {
|
||||
const Test = new freesewing.Design({
|
||||
name: "test",
|
||||
parts: ['front'],
|
||||
options: {
|
||||
len: {
|
||||
dflt: 1,
|
||||
list: [1,2,3]
|
||||
}
|
||||
},
|
||||
})
|
||||
Test.prototype.draftFront = function(part) {
|
||||
const { Point, points, Path, paths, options } = part.shorthand()
|
||||
points.from = new Point(0, 0);
|
||||
points.to = new Point( 100 * options.len, 0)
|
||||
paths.line = new Path()
|
||||
.move(points.from)
|
||||
.line(points.to)
|
||||
|
||||
return part
|
||||
};
|
||||
const pattern = new Test({
|
||||
sample: {
|
||||
type: 'option',
|
||||
option: 'len'
|
||||
}
|
||||
})
|
||||
pattern.sample();
|
||||
expect(pattern.parts.front.paths.line_1.ops[1].to.x).to.equal(100);
|
||||
expect(pattern.parts.front.paths.line_2.ops[1].to.x).to.equal(200);
|
||||
expect(pattern.parts.front.paths.line_3.ops[1].to.x).to.equal(300);
|
||||
});
|
||||
|
||||
it("Should sample a measurement", () => {
|
||||
const Test = new freesewing.Design({
|
||||
name: "test",
|
||||
parts: ['front'],
|
||||
measurements: ['head']
|
||||
})
|
||||
Test.prototype.draftFront = function(part) {
|
||||
const { Point, points, Path, paths, measurements } = part.shorthand()
|
||||
points.from = new Point(0, 0);
|
||||
points.to = new Point( measurements.head, 0)
|
||||
paths.line = new Path()
|
||||
.move(points.from)
|
||||
.line(points.to)
|
||||
|
||||
return part
|
||||
};
|
||||
const pattern = new Test({
|
||||
measurements: {
|
||||
head: 100
|
||||
},
|
||||
sample: {
|
||||
type: 'measurement',
|
||||
measurement: 'head'
|
||||
}
|
||||
})
|
||||
pattern.sample();
|
||||
expect(pattern.is).to.equal('sample')
|
||||
expect(pattern.events.debug[0]).to.equal('Sampling measurement `head`')
|
||||
for (let i=0;i<10;i++) {
|
||||
const j = i + 1
|
||||
expect(pattern.parts.front.paths[`line_${j}`].ops[1].to.x).to.equal(90 + 2*i);
|
||||
}
|
||||
pattern.sampleMeasurement('nope')
|
||||
expect(pattern.events.error.length).to.equal(1)
|
||||
expect(pattern.events.error[0]).to.equal("Cannot sample measurement `nope` because it's `undefined`")
|
||||
});
|
||||
|
||||
it("Should sample models", () => {
|
||||
const Test = new freesewing.Design({
|
||||
name: "test",
|
||||
parts: ['front'],
|
||||
measurements: ['head']
|
||||
})
|
||||
Test.prototype.draftFront = function(part) {
|
||||
const { Point, points, Path, paths, measurements } = part.shorthand()
|
||||
points.from = new Point(0, 0);
|
||||
points.to = new Point( measurements.head, 0)
|
||||
paths.line = new Path()
|
||||
.move(points.from)
|
||||
.line(points.to)
|
||||
|
||||
return part
|
||||
};
|
||||
let pattern = new Test({
|
||||
sample: {
|
||||
type: 'models',
|
||||
models : {
|
||||
a: { head: 100 },
|
||||
b: { head: 50 },
|
||||
}
|
||||
}
|
||||
})
|
||||
pattern.sample();
|
||||
expect(pattern.is).to.equal('sample')
|
||||
expect(pattern.events.debug[0]).to.equal('Sampling models')
|
||||
expect(pattern.parts.front.paths[`line_0`].ops[1].to.x).to.equal(100);
|
||||
expect(pattern.parts.front.paths[`line_1`].ops[1].to.x).to.equal(50);
|
||||
pattern = new Test({
|
||||
sample: {
|
||||
type: 'models',
|
||||
models : {
|
||||
a: { head: 100 },
|
||||
b: { head: 50 },
|
||||
},
|
||||
focus: 'b'
|
||||
}
|
||||
})
|
||||
pattern.sample();
|
||||
expect(pattern.is).to.equal('sample')
|
||||
expect(pattern.parts.front.paths[`line_-1`].ops[1].to.x).to.equal(50);
|
||||
expect(pattern.parts.front.paths[`line_0`].ops[1].to.x).to.equal(100);
|
||||
});
|
||||
|
||||
it("Should register a hook via on", () => {
|
||||
let pattern = new freesewing.Pattern();
|
||||
let count = 0;
|
||||
|
@ -402,6 +518,39 @@ it("Should correctly resolve dependencies - issue #971 - broken version", () =>
|
|||
expect(pattern.config.draftOrder[2]).to.equal('crotch');
|
||||
});
|
||||
|
||||
it("Should correctly resolve dependencies - Handle uncovered code path", () => {
|
||||
let config = {
|
||||
name: "test",
|
||||
dependencies: {
|
||||
front: 'side',
|
||||
crotch:['front','back'],
|
||||
back: 1,
|
||||
},
|
||||
inject: {
|
||||
front: 'back',
|
||||
},
|
||||
parts: ['back','front','crotch'],
|
||||
};
|
||||
const Test = function(settings = false) {
|
||||
freesewing.Pattern.call(this, config);
|
||||
return this;
|
||||
};
|
||||
Test.prototype = Object.create(freesewing.Pattern.prototype);
|
||||
Test.prototype.constructor = Test;
|
||||
Test.prototype.draftBack = function(part) {
|
||||
return part;
|
||||
};
|
||||
Test.prototype.draftFront = function(part) {
|
||||
return part;
|
||||
};
|
||||
|
||||
let pattern = new Test();
|
||||
const deps = pattern.resolveDependencies()
|
||||
expect(pattern.config.draftOrder[0]).to.equal('side');
|
||||
expect(pattern.config.draftOrder[1]).to.equal('back');
|
||||
expect(pattern.config.draftOrder[2]).to.equal('front');
|
||||
});
|
||||
|
||||
it("Should check whether created parts get the pattern context", () => {
|
||||
let pattern = new freesewing.Pattern();
|
||||
let part = new pattern.Part();
|
||||
|
@ -436,3 +585,130 @@ it("Should correctly merge settings for existing array", () => {
|
|||
expect(pattern.settings.margin).to.equal(5);
|
||||
});
|
||||
|
||||
it("Should return all render props", () => {
|
||||
const Test = new freesewing.Design({
|
||||
name: "test",
|
||||
parts: ['front'],
|
||||
});
|
||||
Test.prototype.draftFront = function(part) {
|
||||
return part;
|
||||
};
|
||||
const pattern = new Test()
|
||||
pattern.draft();
|
||||
const rp = pattern.getRenderProps()
|
||||
expect(rp.svg.body).to.equal('');
|
||||
expect(rp.width).to.equal(4);
|
||||
expect(rp.height).to.equal(4);
|
||||
expect(rp.parts.front.height).to.equal(4);
|
||||
});
|
||||
|
||||
it("Should not pack a pattern with errors", () => {
|
||||
const pattern = new freesewing.Pattern()
|
||||
pattern.events.error.push('error')
|
||||
pattern.pack()
|
||||
expect(pattern.events.warning.length).to.equal(1)
|
||||
expect(pattern.events.warning[0]).to.equal('One or more errors occured. Not packing pattern parts')
|
||||
});
|
||||
|
||||
it("Should handle custom layouts", () => {
|
||||
const Test = new freesewing.Design({ name: "test", parts: ['front'] })
|
||||
Test.prototype.draftFront = function(part) { return part }
|
||||
const pattern = new Test({
|
||||
layout: {
|
||||
width: 400,
|
||||
height: 200,
|
||||
parts: { front: { move: { x: 14, y: -202 } } }
|
||||
}
|
||||
})
|
||||
pattern.pack()
|
||||
expect(pattern.width).to.equal(400)
|
||||
expect(pattern.height).to.equal(200)
|
||||
});
|
||||
|
||||
it("Should handle a simple snapped option", () => {
|
||||
const Test = new freesewing.Design({
|
||||
name: "test",
|
||||
parts: ['front'],
|
||||
measurements: [ 'head' ],
|
||||
options: {
|
||||
len: { pct: 50, min: 22, max: 78, snap: 10, ...freesewing.pctBasedOn('head') }
|
||||
}
|
||||
})
|
||||
Test.prototype.draftFront = function(part) {
|
||||
const { Point, points, Path, paths, absoluteOptions } = part.shorthand()
|
||||
points.from = new Point(0, 0);
|
||||
points.to = new Point( 2 * absoluteOptions.len, 0)
|
||||
paths.line = new Path()
|
||||
.move(points.from)
|
||||
.line(points.to)
|
||||
|
||||
return part
|
||||
};
|
||||
let pattern = new Test({
|
||||
sample: {
|
||||
type: 'option',
|
||||
option: 'len'
|
||||
},
|
||||
measurements: {
|
||||
head: 43.23
|
||||
}
|
||||
})
|
||||
pattern.sample();
|
||||
expect(pattern.is).to.equal('sample')
|
||||
expect(pattern.events.debug[0]).to.equal('Sampling option `len`')
|
||||
expect(pattern.parts.front.paths.line_1.ops[1].to.x).to.equal(20);
|
||||
expect(pattern.parts.front.paths.line_2.ops[1].to.x).to.equal(40);
|
||||
expect(pattern.parts.front.paths.line_3.ops[1].to.x).to.equal(40);
|
||||
expect(pattern.parts.front.paths.line_4.ops[1].to.x).to.equal(40);
|
||||
expect(pattern.parts.front.paths.line_5.ops[1].to.x).to.equal(60);
|
||||
expect(pattern.parts.front.paths.line_6.ops[1].to.x).to.equal(60);
|
||||
expect(pattern.parts.front.paths.line_7.ops[1].to.x).to.equal(60);
|
||||
expect(pattern.parts.front.paths.line_8.ops[1].to.x).to.equal(60);
|
||||
expect(pattern.parts.front.paths.line_9.ops[1].to.x).to.equal(80);
|
||||
expect(pattern.parts.front.paths.line_10.ops[1].to.x).to.equal(80);
|
||||
});
|
||||
|
||||
it("Should handle a list snapped option", () => {
|
||||
const Test = new freesewing.Design({
|
||||
name: "test",
|
||||
parts: ['front'],
|
||||
measurements: [ 'head' ],
|
||||
options: {
|
||||
len: { pct: 50, min: 22, max: 78, snap: [10,14,19,28], ...freesewing.pctBasedOn('head') }
|
||||
}
|
||||
})
|
||||
Test.prototype.draftFront = function(part) {
|
||||
const { Point, points, Path, paths, absoluteOptions } = part.shorthand()
|
||||
points.from = new Point(0, 0);
|
||||
points.to = new Point( absoluteOptions.len, 0)
|
||||
paths.line = new Path()
|
||||
.move(points.from)
|
||||
.line(points.to)
|
||||
|
||||
return part
|
||||
};
|
||||
let pattern = new Test({
|
||||
sample: {
|
||||
type: 'option',
|
||||
option: 'len'
|
||||
},
|
||||
measurements: {
|
||||
head: 43.23
|
||||
}
|
||||
})
|
||||
pattern.sample();
|
||||
expect(pattern.is).to.equal('sample')
|
||||
expect(pattern.events.debug[0]).to.equal('Sampling option `len`')
|
||||
expect(pattern.parts.front.paths.line_1.ops[1].to.x).to.equal(10);
|
||||
expect(pattern.parts.front.paths.line_2.ops[1].to.x).to.equal(14);
|
||||
expect(pattern.parts.front.paths.line_3.ops[1].to.x).to.equal(14);
|
||||
expect(pattern.parts.front.paths.line_4.ops[1].to.x).to.equal(19);
|
||||
expect(pattern.parts.front.paths.line_5.ops[1].to.x).to.equal(19);
|
||||
expect(pattern.parts.front.paths.line_6.ops[1].to.x).to.equal(19);
|
||||
expect(pattern.parts.front.paths.line_7.ops[1].to.x).to.equal(28);
|
||||
expect(pattern.parts.front.paths.line_8.ops[1].to.x).to.equal(28);
|
||||
expect(pattern.parts.front.paths.line_9.ops[1].to.x).to.equal(28);
|
||||
expect(freesewing.utils.round(pattern.parts.front.paths.line_10.ops[1].to.x)).to.equal(33.72);
|
||||
});
|
||||
|
||||
|
||||
|
|
|
@ -260,7 +260,6 @@ it("Should raise a warning if rotating around what is not a point", () => {
|
|||
let invalid = false
|
||||
const raise = { warning: () => invalid = true }
|
||||
const p1 = new Point(10,10).withRaise(raise);
|
||||
const p2 = new Point(20, 20).withRaise(raise);
|
||||
expect(invalid).to.equal(false);
|
||||
try {
|
||||
p1.rotate(45, 'a')
|
||||
|
|
|
@ -544,7 +544,6 @@ it("Should shoulder return two methods for pctBasedOn", () => {
|
|||
const measurements = { chest: 1000 }
|
||||
expect(result.toAbs(0.0123, { measurements })).to.equal(12.3)
|
||||
expect(result.fromAbs(12.3, { measurements })).to.equal(0.0123)
|
||||
|
||||
});
|
||||
|
||||
it("Should generate a part transform", () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue