2022-09-09 20:20:38 +02:00
|
|
|
import chai from 'chai'
|
2022-09-10 16:00:43 +02:00
|
|
|
import { Design, Pattern, Path } from '../src/index.mjs'
|
2022-08-25 11:47:54 +02:00
|
|
|
|
|
|
|
const expect = chai.expect
|
2018-08-12 17:02:49 +02:00
|
|
|
|
2022-08-17 14:26:45 -05:00
|
|
|
describe('Part', () => {
|
2022-09-10 19:34:43 +02:00
|
|
|
it('Shorthand should contain the part itself', () => {
|
|
|
|
let dp
|
|
|
|
const part = {
|
|
|
|
name: 'test',
|
|
|
|
draft: ({ part }) => {
|
|
|
|
dp = part
|
|
|
|
return part
|
2022-09-11 18:00:41 +02:00
|
|
|
},
|
2022-09-10 19:34:43 +02:00
|
|
|
}
|
2022-09-11 18:00:41 +02:00
|
|
|
const design = new Design({ parts: [part] })
|
2022-09-10 19:34:43 +02:00
|
|
|
const pattern = new design()
|
|
|
|
pattern.draft()
|
|
|
|
expect(typeof dp).to.equal('object')
|
|
|
|
expect(typeof dp.context).to.equal('object')
|
|
|
|
})
|
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should return a function from macroClosure', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const pattern = new Pattern()
|
|
|
|
const part = pattern.__createPartWithContext()
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(typeof part.macroClosure()).to.equal('function')
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should not run an unknown macro', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const pattern = new Pattern()
|
|
|
|
const part = pattern.__createPartWithContext()
|
|
|
|
const macro = part.macroClosure()
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(macro('unknown')).to.equal(undefined)
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should register and run a macro', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const pattern = new Pattern()
|
|
|
|
const plugin = {
|
2022-09-09 20:20:38 +02:00
|
|
|
name: 'test',
|
|
|
|
version: '0.1-test',
|
2022-08-27 09:27:07 +02:00
|
|
|
macros: {
|
2022-09-09 20:20:38 +02:00
|
|
|
test: function (so) {
|
|
|
|
let points = this.points
|
|
|
|
points.macro = new this.Point(so.x, so.y)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
pattern.use(plugin)
|
2022-09-10 16:00:43 +02:00
|
|
|
const part = pattern.__createPartWithContext()
|
|
|
|
const macro = part.macroClosure()
|
2022-09-09 20:20:38 +02:00
|
|
|
macro('test', { x: 123, y: 456 })
|
|
|
|
expect(part.points.macro.x).to.equal(123)
|
|
|
|
expect(part.points.macro.y).to.equal(456)
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should return a free ID', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const pattern = new Pattern()
|
|
|
|
const part = pattern.__createPartWithContext()
|
|
|
|
const free = part.getId()
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(part.getId()).to.equal('' + (parseInt(free) + 1))
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should return a function from unitsClosure', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const pattern = new Pattern()
|
|
|
|
const part = pattern.__createPartWithContext()
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(typeof part.unitsClosure()).to.equal('function')
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should convert units', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design()
|
|
|
|
const part = pattern.__createPartWithContext()
|
|
|
|
const units = part.unitsClosure()
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(units(123.456)).to.equal('12.35cm')
|
|
|
|
expect(part.units(123.456)).to.equal('12.35cm')
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should set part attributes', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const pattern = new Pattern()
|
|
|
|
const part = pattern.__createPartWithContext()
|
2022-09-09 20:20:38 +02:00
|
|
|
part.attr('foo', 'bar')
|
|
|
|
expect(part.attributes.get('foo')).to.equal('bar')
|
|
|
|
part.attr('foo', 'baz')
|
|
|
|
expect(part.attributes.get('foo')).to.equal('bar baz')
|
|
|
|
part.attr('foo', 'schmoo', true)
|
|
|
|
expect(part.attributes.get('foo')).to.equal('schmoo')
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should raise a warning when setting a non-Point value in points', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design()
|
|
|
|
const part = pattern.__createPartWithContext()
|
|
|
|
pattern.init()
|
2022-08-27 09:27:07 +02:00
|
|
|
const { points } = part.shorthand()
|
|
|
|
points.a = 'banana'
|
2022-09-10 16:00:43 +02:00
|
|
|
expect(pattern.store.logs.warning.length).to.equal(4)
|
|
|
|
expect(pattern.store.logs.warning[0]).to.equal(
|
2022-09-09 20:20:38 +02:00
|
|
|
'`points.a` was set with a value that is not a `Point` object'
|
|
|
|
)
|
2022-09-10 16:00:43 +02:00
|
|
|
expect(pattern.store.logs.warning[1]).to.equal(
|
2022-09-09 20:20:38 +02:00
|
|
|
'`points.a` was set with a `x` parameter that is not a `number`'
|
|
|
|
)
|
2022-09-10 16:00:43 +02:00
|
|
|
expect(pattern.store.logs.warning[2]).to.equal(
|
2022-09-09 20:20:38 +02:00
|
|
|
'`points.a` was set with a `y` parameter that is not a `number`'
|
|
|
|
)
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should raise a warning when setting a non-Snippet value in snippets', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design()
|
|
|
|
const part = pattern.__createPartWithContext()
|
|
|
|
pattern.init()
|
2022-08-27 09:27:07 +02:00
|
|
|
const { snippets } = part.shorthand()
|
|
|
|
snippets.a = 'banana'
|
2022-09-10 16:00:43 +02:00
|
|
|
expect(pattern.store.logs.warning.length).to.equal(4)
|
|
|
|
expect(pattern.store.logs.warning[0]).to.equal(
|
2022-09-09 20:20:38 +02:00
|
|
|
'`snippets.a` was set with a value that is not a `Snippet` object'
|
|
|
|
)
|
2022-09-10 16:00:43 +02:00
|
|
|
expect(pattern.store.logs.warning[1]).to.equal(
|
2022-09-09 20:20:38 +02:00
|
|
|
'`snippets.a` was set with a `def` parameter that is not a `string`'
|
|
|
|
)
|
2022-09-10 16:00:43 +02:00
|
|
|
expect(pattern.store.logs.warning[2]).to.equal(
|
2022-09-09 20:20:38 +02:00
|
|
|
'`snippets.a` was set with an `anchor` parameter that is not a `Point`'
|
|
|
|
)
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should calculate the part boundary with default margin', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design()
|
|
|
|
const part = pattern.__createPartWithContext()
|
|
|
|
pattern.init()
|
|
|
|
const short = part.shorthand()
|
2022-09-09 20:20:38 +02:00
|
|
|
part.points.from = new short.Point(123, 456)
|
|
|
|
part.points.to = new short.Point(19, 76)
|
|
|
|
part.paths.test = new short.Path().move(part.points.from).line(part.points.to)
|
|
|
|
let boundary = part.boundary()
|
|
|
|
expect(boundary.topLeft.x).to.equal(17)
|
|
|
|
expect(boundary.topLeft.y).to.equal(74)
|
|
|
|
expect(boundary.bottomRight.x).to.equal(125)
|
|
|
|
expect(boundary.bottomRight.y).to.equal(458)
|
|
|
|
boundary = part.boundary()
|
|
|
|
expect(boundary.width).to.equal(108)
|
|
|
|
expect(boundary.height).to.equal(384)
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should calculate the part boundary with custom margin', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design({ margin: 5 })
|
|
|
|
const part = pattern.__createPartWithContext()
|
|
|
|
pattern.init()
|
|
|
|
const short = part.shorthand()
|
2022-09-09 20:20:38 +02:00
|
|
|
part.points.from = new short.Point(123, 456)
|
|
|
|
part.points.to = new short.Point(19, 76)
|
|
|
|
part.paths.test = new short.Path().move(part.points.from).line(part.points.to)
|
|
|
|
let boundary = part.boundary()
|
|
|
|
expect(boundary.topLeft.x).to.equal(14)
|
|
|
|
expect(boundary.topLeft.y).to.equal(71)
|
|
|
|
expect(boundary.bottomRight.x).to.equal(128)
|
|
|
|
expect(boundary.bottomRight.y).to.equal(461)
|
|
|
|
boundary = part.boundary()
|
|
|
|
expect(boundary.width).to.equal(114)
|
|
|
|
expect(boundary.height).to.equal(390)
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should calculate the part boundary for paperless', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design({ paperless: true })
|
|
|
|
const part = pattern.__createPartWithContext()
|
|
|
|
pattern.init()
|
|
|
|
const short = part.shorthand()
|
2022-09-09 20:20:38 +02:00
|
|
|
part.points.from = new short.Point(123, 456)
|
|
|
|
part.points.to = new short.Point(19, 76)
|
|
|
|
part.paths.test = new short.Path().move(part.points.from).line(part.points.to)
|
|
|
|
let boundary = part.boundary()
|
|
|
|
expect(boundary.topLeft.x).to.equal(9)
|
|
|
|
expect(boundary.topLeft.y).to.equal(66)
|
|
|
|
expect(boundary.bottomRight.x).to.equal(133)
|
|
|
|
expect(boundary.bottomRight.y).to.equal(466)
|
|
|
|
boundary = part.boundary()
|
|
|
|
expect(boundary.width).to.equal(124)
|
|
|
|
expect(boundary.height).to.equal(400)
|
|
|
|
})
|
2022-09-11 18:00:41 +02:00
|
|
|
/*
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should stack a part', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const part = {
|
|
|
|
name: 'test',
|
|
|
|
draft: (part) => {
|
|
|
|
const { points, Point, paths, Path } = part.shorthand()
|
|
|
|
points.from = new Point(123, 456)
|
|
|
|
points.to = new Point(19, 76)
|
|
|
|
paths.test = new Path().move(points.from).line(points.to)
|
|
|
|
return aprt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const design = new Design({ parts: [ part ]})
|
|
|
|
const pattern = new design({ paperless: true })
|
|
|
|
pattern.draft()
|
2022-09-12 20:10:22 +02:00
|
|
|
pattern.parts.test.home()
|
2022-09-10 16:00:43 +02:00
|
|
|
console.log(pattern.parts.test.attributes)
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(part.attributes.get('transform')).to.equal('translate(-17, -74)')
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should only stack a part if needed', () => {
|
|
|
|
let pattern = new 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)
|
2022-09-12 20:10:22 +02:00
|
|
|
part.home()
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(part.attributes.get('transform')).to.equal(false)
|
2022-09-12 20:10:22 +02:00
|
|
|
part.home()
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(part.attributes.get('transform')).to.equal(false)
|
|
|
|
})
|
2022-09-10 16:00:43 +02:00
|
|
|
*/
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should run hooks', () => {
|
2022-08-27 09:27:07 +02:00
|
|
|
let count = 0
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design({ paperless: true })
|
|
|
|
const part = pattern.__createPartWithContext()
|
2022-09-09 20:20:38 +02:00
|
|
|
part.hooks.preDraft = [
|
|
|
|
{
|
|
|
|
method: function (p) {
|
|
|
|
count++
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
2022-08-27 09:27:07 +02:00
|
|
|
part.runHooks('preDraft')
|
2022-09-09 20:20:38 +02:00
|
|
|
expect(count).to.equal(1)
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should get the units closure to raise a debug when passing a non-number', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design({ margin: 5 })
|
|
|
|
const part = pattern.__createPartWithContext()
|
|
|
|
pattern.init()
|
2022-09-09 20:20:38 +02:00
|
|
|
const short = part.shorthand()
|
2022-08-27 09:27:07 +02:00
|
|
|
short.units('a')
|
2022-09-10 16:00:43 +02:00
|
|
|
expect(pattern.store.logs.warning.length).to.equal(1)
|
|
|
|
expect(pattern.store.logs.warning[0]).to.equal(
|
2022-09-09 20:20:38 +02:00
|
|
|
'Calling `units(value)` but `value` is not a number (`string`)'
|
|
|
|
)
|
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should generate the part transforms', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design({ margin: 5 })
|
|
|
|
const part = pattern.__createPartWithContext()
|
|
|
|
pattern.init()
|
2022-09-09 20:20:38 +02:00
|
|
|
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)
|
2022-09-12 20:10:22 +02:00
|
|
|
part.home()
|
2022-08-27 09:27:07 +02:00
|
|
|
part.generateTransform({
|
|
|
|
move: {
|
|
|
|
x: 10,
|
2022-09-09 20:20:38 +02:00
|
|
|
y: 20,
|
|
|
|
},
|
2022-08-27 09:27:07 +02:00
|
|
|
})
|
|
|
|
expect(part.attributes.list.transform.length).to.equal(1)
|
|
|
|
expect(part.attributes.list.transform[0]).to.equal('translate(10 20)')
|
2022-09-09 20:20:38 +02:00
|
|
|
})
|
2022-08-27 09:27:07 +02:00
|
|
|
describe('isEmpty', () => {
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should return true if the part has no paths or snippets', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design()
|
|
|
|
const part = pattern.__createPartWithContext()
|
2022-08-27 09:27:07 +02:00
|
|
|
expect(part.isEmpty()).to.be.true
|
|
|
|
})
|
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should return true if the part has paths but they have no length', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design()
|
|
|
|
const part = pattern.__createPartWithContext()
|
2022-09-09 20:20:38 +02:00
|
|
|
const { Path, paths, Point } = part.shorthand()
|
2022-08-27 09:27:07 +02:00
|
|
|
paths.seam = new Path()
|
|
|
|
expect(part.isEmpty()).to.be.true
|
|
|
|
})
|
|
|
|
|
|
|
|
it("Should return true if the part has paths but they don't render", () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design()
|
|
|
|
const part = pattern.__createPartWithContext()
|
2022-09-09 20:20:38 +02:00
|
|
|
const { Path, paths, Point } = part.shorthand()
|
|
|
|
paths.seam = new Path().move(new Point(0, 0)).line(new Point(2, 3)).setRender(false)
|
2022-08-27 09:27:07 +02:00
|
|
|
expect(part.isEmpty()).to.be.true
|
|
|
|
})
|
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should return false if the part has a path with length', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design()
|
|
|
|
const part = pattern.__createPartWithContext()
|
2022-09-09 20:20:38 +02:00
|
|
|
const { Path, paths, Point } = part.shorthand()
|
|
|
|
paths.seam = new Path().move(new Point(0, 0)).line(new Point(2, 3))
|
2022-08-27 09:27:07 +02:00
|
|
|
|
|
|
|
expect(part.isEmpty()).to.be.false
|
|
|
|
})
|
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should return false if the part has a snippet', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design()
|
|
|
|
const part = pattern.__createPartWithContext()
|
2022-09-09 20:20:38 +02:00
|
|
|
const { Point, snippets, Snippet } = part.shorthand()
|
|
|
|
snippets.test = new Snippet('test', new Point(0, 0))
|
2022-08-27 09:27:07 +02:00
|
|
|
|
|
|
|
expect(part.isEmpty()).to.be.false
|
|
|
|
})
|
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should return false if the part has a point that has text', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design()
|
|
|
|
const part = pattern.__createPartWithContext()
|
2022-09-09 20:20:38 +02:00
|
|
|
const { Point, points } = part.shorthand()
|
|
|
|
points.test = new Point(0, 0)
|
2022-08-27 09:27:07 +02:00
|
|
|
points.test.attributes.set('data-text', 'text')
|
|
|
|
expect(part.isEmpty()).to.be.false
|
|
|
|
})
|
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should return false if the part has a point that has a circle', () => {
|
2022-09-10 16:00:43 +02:00
|
|
|
const design = new Design()
|
|
|
|
const pattern = new design()
|
|
|
|
const part = pattern.__createPartWithContext()
|
2022-09-09 20:20:38 +02:00
|
|
|
const { Point, points } = part.shorthand()
|
|
|
|
points.test = new Point(0, 0)
|
2022-08-27 09:27:07 +02:00
|
|
|
points.test.attributes.set('data-circle', 10)
|
|
|
|
expect(part.isEmpty()).to.be.false
|
|
|
|
})
|
2022-08-24 13:36:19 -05:00
|
|
|
})
|
2022-08-17 14:26:45 -05:00
|
|
|
})
|