1
0
Fork 0
freesewing/packages/core/tests/pattern-sample.test.mjs

190 lines
5.5 KiB
JavaScript
Raw Normal View History

import chai from 'chai'
2022-09-25 09:29:41 +02:00
import { round, Design } from '../src/index.mjs'
const expect = chai.expect
describe('Pattern', () => {
describe('Pattern.sample()', () => {
it('Should sample an option', () => {
2022-09-19 23:35:52 +02:00
const part = {
name: 'test',
measurements: ['head'],
options: {
2022-09-19 23:35:52 +02:00
size: { pct: 50, min: 20, max: 80 },
},
draft: ({ Point, paths, Path, measurements, options, part }) => {
paths.test = new Path()
2022-09-25 09:29:41 +02:00
.move(new Point(0, 0))
2022-09-19 23:35:52 +02:00
.line(new Point(0, measurements.head * options.size))
return part
},
2022-09-19 23:35:52 +02:00
}
const Pattern = new Design({ parts: [part] })
const pattern = new Pattern({
measurements: { head: 400 },
sample: {
type: 'option',
2022-09-25 09:29:41 +02:00
option: 'size',
},
})
2022-09-19 23:35:52 +02:00
pattern.sample()
expect(pattern.setStores.length).to.equal(10)
2022-09-19 23:35:52 +02:00
expect(pattern.settings.length).to.equal(10)
expect(pattern.parts[9].test.paths.test.ops[1].to.y).to.equal(320)
})
it('Should sample a static option', () => {
const part = {
name: 'test',
measurements: ['head'],
options: {
size: 0.05,
},
draft: ({ Point, paths, Path, measurements, options, part }) => {
paths.test = new Path()
2022-09-25 09:29:41 +02:00
.move(new Point(0, 0))
2022-09-19 23:35:52 +02:00
.line(new Point(0, measurements.head * options.size))
return part
},
}
2022-09-19 23:35:52 +02:00
const Pattern = new Design({ parts: [part] })
const pattern = new Pattern({
measurements: { head: 400 },
sample: {
type: 'option',
2022-09-25 09:29:41 +02:00
option: 'size',
},
2022-09-19 23:35:52 +02:00
})
pattern.sample()
expect(pattern.setStores.length).to.equal(10)
2022-09-19 23:35:52 +02:00
expect(pattern.settings.length).to.equal(10)
expect(round(pattern.parts[9].test.paths.test.ops[1].to.y)).to.equal(22)
})
it('Should sample a list option', () => {
const part = {
name: 'test',
measurements: ['head'],
options: {
2022-09-25 09:29:41 +02:00
size: { dflt: 5, list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] },
2022-09-19 23:35:52 +02:00
},
draft: ({ Point, paths, Path, measurements, options, part }) => {
paths.test = new Path()
2022-09-25 09:29:41 +02:00
.move(new Point(0, 0))
.line(new Point(0, (measurements.head * options.size) / 10))
2022-09-19 23:35:52 +02:00
return part
},
}
2022-09-19 23:35:52 +02:00
const Pattern = new Design({ parts: [part] })
const pattern = new Pattern({
measurements: { head: 400 },
sample: {
type: 'option',
2022-09-25 09:29:41 +02:00
option: 'size',
},
2022-09-19 23:35:52 +02:00
})
pattern.sample()
expect(pattern.setStores.length).to.equal(10)
2022-09-19 23:35:52 +02:00
expect(pattern.settings.length).to.equal(10)
expect(pattern.parts[9].test.paths.test.ops[1].to.y).to.equal(400)
})
2022-09-19 23:35:52 +02:00
it('Should sample a measurement', () => {
const part = {
name: 'test',
measurements: ['head'],
options: {
size: { pct: 50, min: 20, max: 80 },
},
draft: ({ Point, paths, Path, measurements, options, part }) => {
paths.test = new Path()
2022-09-25 09:29:41 +02:00
.move(new Point(0, 0))
2022-09-19 23:35:52 +02:00
.line(new Point(0, measurements.head * options.size))
return part
},
}
const Pattern = new Design({ parts: [part] })
const pattern = new Pattern({
measurements: { head: 400 },
sample: {
type: 'measurement',
2022-09-25 09:29:41 +02:00
measurement: 'head',
},
2022-09-19 23:35:52 +02:00
})
pattern.sample()
expect(pattern.setStores.length).to.equal(10)
2022-09-19 23:35:52 +02:00
expect(pattern.settings.length).to.equal(10)
expect(pattern.parts[9].test.paths.test.ops[1].to.y).to.equal(216)
})
it('Should log an error when sampling an undefined measurement', () => {
const part = {
name: 'test',
measurements: ['head'],
options: {
size: { pct: 50, min: 20, max: 80 },
},
draft: ({ Point, paths, Path, measurements, options, part }) => {
paths.test = new Path()
2022-09-25 09:29:41 +02:00
.move(new Point(0, 0))
2022-09-19 23:35:52 +02:00
.line(new Point(0, measurements.head * options.size))
return part
},
}
const Pattern = new Design({ parts: [part] })
const pattern = new Pattern({
2022-09-25 09:29:41 +02:00
measurements: {},
2022-09-19 23:35:52 +02:00
sample: {
type: 'measurement',
2022-09-25 09:29:41 +02:00
measurement: 'head',
},
2022-09-19 23:35:52 +02:00
})
pattern.sample()
expect(pattern.store.logs.error.length).to.equal(1)
2022-09-25 09:29:41 +02:00
expect(pattern.store.logs.error[0]).to.equal(
"Cannot sample measurement `head` because it's `undefined`"
)
2022-09-19 23:35:52 +02:00
})
it('Should sample models', () => {
const part = {
name: 'test',
measurements: ['head'],
options: {
size: { pct: 50, min: 20, max: 80 },
},
draft: ({ Point, paths, Path, measurements, options, part }) => {
paths.test = new Path()
2022-09-25 09:29:41 +02:00
.move(new Point(0, 0))
2022-09-19 23:35:52 +02:00
.line(new Point(0, measurements.head * options.size))
return part
},
}
const Pattern = new Design({ parts: [part] })
const pattern = new Pattern({
measurements: { head: 400 },
sample: {
type: 'models',
models: {
a: { head: 100 },
b: { head: 200 },
c: { head: 300 },
d: { head: 400 },
},
2022-09-25 09:29:41 +02:00
focus: 'c',
},
2022-09-19 23:35:52 +02:00
})
pattern.sample()
expect(pattern.setStores.length).to.equal(4)
2022-09-19 23:35:52 +02:00
expect(pattern.settings.length).to.equal(4)
expect(pattern.parts[3].test.paths.test.ops[1].to.y).to.equal(200)
})
})
})