1
0
Fork 0

add sinon for better testing

This commit is contained in:
Enoch Riese 2023-02-23 19:15:20 +02:00
parent 12ffb0a9c1
commit dc998d4e98
6 changed files with 128 additions and 11 deletions

View file

@ -60,7 +60,10 @@
},
"devDependencies": {
"eslint": "8.34.0",
"nyc": "15.1.0"
"nyc": "15.1.0",
"mocha": "10.0.0",
"chai": "4.2.0",
"sinon": "^15.0.1"
},
"files": [
"dist/*",

View file

@ -9,7 +9,7 @@ PatternDraftQueue.prototype.start = function () {
}
PatternDraftQueue.prototype.addPart = function (partName) {
this.queue.push(partName)
if (!this.contains(partName)) this.queue.push(partName)
return this
}
@ -27,6 +27,10 @@ PatternDraftQueue.prototype.next = function () {
return next
}
PatternDraftQueue.prototype.contains = function (partName) {
return this.queue.indexOf(partName) !== -1
}
/**
* Resolves the draft order based on the configuation
*

View file

@ -67,8 +67,11 @@ export function Pattern(designConfig = {}) {
* It might be useful to not resolve immediately if a number of parts will be added over multiple calls
* @return {object} this - The Pattern instance
*/
Pattern.prototype.addPart = function (part, resolveImmediately = false) {
if (this.__configResolver.isPartValid(part) && this.designConfig.parts.indexOf(part) === -1) {
Pattern.prototype.addPart = function (part, resolveImmediately = true) {
if (
this.__configResolver.isPartValid(part) &&
!this.designConfig.parts.find((p) => p.name == part.name)
) {
this.designConfig.parts.push(part)
if (resolveImmediately) {
if (this.__configResolver.addPart(part) && typeof this.draftQueue !== 'undefined')

View file

@ -1,5 +1,6 @@
import chai from 'chai'
import { Design } from '../src/index.mjs'
import sinon from 'sinon'
const expect = chai.expect
@ -22,11 +23,6 @@ describe('Pattern', () => {
draft: ({ part }) => part,
}
describe('with runtime: true, resolveImmediately: true', () => {
it('adds the part to the current draft cycle')
it('does not add the part to subsequent draft cycles')
})
describe('with resolveImmediately: true', () => {
it('Should add the part to parts object', () => {
const design = new Design({ parts: [part1] })
@ -152,6 +148,51 @@ describe('Pattern', () => {
pattern.addPart(part3, true)
expect(pattern.config.options.opt1).to.equal(opt1)
})
describe('during drafting', () => {
it('adds the part to the draft queue', () => {
const design = new Design({ parts: [part1] })
const pattern = new design()
pattern.use({
name: 'draftTimePartPlugin',
hooks: {
postPartDraft: (pattern) => {
const newPart = {
name: 'newPartTest',
draft: ({ part }) => part,
}
pattern.addPart(newPart)
},
},
})
pattern.draft()
expect(pattern.draftQueue.contains('newPartTest')).to.be.true
})
it('drafts the part', () => {
const design = new Design({ parts: [part1] })
const pattern = new design()
const part2Draft = ({ part }) => part
const draftSpy = sinon.spy(part2Draft)
pattern.use({
name: 'draftTimePartPlugin',
hooks: {
postPartDraft: (pattern) => {
const newPart = {
name: 'newPartTest',
draft: draftSpy,
}
pattern.addPart(newPart)
},
},
})
pattern.draft()
expect(draftSpy.calledOnce).to.be.true
})
})
})
describe('with resolveImmediately: false', () => {