291 lines
9.1 KiB
JavaScript
291 lines
9.1 KiB
JavaScript
import chai from 'chai'
|
|
import { Design, Part } from '../src/index.mjs'
|
|
|
|
const expect = chai.expect
|
|
|
|
describe('Part', () => {
|
|
it('Shorthand should contain the part itself', () => {
|
|
let dp
|
|
const part = {
|
|
name: 'test',
|
|
draft: ({ part }) => {
|
|
dp = part
|
|
return part
|
|
},
|
|
}
|
|
const design = new Design({ parts: [part] })
|
|
const pattern = new design()
|
|
pattern.draft()
|
|
expect(typeof dp).to.equal('object')
|
|
expect(typeof dp.context).to.equal('object')
|
|
})
|
|
|
|
it('Should return a function from __macroClosure', () => {
|
|
const part = new Part()
|
|
expect(typeof part.__macroClosure()).to.equal('function')
|
|
})
|
|
|
|
it('Should not run an unknown macro', () => {
|
|
const part = new Part()
|
|
const macro = part.__macroClosure()
|
|
expect(macro('unknown')).to.equal(undefined)
|
|
})
|
|
|
|
it('Should register and run a macro', () => {
|
|
const part = {
|
|
name: 'test',
|
|
draft: ({ part, macro }) => {
|
|
macro('test', { x: 123, y: 456 })
|
|
return part
|
|
},
|
|
}
|
|
const plugin = {
|
|
name: 'test',
|
|
version: '0.1-test',
|
|
macros: {
|
|
test: function (so) {
|
|
let points = this.points
|
|
points.macro = new this.Point(so.x, so.y)
|
|
},
|
|
},
|
|
}
|
|
const design = new Design({ parts: [part], plugins: [plugin] })
|
|
const pattern = new design()
|
|
pattern.draft()
|
|
expect(pattern.parts[0].test.points.macro.x).to.equal(123)
|
|
expect(pattern.parts[0].test.points.macro.y).to.equal(456)
|
|
})
|
|
|
|
it('Should return a free ID', () => {
|
|
const part = new Part()
|
|
const free = part.getId()
|
|
expect(part.getId()).to.equal('' + (parseInt(free) + 1))
|
|
})
|
|
|
|
it('Should return a function from __unitsClosure', () => {
|
|
const part = new Part()
|
|
expect(typeof part.__unitsClosure()).to.equal('function')
|
|
})
|
|
|
|
it('Should convert units', () => {
|
|
const part = new Part()
|
|
part.context = { settings: { units: 'metric' } }
|
|
const units = part.__unitsClosure()
|
|
expect(units(123.456)).to.equal('12.35cm')
|
|
expect(units(123.456)).to.equal('12.35cm')
|
|
})
|
|
|
|
it('Should set part attributes', () => {
|
|
const part = new Part()
|
|
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')
|
|
})
|
|
|
|
it('Should raise a warning when setting a non-Point value in points', () => {
|
|
const part = {
|
|
name: 'test',
|
|
draft: ({ points, part }) => {
|
|
points.a = 'banana'
|
|
return part
|
|
},
|
|
}
|
|
const design = new Design({ parts: [part] })
|
|
const pattern = new design()
|
|
pattern.draft()
|
|
expect(pattern.stores[0].logs.warning.length).to.equal(4)
|
|
expect(pattern.stores[0].logs.warning[0]).to.equal(
|
|
'`points.a` was set with a value that is not a `Point` object'
|
|
)
|
|
expect(pattern.stores[0].logs.warning[1]).to.equal(
|
|
'`points.a` was set with a `x` parameter that is not a `number`'
|
|
)
|
|
expect(pattern.stores[0].logs.warning[2]).to.equal(
|
|
'`points.a` was set with a `y` parameter that is not a `number`'
|
|
)
|
|
})
|
|
|
|
it('Should raise a warning when setting a non-Snippet value in snippets', () => {
|
|
const part = {
|
|
name: 'test',
|
|
draft: ({ snippets, part }) => {
|
|
snippets.a = 'banana'
|
|
return part
|
|
},
|
|
}
|
|
const design = new Design({ parts: [part] })
|
|
const pattern = new design()
|
|
pattern.draft()
|
|
expect(pattern.stores[0].logs.warning.length).to.equal(4)
|
|
expect(pattern.stores[0].logs.warning[0]).to.equal(
|
|
'`snippets.a` was set with a value that is not a `Snippet` object'
|
|
)
|
|
expect(pattern.stores[0].logs.warning[1]).to.equal(
|
|
'`snippets.a` was set with a `def` parameter that is not a `string`'
|
|
)
|
|
expect(pattern.stores[0].logs.warning[2]).to.equal(
|
|
'`snippets.a` was set with an `anchor` parameter that is not a `Point`'
|
|
)
|
|
})
|
|
|
|
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)
|
|
return part
|
|
},
|
|
}
|
|
const design = new Design({ parts: [part] })
|
|
const pattern = new design()
|
|
pattern.draft()
|
|
const boundary = pattern.parts[0].test.__boundary()
|
|
const { topLeft, bottomRight, width, height } = boundary
|
|
expect(topLeft.x).to.equal(19)
|
|
expect(topLeft.y).to.equal(76)
|
|
expect(bottomRight.x).to.equal(123)
|
|
expect(bottomRight.y).to.equal(456)
|
|
expect(width).to.equal(104)
|
|
expect(height).to.equal(380)
|
|
})
|
|
|
|
/*
|
|
it('Should stack a part', () => {
|
|
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()
|
|
pattern.parts.test.home()
|
|
console.log(pattern.parts.test.attributes)
|
|
expect(part.attributes.get('transform')).to.equal('translate(-17, -74)')
|
|
})
|
|
|
|
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)
|
|
part.home()
|
|
expect(part.attributes.get('transform')).to.equal(false)
|
|
part.home()
|
|
expect(part.attributes.get('transform')).to.equal(false)
|
|
})
|
|
it('Should run hooks', () => {
|
|
let count = 0
|
|
const design = new Design()
|
|
const pattern = new design({ paperless: true })
|
|
const part = pattern.__createPartWithContext()
|
|
part.hooks.preDraft = [
|
|
{
|
|
method: function () {
|
|
count++
|
|
},
|
|
},
|
|
]
|
|
part.runHooks('preDraft')
|
|
expect(count).to.equal(1)
|
|
})
|
|
*/
|
|
|
|
it('Units closure should log a warning when passing a non-number', () => {
|
|
const part = {
|
|
name: 'test',
|
|
draft: ({ units, part }) => {
|
|
units('a')
|
|
return part
|
|
},
|
|
}
|
|
const design = new Design({ parts: [part] })
|
|
const pattern = new design()
|
|
pattern.draft()
|
|
expect(pattern.stores[0].logs.warning.length).to.equal(1)
|
|
expect(pattern.stores[0].logs.warning[0]).to.equal(
|
|
'Calling `units(value)` but `value` is not a number (`string`)'
|
|
)
|
|
})
|
|
/*
|
|
describe('isEmpty', () => {
|
|
it('Should return true if the part has no paths or snippets', () => {
|
|
const design = new Design()
|
|
const pattern = new design()
|
|
const part = pattern.__createPartWithContext()
|
|
expect(part.isEmpty()).to.be.true
|
|
})
|
|
|
|
it('Should return true if the part has paths but they have no length', () => {
|
|
const design = new Design()
|
|
const pattern = new design()
|
|
const part = pattern.__createPartWithContext()
|
|
const { Path, paths } = part.shorthand()
|
|
paths.seam = new Path()
|
|
expect(part.isEmpty()).to.be.true
|
|
})
|
|
|
|
it("Should return true if the part has paths but they don't render", () => {
|
|
const design = new Design()
|
|
const pattern = new design()
|
|
const part = pattern.__createPartWithContext()
|
|
const { Path, paths, Point } = part.shorthand()
|
|
paths.seam = new Path().move(new Point(0, 0)).line(new Point(2, 3)).setRender(false)
|
|
expect(part.isEmpty()).to.be.true
|
|
})
|
|
|
|
it('Should return false if the part has a path with length', () => {
|
|
const design = new Design()
|
|
const pattern = new design()
|
|
const part = pattern.__createPartWithContext()
|
|
const { Path, paths, Point } = part.shorthand()
|
|
paths.seam = new Path().move(new Point(0, 0)).line(new Point(2, 3))
|
|
|
|
expect(part.isEmpty()).to.be.false
|
|
})
|
|
|
|
it('Should return false if the part has a snippet', () => {
|
|
const design = new Design()
|
|
const pattern = new design()
|
|
const part = pattern.__createPartWithContext()
|
|
const { Point, snippets, Snippet } = part.shorthand()
|
|
snippets.test = new Snippet('test', new Point(0, 0))
|
|
|
|
expect(part.isEmpty()).to.be.false
|
|
})
|
|
|
|
it('Should return false if the part has a point that has text', () => {
|
|
const design = new Design()
|
|
const pattern = new design()
|
|
const part = pattern.__createPartWithContext()
|
|
const { Point, points } = part.shorthand()
|
|
points.test = new Point(0, 0)
|
|
points.test.attributes.set('data-text', 'text')
|
|
expect(part.isEmpty()).to.be.false
|
|
})
|
|
|
|
it('Should return false if the part has a point that has a circle', () => {
|
|
const design = new Design()
|
|
const pattern = new design()
|
|
const part = pattern.__createPartWithContext()
|
|
const { Point, points } = part.shorthand()
|
|
points.test = new Point(0, 0)
|
|
points.test.attributes.set('data-circle', 10)
|
|
expect(part.isEmpty()).to.be.false
|
|
})
|
|
})
|
|
*/
|
|
})
|