2024-02-04 12:14:42 +01:00
|
|
|
import { expect, AssertionError, Assertion } from 'chai'
|
2023-03-08 12:12:13 -06:00
|
|
|
import { Design, hidePresets } from '../src/index.mjs'
|
2023-03-07 16:34:14 -06:00
|
|
|
|
|
|
|
function hidePartMatcher(partName) {
|
|
|
|
const isHidden = this._obj.__isPartHidden(partName)
|
|
|
|
if (!this._obj.config.parts[partName]) {
|
2024-02-04 12:14:42 +01:00
|
|
|
throw new AssertionError(`expected part \`${partName}\` to exist in pattern`)
|
2023-03-07 16:34:14 -06:00
|
|
|
}
|
|
|
|
this.assert(
|
|
|
|
isHidden,
|
|
|
|
`expected part ${partName} to be hidden, but it is shown`,
|
|
|
|
`expected part ${partName} to NOT be hidden, but it is hidden`
|
|
|
|
)
|
|
|
|
}
|
2023-03-08 12:12:13 -06:00
|
|
|
|
|
|
|
function hidePartsMatcher(...partNames) {
|
|
|
|
const hiddens = partNames.map((n) => {
|
|
|
|
if (!this._obj.config.parts[n]) {
|
2024-02-04 12:14:42 +01:00
|
|
|
throw new AssertionError(`expected part \`${n}\` to exist in pattern`)
|
2023-03-08 12:12:13 -06:00
|
|
|
}
|
|
|
|
return this._obj.__isPartHidden(n)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.assert(
|
|
|
|
hiddens.every((v) => v === true),
|
|
|
|
`expected parts [${partNames}] to be hidden, but hide check returns [${hiddens}]`,
|
|
|
|
`expected parts [${partNames}] to NOT be hidden, but hide check returns [${hiddens}]`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-02-04 12:14:42 +01:00
|
|
|
Assertion.addMethod('hidePart', hidePartMatcher)
|
|
|
|
Assertion.addMethod('hideParts', hidePartsMatcher)
|
2023-03-07 16:34:14 -06:00
|
|
|
|
|
|
|
const blankDraft = ({ part }) => part
|
|
|
|
const blankPart = (name, config = {}) => ({
|
|
|
|
name,
|
|
|
|
draft: blankDraft,
|
|
|
|
...config,
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Hiding parts', () => {
|
|
|
|
const afterPart = blankPart('afterPart')
|
|
|
|
const fromPart = blankPart('fromPart')
|
|
|
|
|
|
|
|
describe('With {hide: {self: true}}', () => {
|
|
|
|
const mainPart = {
|
|
|
|
name: 'mainPart',
|
|
|
|
after: afterPart,
|
|
|
|
from: fromPart,
|
|
|
|
hide: { self: true },
|
|
|
|
draft: blankDraft,
|
|
|
|
}
|
|
|
|
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [mainPart],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
it('Should hide the part', () => {
|
|
|
|
expect(pattern).to.hidePart('mainPart')
|
|
|
|
})
|
|
|
|
|
|
|
|
it("Should NOT hide the part's dependencies", () => {
|
2023-03-08 12:12:13 -06:00
|
|
|
expect(pattern).not.to.hidePart('fromPart', 'afterPart')
|
2023-03-07 16:34:14 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('Inherited Parts', () => {
|
|
|
|
const mainPart = {
|
|
|
|
name: 'mainPart',
|
|
|
|
after: afterPart,
|
|
|
|
from: fromPart,
|
|
|
|
draft: blankDraft,
|
|
|
|
}
|
|
|
|
const grandChild = {
|
|
|
|
name: 'grandChild',
|
|
|
|
from: mainPart,
|
|
|
|
hide: { self: true },
|
|
|
|
draft: blankDraft,
|
|
|
|
}
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [grandChild],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
it('Should NOT hide inherited `from` dependencies', () => {
|
2023-03-08 12:12:13 -06:00
|
|
|
expect(pattern).not.to.hideParts('fromPart', 'mainPart')
|
2023-03-07 16:34:14 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should NOT hide inherited `after` dependencies', () => {
|
|
|
|
expect(pattern).not.to.hidePart('afterPart')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('With {hide: {from: true}}', () => {
|
|
|
|
const mainPart = {
|
|
|
|
name: 'mainPart',
|
|
|
|
hide: { from: true },
|
|
|
|
after: afterPart,
|
|
|
|
from: fromPart,
|
|
|
|
draft: blankDraft,
|
|
|
|
}
|
|
|
|
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [mainPart],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
it('Should NOT hide the part', () => {
|
|
|
|
expect(pattern).not.to.hidePart('mainPart')
|
|
|
|
})
|
|
|
|
it("Should hide the part's `from` dependencies", () => {
|
|
|
|
expect(pattern).to.hidePart('fromPart')
|
|
|
|
})
|
|
|
|
it("Should NOT hide the part's `after` dependencies", () => {
|
|
|
|
expect(pattern).not.to.hidePart('afterPart')
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Inherited Parts', () => {
|
|
|
|
const mainPart = {
|
|
|
|
name: 'mainPart',
|
|
|
|
after: afterPart,
|
|
|
|
from: fromPart,
|
|
|
|
draft: blankDraft,
|
|
|
|
}
|
|
|
|
const grandChild = {
|
|
|
|
name: 'grandChild',
|
|
|
|
from: mainPart,
|
|
|
|
hide: { from: true },
|
|
|
|
draft: blankDraft,
|
|
|
|
}
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [grandChild],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
it("Should hide the part's `from` dependencies", () => {
|
|
|
|
expect(pattern).to.hidePart('mainPart')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should NOT hide inherited `from` dependencies', () => {
|
|
|
|
expect(pattern).not.to.hidePart('fromPart')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should NOT hide inherited `after` dependencies', () => {
|
|
|
|
expect(pattern).not.to.hidePart('afterPart')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('With {hide: {after: true}}', () => {
|
|
|
|
const mainPart = {
|
|
|
|
name: 'mainPart',
|
|
|
|
hide: { after: true },
|
|
|
|
after: afterPart,
|
|
|
|
from: fromPart,
|
|
|
|
draft: blankDraft,
|
|
|
|
}
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [mainPart],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
it('Should NOT hide the part', () => {
|
|
|
|
expect(pattern).not.to.hidePart('mainPart')
|
|
|
|
})
|
|
|
|
it("Should NOT hide the part's `from` dependencies", () => {
|
|
|
|
expect(pattern).not.to.hidePart('fromPart')
|
|
|
|
})
|
|
|
|
it("Should hide the part's `after` dependencies", () => {
|
|
|
|
expect(pattern).to.hidePart('afterPart')
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Inherited Parts', () => {
|
|
|
|
const mainPart = {
|
|
|
|
name: 'mainPart',
|
|
|
|
after: afterPart,
|
|
|
|
from: fromPart,
|
|
|
|
draft: blankDraft,
|
|
|
|
}
|
|
|
|
const grandChild = {
|
|
|
|
name: 'grandChild',
|
|
|
|
from: mainPart,
|
|
|
|
hide: { after: true },
|
|
|
|
draft: blankDraft,
|
|
|
|
}
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [grandChild],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
it('Should NOT hide inherited `from` dependencies', () => {
|
|
|
|
expect(pattern).not.to.hidePart('fromPart')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should NOT hide inherited `after` dependencies', () => {
|
|
|
|
expect(pattern).not.to.hidePart('afterPart')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('With {hide: {inherited: true}}', () => {
|
|
|
|
const grandParent = blankPart('grandParent', { from: fromPart, after: afterPart })
|
|
|
|
const parentAfter = blankPart('parentAfter')
|
|
|
|
const parent = blankPart('parent', { from: grandParent, after: parentAfter })
|
|
|
|
const mainAfterFrom = blankPart('mainAfterFrom')
|
|
|
|
const mainAfterAfter = blankPart('mainAfterAfter')
|
|
|
|
const mainAfter = blankPart('mainAfter', { after: mainAfterAfter, from: mainAfterFrom })
|
|
|
|
const mainPart = {
|
|
|
|
name: 'mainPart',
|
|
|
|
from: parent,
|
|
|
|
after: mainAfter,
|
|
|
|
hide: { inherited: true },
|
|
|
|
draft: blankDraft,
|
|
|
|
}
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [mainPart],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
it('Should NOT hide the part', () => {
|
|
|
|
expect(pattern).not.to.hidePart('mainPart')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should NOT hide the `from` dependency', () => {
|
|
|
|
expect(pattern).not.to.hidePart('parent')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should NOT hide the `after` dependency', () => {
|
|
|
|
expect(pattern).not.to.hidePart('mainAfter')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should NOT hide the `after` dependencies of `after` dependencies', () => {
|
|
|
|
expect(pattern).not.to.hidePart('mainAfterAfter')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should hide the `from` dependencies of `after` dependencies', () => {
|
|
|
|
expect(pattern).to.hidePart('mainAfterFrom')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should hide the `after` dependencies of `from` dependencies', () => {
|
2023-03-08 12:12:13 -06:00
|
|
|
expect(pattern).to.hideParts('afterPart', 'parentAfter')
|
2023-03-07 16:34:14 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should hide the `from` dependencies of `from` dependencies', () => {
|
2023-03-08 12:12:13 -06:00
|
|
|
expect(pattern).to.hideParts('fromPart', 'grandParent')
|
2023-03-07 16:34:14 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("With {hide: {always: ['partname']} }", () => {
|
|
|
|
it('Should hide the given part', () => {
|
|
|
|
const mainPart = blankPart('mainPart', {
|
|
|
|
after: afterPart,
|
|
|
|
hide: {
|
|
|
|
always: ['afterPart'],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [mainPart],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
expect(pattern).to.hidePart('afterPart')
|
|
|
|
})
|
|
|
|
|
|
|
|
it("Should NOT hide the given part if a higher-level part includes it in {hide: {never: ['partName']} }", () => {
|
|
|
|
const grandParent = blankPart('grandParent')
|
|
|
|
const parent = blankPart('parent', {
|
|
|
|
from: grandParent,
|
|
|
|
hide: { always: ['grandParent'] },
|
|
|
|
})
|
|
|
|
const main1 = blankPart('main1', {
|
|
|
|
from: parent,
|
|
|
|
hide: { from: true, never: ['grandParent'] },
|
|
|
|
})
|
|
|
|
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [main1],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
expect(pattern).not.to.hidePart('grandParent')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("With {hide: {never: ['partName']} }", () => {
|
|
|
|
it('Should NOT hide the given part even if another setting would hide it', () => {
|
|
|
|
const parent = blankPart('parent')
|
|
|
|
const main1 = blankPart('main1', {
|
|
|
|
from: parent,
|
|
|
|
hide: { from: true, never: ['parent'] },
|
|
|
|
})
|
|
|
|
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [main1],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
expect(pattern).not.to.hidePart('parent')
|
|
|
|
})
|
|
|
|
|
|
|
|
it("Should hide the given part if a higher-level part includes it in {hide: {always: ['partName']} }", () => {
|
|
|
|
const grandParent = blankPart('grandParent')
|
|
|
|
const parent = blankPart('parent', {
|
|
|
|
from: grandParent,
|
|
|
|
hide: { never: ['grandParent'] },
|
|
|
|
})
|
|
|
|
const main1 = blankPart('main1', {
|
|
|
|
from: parent,
|
|
|
|
hide: { from: true, always: ['grandParent'] },
|
|
|
|
})
|
|
|
|
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [main1],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
expect(pattern).to.hidePart('grandParent')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-03-08 12:12:13 -06:00
|
|
|
describe('HIDE_ALL', () => {
|
|
|
|
const grandParent = blankPart('grandParent')
|
|
|
|
const parent = blankPart('parent', {
|
|
|
|
from: grandParent,
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should behave like `{self: true, after: true, from: true, inherited: true}`', () => {
|
|
|
|
const main1 = blankPart('main1', {
|
|
|
|
from: parent,
|
|
|
|
after: afterPart,
|
|
|
|
hide: hidePresets.HIDE_ALL,
|
|
|
|
})
|
|
|
|
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [main1],
|
|
|
|
})
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
expect(pattern).to.hideParts('grandParent', 'parent', 'main1', 'afterPart')
|
|
|
|
})
|
|
|
|
it('Should work when passed as a string', () => {
|
|
|
|
const main1 = blankPart('main1', {
|
|
|
|
from: parent,
|
|
|
|
after: afterPart,
|
|
|
|
hide: 'HIDE_ALL',
|
|
|
|
})
|
|
|
|
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [main1],
|
|
|
|
})
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
expect(pattern).to.hideParts('grandParent', 'parent', 'main1', 'afterPart')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('HIDE_TREE', () => {
|
|
|
|
const grandParent = blankPart('grandParent', { from: fromPart, after: afterPart })
|
|
|
|
const parentAfter = blankPart('parentAfter')
|
|
|
|
const parent = blankPart('parent', { from: grandParent, after: parentAfter })
|
|
|
|
const mainAfterFrom = blankPart('mainAfterFrom')
|
|
|
|
const mainAfterAfter = blankPart('mainAfterAfter')
|
|
|
|
const mainAfter = blankPart('mainAfter', { after: mainAfterAfter, from: mainAfterFrom })
|
|
|
|
|
|
|
|
it('Should behave like `{from: true, inherited: true}`', () => {
|
|
|
|
const mainPart = {
|
|
|
|
name: 'mainPart',
|
|
|
|
from: parent,
|
|
|
|
after: mainAfter,
|
|
|
|
hide: hidePresets.HIDE_TREE,
|
|
|
|
draft: blankDraft,
|
|
|
|
}
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [mainPart],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
expect(pattern).to.hideParts(
|
|
|
|
'grandParent',
|
|
|
|
'fromPart',
|
|
|
|
'afterPart',
|
|
|
|
'parentAfter',
|
|
|
|
`mainAfterFrom`,
|
|
|
|
`parent`
|
|
|
|
)
|
|
|
|
expect(pattern).to.not.hideParts('mainPart', 'mainAfter', 'mainAfterAfter')
|
|
|
|
})
|
|
|
|
it('Should work when passed as a string', () => {
|
|
|
|
const mainPart = {
|
|
|
|
name: 'mainPart',
|
|
|
|
from: parent,
|
|
|
|
after: mainAfter,
|
|
|
|
hide: 'HIDE_TREE',
|
|
|
|
draft: blankDraft,
|
|
|
|
}
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [mainPart],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
expect(pattern).to.hideParts(
|
|
|
|
'grandParent',
|
|
|
|
'fromPart',
|
|
|
|
'afterPart',
|
|
|
|
'parentAfter',
|
|
|
|
`mainAfterFrom`,
|
|
|
|
`parent`
|
|
|
|
)
|
|
|
|
expect(pattern).to.not.hideParts('mainPart', 'mainAfter', 'mainAfterAfter')
|
|
|
|
})
|
|
|
|
})
|
2023-03-07 16:34:14 -06:00
|
|
|
describe('With complex inheritance', () => {
|
|
|
|
it('Should use the strictest hiding configuration given by toplevel parts', () => {
|
|
|
|
const greatGrandParent = blankPart('greatGrandParent')
|
|
|
|
const grandParent = blankPart('grandParent', { from: greatGrandParent })
|
|
|
|
const parent = blankPart('parent', { from: grandParent })
|
|
|
|
const main1 = blankPart('main1', {
|
|
|
|
from: parent,
|
|
|
|
})
|
|
|
|
const main2 = blankPart('main2', {
|
|
|
|
from: parent,
|
|
|
|
hide: { from: true, inherited: true },
|
|
|
|
})
|
|
|
|
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [main1, main2],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
2023-03-08 12:12:13 -06:00
|
|
|
expect(pattern).to.hideParts('parent', 'grandParent', 'greatGrandParent')
|
2023-03-07 16:34:14 -06:00
|
|
|
})
|
|
|
|
it('Should use inherited configurations that are not overridden', () => {
|
|
|
|
const greatGrandParent = blankPart('greatGrandParent')
|
|
|
|
const grandParent = blankPart('grandParent', { from: greatGrandParent })
|
|
|
|
const parent = blankPart('parent', {
|
|
|
|
from: grandParent,
|
|
|
|
hide: { inherited: true },
|
|
|
|
})
|
|
|
|
const main1 = blankPart('main1', {
|
|
|
|
from: parent,
|
|
|
|
hide: { from: true },
|
|
|
|
})
|
|
|
|
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [main1],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
expect(pattern).to.hidePart('greatGrandParent')
|
|
|
|
})
|
|
|
|
it('Should override inherited hiding configurations', () => {
|
|
|
|
const greatGrandParent = blankPart('greatGrandParent')
|
|
|
|
const grandParent = blankPart('grandParent', { from: greatGrandParent })
|
|
|
|
const parent = blankPart('parent', {
|
|
|
|
from: grandParent,
|
|
|
|
hide: { inherited: true },
|
|
|
|
})
|
|
|
|
const main1 = blankPart('main1', {
|
|
|
|
from: parent,
|
|
|
|
hide: { from: true, inherited: false },
|
|
|
|
})
|
|
|
|
|
|
|
|
const Test = new Design({
|
|
|
|
name: 'test',
|
|
|
|
parts: [main1],
|
|
|
|
})
|
|
|
|
|
|
|
|
const pattern = new Test()
|
|
|
|
pattern.__init()
|
|
|
|
|
|
|
|
expect(pattern).not.to.hidePart('greatGrandParent')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|