2022-09-19 23:35:52 +02:00
|
|
|
import chai from 'chai'
|
|
|
|
import { Design } from '../src/index.mjs'
|
|
|
|
|
|
|
|
const expect = chai.expect
|
|
|
|
|
|
|
|
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)
|
2022-09-24 18:37:49 +02:00
|
|
|
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',
|
2022-09-24 18:37:49 +02:00
|
|
|
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()
|
2022-09-24 18:37:49 +02:00
|
|
|
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(
|
2023-06-11 09:36:16 +02:00
|
|
|
'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()
|
2022-09-24 18:37:49 +02:00
|
|
|
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(
|
2023-06-11 09:36:16 +02:00
|
|
|
'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()
|
2023-04-17 19:45:00 -04:00
|
|
|
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()
|
2022-09-24 18:37:49 +02:00
|
|
|
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' }
|
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'] })
|
|
|
|
pattern.use(plugin)
|
|
|
|
pattern.use(plugin)
|
|
|
|
pattern.use({ plugin })
|
|
|
|
pattern.use({ plugin })
|
2023-04-16 23:59:21 -04:00
|
|
|
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({})
|
2022-09-24 18:37:49 +02:00
|
|
|
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'] })
|
2022-09-20 15:24:10 +02:00
|
|
|
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()
|
2022-09-24 18:37:49 +02:00
|
|
|
expect(pattern.setStores[0].logs.error.length).to.equal(1)
|
2023-06-11 09:36:16 +02:00
|
|
|
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()
|
feat(core): Added Pattern.getLogs() and updated Pattern.getRenderProps()
The data returned by `Pattern.getRenderProps()` was not serializable as
we were returning `this` all over the place, thereby including marcors,
log methods, cyclic object references, and so on.
This commit changes that by implementing a `.asRenderProp()` method on
all of the various objects (stack, part, path, point, snippet,
attributes, svg) and only including data that can be serialized.
In addition, we no longer include the logs in the renderProps because
they are not related to rendering the pattern.
Instead, the new method `Pattern.getLogs()` gives you the logs.
2023-06-01 16:45:13 +02:00
|
|
|
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
|
|
|
})
|
|
|
|
})
|