2022-09-12 20:10:22 +02:00
|
|
|
import chai from 'chai'
|
2022-09-14 15:02:39 +02:00
|
|
|
import { Design } from '../src/index.mjs'
|
2022-09-12 20:10:22 +02:00
|
|
|
|
|
|
|
const expect = chai.expect
|
|
|
|
|
|
|
|
describe('Stacks', () => {
|
2022-09-20 15:24:10 +02:00
|
|
|
describe('Pattern.__init()', () => {
|
2022-09-12 20:10:22 +02:00
|
|
|
const partA = {
|
|
|
|
name: 'test.partA',
|
|
|
|
measurements: ['head'],
|
|
|
|
options: {
|
|
|
|
size: { pct: 40, min: 20, max: 80 },
|
|
|
|
},
|
2022-09-13 17:56:01 +02:00
|
|
|
draft: ({ points, Point, paths, Path, part, store, measurements, options }) => {
|
2022-09-12 20:10:22 +02:00
|
|
|
store.set('size', measurements.head * options.size)
|
2022-09-13 17:56:01 +02:00
|
|
|
points.from = new Point(0, 0)
|
2022-09-12 20:10:22 +02:00
|
|
|
points.to = new Point(0, store.get('size'))
|
|
|
|
paths.line = new Path().move(points.from).line(points.to)
|
|
|
|
return part
|
|
|
|
},
|
|
|
|
stack: 'box',
|
|
|
|
}
|
|
|
|
const partB = {
|
|
|
|
name: 'test.partB',
|
|
|
|
measurements: ['head'],
|
|
|
|
after: partA,
|
2022-09-13 17:56:01 +02:00
|
|
|
draft: ({ points, Point, paths, Path, part, store }) => {
|
|
|
|
points.from = new Point(0, store.get('size'))
|
2022-09-12 20:10:22 +02:00
|
|
|
points.to = new Point(store.get('size'), store.get('size'))
|
|
|
|
paths.line = new Path().move(points.from).line(points.to)
|
|
|
|
return part
|
|
|
|
},
|
|
|
|
stack: 'box',
|
|
|
|
}
|
|
|
|
const partC = {
|
|
|
|
name: 'test.partC',
|
|
|
|
after: partB,
|
2022-09-13 17:56:01 +02:00
|
|
|
draft: ({ points, Point, paths, Path, part, store }) => {
|
2022-09-12 20:10:22 +02:00
|
|
|
points.from = new Point(store.get('size'), store.get('size'))
|
|
|
|
points.to = new Point(store.get('size'), 0)
|
|
|
|
paths.line = new Path().move(points.from).line(points.to)
|
|
|
|
return part
|
|
|
|
},
|
|
|
|
stack: 'box',
|
|
|
|
}
|
|
|
|
const partD = {
|
|
|
|
name: 'test.partD',
|
|
|
|
after: partC,
|
2022-09-13 17:56:01 +02:00
|
|
|
draft: ({ points, Point, paths, Path, part, store }) => {
|
2022-09-12 20:10:22 +02:00
|
|
|
points.from = new Point(store.get('size'), 0)
|
|
|
|
points.to = new Point(0, 0)
|
|
|
|
paths.line = new Path().move(points.from).line(points.to)
|
|
|
|
return part
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const Pattern = new Design({
|
|
|
|
data: {
|
|
|
|
name: 'test',
|
|
|
|
version: '1.2.3',
|
|
|
|
},
|
|
|
|
parts: [partD],
|
|
|
|
})
|
|
|
|
const pattern = new Pattern({
|
|
|
|
measurements: {
|
2022-09-13 17:56:01 +02:00
|
|
|
head: 400,
|
|
|
|
},
|
2022-09-12 20:10:22 +02:00
|
|
|
})
|
|
|
|
pattern.draft()
|
|
|
|
|
2022-09-20 15:24:10 +02:00
|
|
|
it('Pattern.__init() should resolve dependencies', () => {
|
2022-09-12 20:10:22 +02:00
|
|
|
expect(typeof pattern.config.resolvedDependencies).to.equal('object')
|
|
|
|
expect(Array.isArray(pattern.config.resolvedDependencies['test.partA'])).to.equal(true)
|
|
|
|
expect(pattern.config.resolvedDependencies['test.partA'].length).to.equal(0)
|
|
|
|
expect(Array.isArray(pattern.config.resolvedDependencies['test.partB'])).to.equal(true)
|
|
|
|
expect(pattern.config.resolvedDependencies['test.partB'].length).to.equal(1)
|
|
|
|
expect(pattern.config.resolvedDependencies['test.partB'][0]).to.equal('test.partA')
|
|
|
|
expect(Array.isArray(pattern.config.resolvedDependencies['test.partC'])).to.equal(true)
|
|
|
|
expect(pattern.config.resolvedDependencies['test.partC'].length).to.equal(2)
|
|
|
|
expect(
|
|
|
|
pattern.config.resolvedDependencies['test.partC'].indexOf('test.partA') !== -1
|
|
|
|
).to.equal(true)
|
|
|
|
expect(
|
|
|
|
pattern.config.resolvedDependencies['test.partC'].indexOf('test.partB') !== -1
|
|
|
|
).to.equal(true)
|
|
|
|
})
|
2022-09-14 12:24:09 +02:00
|
|
|
|
2022-09-14 15:04:24 +02:00
|
|
|
it('Should calculate the part boundary', () => {
|
|
|
|
const part = {
|
|
|
|
name: 'test',
|
|
|
|
draft: ({ points, Point, paths, Path, part }) => {
|
|
|
|
points.from = new Point(123, 456)
|
|
|
|
points.to = new Point(19, 76)
|
|
|
|
paths.test = new Path().move(points.from).line(points.to)
|
2022-09-14 12:24:09 +02:00
|
|
|
|
2022-09-14 15:04:24 +02:00
|
|
|
return part
|
|
|
|
},
|
2022-09-14 12:24:09 +02:00
|
|
|
}
|
2022-09-14 15:04:24 +02:00
|
|
|
const design = new Design({ parts: [part] })
|
|
|
|
const pattern = new design()
|
|
|
|
pattern.draft().render()
|
|
|
|
expect(pattern.stacks.test.topLeft.x).to.equal(17)
|
|
|
|
expect(pattern.stacks.test.topLeft.y).to.equal(74)
|
2022-09-19 23:35:52 +02:00
|
|
|
pattern.render()
|
2022-09-14 15:04:24 +02:00
|
|
|
expect(pattern.stacks.test.bottomRight.x).to.equal(125)
|
|
|
|
expect(pattern.stacks.test.bottomRight.y).to.equal(458)
|
|
|
|
expect(pattern.stacks.test.width).to.equal(108)
|
|
|
|
expect(pattern.stacks.test.height).to.equal(384)
|
|
|
|
})
|
2022-09-14 12:24:09 +02:00
|
|
|
|
2022-09-14 15:04:24 +02:00
|
|
|
it('Should calculate the part boundary with custom margin', () => {
|
|
|
|
const part = {
|
|
|
|
name: 'test',
|
|
|
|
draft: ({ points, Point, paths, Path, part }) => {
|
|
|
|
points.from = new Point(123, 456)
|
|
|
|
points.to = new Point(19, 76)
|
|
|
|
paths.test = new Path().move(points.from).line(points.to)
|
2022-09-14 12:24:09 +02:00
|
|
|
|
2022-09-14 15:04:24 +02:00
|
|
|
return part
|
|
|
|
},
|
2022-09-14 12:24:09 +02:00
|
|
|
}
|
2022-09-14 15:04:24 +02:00
|
|
|
const design = new Design({ parts: [part] })
|
|
|
|
const pattern = new design({ margin: 5 })
|
|
|
|
pattern.draft().render()
|
|
|
|
expect(pattern.stacks.test.topLeft.x).to.equal(14)
|
|
|
|
expect(pattern.stacks.test.topLeft.y).to.equal(71)
|
|
|
|
expect(pattern.stacks.test.bottomRight.x).to.equal(128)
|
|
|
|
expect(pattern.stacks.test.bottomRight.y).to.equal(461)
|
|
|
|
expect(pattern.stacks.test.width).to.equal(114)
|
|
|
|
expect(pattern.stacks.test.height).to.equal(390)
|
|
|
|
})
|
2022-09-14 12:24:09 +02:00
|
|
|
|
2022-09-14 15:04:24 +02:00
|
|
|
it('Should calculate the part boundary for paperless', () => {
|
|
|
|
const part = {
|
|
|
|
name: 'test',
|
|
|
|
draft: ({ points, Point, paths, Path, part }) => {
|
|
|
|
points.from = new Point(123, 456)
|
|
|
|
points.to = new Point(19, 76)
|
|
|
|
paths.test = new Path().move(points.from).line(points.to)
|
2022-09-14 12:24:09 +02:00
|
|
|
|
2022-09-14 15:04:24 +02:00
|
|
|
return part
|
|
|
|
},
|
2022-09-14 12:24:09 +02:00
|
|
|
}
|
2022-09-14 15:04:24 +02:00
|
|
|
const design = new Design({ parts: [part] })
|
|
|
|
const pattern = new design({ paperless: true })
|
|
|
|
pattern.draft().render()
|
|
|
|
expect(pattern.stacks.test.topLeft.x).to.equal(9)
|
|
|
|
expect(pattern.stacks.test.topLeft.y).to.equal(66)
|
|
|
|
})
|
2022-09-19 23:35:52 +02:00
|
|
|
|
|
|
|
it('Should generate the stack transforms', () => {
|
2022-09-14 15:04:24 +02:00
|
|
|
const part = {
|
|
|
|
name: 'test',
|
|
|
|
draft: ({ points, Point, paths, Path, part }) => {
|
|
|
|
points.from = new Point(2, 2)
|
|
|
|
points.to = new Point(19, 76)
|
|
|
|
paths.test = new Path().move(points.from).line(points.to)
|
2022-09-14 12:24:09 +02:00
|
|
|
|
2022-09-14 15:04:24 +02:00
|
|
|
return part
|
|
|
|
},
|
2022-09-14 12:24:09 +02:00
|
|
|
}
|
2022-09-14 15:04:24 +02:00
|
|
|
const design = new Design({ parts: [part] })
|
|
|
|
const pattern = new design()
|
|
|
|
pattern.draft().render()
|
|
|
|
pattern.stacks.test.generateTransform({
|
|
|
|
move: {
|
|
|
|
x: 10,
|
|
|
|
y: 20,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(pattern.stacks.test.attributes.list.transform.length).to.equal(1)
|
|
|
|
expect(pattern.stacks.test.attributes.list.transform[0]).to.equal('translate(10 20)')
|
2022-09-14 12:24:09 +02:00
|
|
|
})
|
2022-09-12 20:10:22 +02:00
|
|
|
})
|
2022-09-19 23:35:52 +02:00
|
|
|
|
|
|
|
it('Should get the anchor for the stack', () => {
|
|
|
|
const part = {
|
|
|
|
name: 'test',
|
2022-09-25 09:29:41 +02:00
|
|
|
draft: ({ points, Point, part }) => {
|
2022-09-19 23:35:52 +02:00
|
|
|
points.anchor = new Point(2, 2)
|
|
|
|
|
|
|
|
return part
|
|
|
|
},
|
|
|
|
}
|
|
|
|
const design = new Design({ parts: [part] })
|
|
|
|
const pattern = new design()
|
|
|
|
pattern.draft().render()
|
|
|
|
const anchor = pattern.stacks.test.getAnchor()
|
|
|
|
expect(anchor.name).to.equal('anchor')
|
|
|
|
expect(anchor.x).to.equal(2)
|
|
|
|
expect(anchor.y).to.equal(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should get the gridAnchor for the stack', () => {
|
|
|
|
const part = {
|
|
|
|
name: 'test',
|
2022-09-25 09:29:41 +02:00
|
|
|
draft: ({ points, Point, part }) => {
|
2022-09-19 23:35:52 +02:00
|
|
|
points.gridAnchor = new Point(3, 3)
|
|
|
|
|
|
|
|
return part
|
|
|
|
},
|
|
|
|
}
|
|
|
|
const design = new Design({ parts: [part] })
|
|
|
|
const pattern = new design()
|
|
|
|
pattern.draft().render()
|
|
|
|
const anchor = pattern.stacks.test.getAnchor()
|
|
|
|
expect(anchor.name).to.equal('gridAnchor')
|
|
|
|
expect(anchor.x).to.equal(3)
|
|
|
|
expect(anchor.y).to.equal(3)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should get the default aAnchor for the stack', () => {
|
|
|
|
const part = {
|
|
|
|
name: 'test',
|
2022-09-25 09:29:41 +02:00
|
|
|
draft: ({ points, Point, part }) => {
|
2022-09-19 23:35:52 +02:00
|
|
|
points.test = new Point(3, 3)
|
|
|
|
|
|
|
|
return part
|
|
|
|
},
|
|
|
|
}
|
|
|
|
const design = new Design({ parts: [part] })
|
|
|
|
const pattern = new design()
|
|
|
|
pattern.draft().render()
|
|
|
|
const anchor = pattern.stacks.test.getAnchor()
|
|
|
|
expect(anchor.x).to.equal(0)
|
|
|
|
expect(anchor.y).to.equal(0)
|
|
|
|
})
|
2022-09-12 20:10:22 +02:00
|
|
|
})
|