Added RegularItem with tests

This commit is contained in:
Benjamin Barreto 2020-10-21 15:10:21 +02:00
parent 9cbeedd736
commit b2954e27ff
2 changed files with 55 additions and 7 deletions

View File

@ -11,6 +11,30 @@ class Item {
}
}
class RegularItem extends Item {
constructor (itemProps) {
const { name, sellIn, quality } = itemProps
super(name, sellIn, quality)
this.validateItemProps(itemProps)
}
validateItemProps({ name, sellIn, quality }) {
const errors = [];
const isNameValid = typeof name === 'string' && name.length
const isSellInValid = typeof sellIn === 'number' && sellIn > 0
const isQualityValid = typeof quality === 'number' && quality >= 0 && quality <=50
!isNameValid && errors.push('"name" must be a valid, non-empty string')
!isSellInValid && errors.push('"sellIn" must be an integer, greater than zero')
!isQualityValid && errors.push('"qualityValid" must be an integer, between 0 and 50')
if (errors.length) {
throw new Error(`[RegularItem.validateItemProps] Invalid itemProps passed to the constructor: ${errors.join(', ')}`)
}
}
}
class Blade extends Item {
constructor(...args) {
super(...args)
@ -91,5 +115,6 @@ class Shop {
module.exports = {
Item,
Shop,
Sword
Sword,
RegularItem
}

View File

@ -1,9 +1,32 @@
const { Shop, Item } = require('../src/gilded_rose')
const { Shop, Item, RegularItem } = require('../src/gilded_rose')
describe('Gilded Rose', function () {
it('should foo', function () {
const gildedRose = new Shop([new Item('foo', 0, 0)])
const items = gildedRose.updateQuality()
expect(items[0].name).toBe('fixme')
// describe('Gilded Rose', function () {
// it('should foo', function () {
// const gildedRose = new Shop([new Item('foo', 0, 0)])
// const items = gildedRose.updateQuality()
// expect(items[0].name).toBe('fixme')
// })
// })
describe('RegularItem', () => {
const getRegularItemFactory = (itemProps) => () => new RegularItem({
name: 'Mock RegularItem',
sellIn: 5,
quality: 25,
...itemProps
})
it('should throw an error if initialized with an invalid name', () => {
expect(getRegularItemFactory({ name: '' })).toThrow()
})
it('should throw an error if initialized with an invalid sellIn property', () => {
expect(getRegularItemFactory({ sellIn: -1 })).toThrow()
expect(getRegularItemFactory({ sellIn: 0 })).toThrow()
})
it('should throw an error if initialized with a quality property < 0 || > 50', () => {
expect(getRegularItemFactory({ quality: -1 })).toThrow()
expect(getRegularItemFactory({ quality: 51 })).toThrow()
})
})