From b2954e27ff4a744dfea70f7cc0add1735b9de9ec Mon Sep 17 00:00:00 2001 From: Benjamin Barreto <4544900-benalexb@users.noreply.gitlab.com> Date: Wed, 21 Oct 2020 15:10:21 +0200 Subject: [PATCH] Added RegularItem with tests --- js-jest/src/gilded_rose.js | 27 +++++++++++++++++++++++- js-jest/test/gilded_rose.test.js | 35 ++++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/js-jest/src/gilded_rose.js b/js-jest/src/gilded_rose.js index ad71ae43..1cebe0c6 100644 --- a/js-jest/src/gilded_rose.js +++ b/js-jest/src/gilded_rose.js @@ -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 } diff --git a/js-jest/test/gilded_rose.test.js b/js-jest/test/gilded_rose.test.js index 4ed9ab26..db2a2c72 100644 --- a/js-jest/test/gilded_rose.test.js +++ b/js-jest/test/gilded_rose.test.js @@ -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() }) })