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

179 lines
5.4 KiB
JavaScript
Raw Permalink Normal View History

import { expect } from 'chai'
2022-09-19 23:35:52 +02:00
import { Design } from '../src/index.mjs'
describe('Pattern', () => {
it('Should log an error when a part does not have a name', () => {
const part = { draft: ({ part }) => part }
const design = new Design()
const pattern = new design()
pattern.addPart(part)
expect(pattern.store.logs.error.length).to.equal(1)
expect(pattern.store.logs.error[0]).to.equal('Part must have a name')
2022-09-19 23:35:52 +02:00
})
it('Should log an error when a part does not have a draft method', () => {
const from = {
name: 'test',
noDraft: ({ points, part }) => {
2022-09-19 23:35:52 +02:00
points.test = false
return part
2022-09-25 09:29:41 +02:00
},
2022-09-19 23:35:52 +02:00
}
const to = {
name: 'testTo',
from,
2022-09-25 09:29:41 +02:00
draft: ({ part }) => part,
2022-09-19 23:35:52 +02:00
}
2022-09-25 09:29:41 +02:00
const design = new Design({ parts: [to] })
2022-09-19 23:35:52 +02:00
const pattern = new design()
pattern.draft()
expect(pattern.setStores[0].logs.error.length).to.equal(1)
2022-09-25 09:29:41 +02:00
expect(pattern.setStores[0].logs.error[0]).to.equal(
'Unable to draft pattern part `test`. Part.draft() is not callable'
2022-09-25 09:29:41 +02:00
)
2022-09-19 23:35:52 +02:00
})
it('Not returning the part from the draft method should log an error', () => {
const test = {
name: 'test',
2022-09-25 09:29:41 +02:00
draft: () => {},
2022-09-19 23:35:52 +02:00
}
2022-09-25 09:29:41 +02:00
const design = new Design({ parts: [test] })
2022-09-19 23:35:52 +02:00
const pattern = new design()
pattern.draft()
expect(pattern.setStores[0].logs.error.length).to.equal(1)
2022-09-25 09:29:41 +02:00
expect(pattern.setStores[0].logs.error[0]).to.equal(
'Result of drafting part `test` was undefined. Did you forget to return the part?'
2022-09-25 09:29:41 +02:00
)
2022-09-19 23:35:52 +02:00
})
it('Should skip unneeded parts', () => {
const test = {
name: 'test',
2022-09-25 09:29:41 +02:00
draft: ({ part }) => part,
2022-09-19 23:35:52 +02:00
}
2023-02-23 08:13:44 +02:00
const you = {
name: 'you',
draft: ({ part }) => part,
}
const design = new Design({ parts: [test, you] })
2022-09-19 23:35:52 +02:00
const pattern = new design({ only: ['you'] })
pattern.draft()
expect(pattern.setStores[0].logs.debug).to.include('Part `test` is not needed. Skipping part')
2022-09-19 23:35:52 +02:00
})
it('Should return the initialized config', () => {
const test = {
name: 'test',
2022-09-25 09:29:41 +02:00
draft: ({ part }) => part,
2022-09-19 23:35:52 +02:00
}
2022-09-25 09:29:41 +02:00
const design = new Design({ parts: [test] })
2022-09-19 23:35:52 +02:00
const pattern = new design({ only: ['you'] })
const config = pattern.getConfig()
expect(config.draftOrder.length).to.equal(1)
expect(config.draftOrder[0]).to.equal('test')
2022-09-19 23:35:52 +02:00
})
it('Should skip a plugin that is loaded twice', () => {
const test = {
name: 'test',
2022-09-25 09:29:41 +02:00
draft: ({ part }) => part,
2022-09-19 23:35:52 +02:00
}
const plugin = { name: 'test' }
const design = new Design({ parts: [test], noCorePlugins: true })
const pattern = new design({ only: ['you'], noCorePlugins: true })
2022-09-19 23:35:52 +02:00
pattern.use(plugin)
pattern.use(plugin)
pattern.use({ plugin })
pattern.use({ plugin })
expect(Object.keys(pattern.plugins.plugins)).to.have.lengthOf(1)
expect(Object.keys(pattern.plugins.plugins)[0]).to.equal('test')
2022-09-19 23:35:52 +02:00
})
it('Should log an error of added parts do not have a draft method', () => {
const design = new Design()
const pattern = new design()
pattern.addPart({})
expect(pattern.store.logs.error.length).to.equal(1)
expect(pattern.store.logs.error[0]).to.equal('Part must have a draft() method')
2022-09-19 23:35:52 +02:00
})
it('Parts in only are never hidden', () => {
const test = {
name: 'test',
hidden: true,
2022-09-25 09:29:41 +02:00
draft: ({ part }) => part,
2022-09-19 23:35:52 +02:00
}
2022-09-25 09:29:41 +02:00
const design = new Design({ parts: [test] })
const pattern = new design({ only: ['test'] })
pattern.__init()
2022-09-19 23:35:52 +02:00
expect(pattern.__isPartHidden('test')).to.equal(false)
})
it('Stacks with parts in only are never hidden', () => {
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] })
2022-09-25 09:29:41 +02:00
const pattern = new design({ only: ['test'] })
2022-09-19 23:35:52 +02:00
pattern.draft().render()
expect(pattern.__isStackHidden('test')).to.equal(false)
})
it('Stacks with parts in only are never hidden', () => {
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] })
2022-09-25 09:29:41 +02:00
const pattern = new design({ only: ['test'] })
2022-09-19 23:35:52 +02:00
pattern.draft().render()
expect(pattern.__isStackHidden('test')).to.equal(false)
})
it('Drafts with errors should not get packed', () => {
2022-09-25 09:29:41 +02:00
const part = {
2022-09-19 23:35:52 +02:00
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)
2022-09-25 09:29:41 +02:00
joints.foo = 'bar' // eslint-disable-line no-undef
2022-09-19 23:35:52 +02:00
return part
},
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(pattern.setStores[0].logs.error.length).to.equal(1)
expect(pattern.setStores[0].logs.error[0][0]).to.equal('Unable to draft part `test` (set `0`)')
2022-09-19 23:35:52 +02:00
})
it('Handle layout object', () => {
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] })
2022-09-25 09:29:41 +02:00
const pattern = new design({
layout: { stacks: { test: { flipX: true } }, width: 300, height: 400 },
})
2022-09-19 23:35:52 +02:00
const props = pattern.draft().getRenderProps()
expect(props.stacks.test.attributes.list.transform[0]).to.equal('scale(-1, 1)')
2022-09-25 09:29:41 +02:00
expect(props.width).to.equal(300)
expect(props.height).to.equal(400)
2022-09-19 23:35:52 +02:00
})
})