diff --git a/js-jest/src/gilded_rose.js b/js-jest/src/gilded_rose.js index dc571818..e83ae86b 100644 --- a/js-jest/src/gilded_rose.js +++ b/js-jest/src/gilded_rose.js @@ -16,17 +16,7 @@ class RegularItem extends Item { const { name, sellIn, quality } = itemProps super(name, sellIn, quality) this.validateItemProps(itemProps) - } - - _privateVar = 0 - - get privateVar () { - return this._privateVar - } - - set privateVar (value) { - console.log(`entered set privateVar with value: ${value}`) - this._privateVar = value + this.depreciationRate = 1 } validateItemProps ({ name, sellIn, quality }) { @@ -46,6 +36,20 @@ class RegularItem extends Item { throw new Error(`[RegularItem.validateItemProps] Invalid itemProps passed to the constructor: ${errors.join(', ')}`) } } + + updateQuality () { + // "Once the sell by date has passed, Quality degrades twice as fast" + const adjustedDepreciationRate = this.sellIn < 0 + ? this.depreciationRate * 2 + : this.depreciationRate + + // "The Quality of an item is never negative" + this.quality = Math.max(0, this.quality - adjustedDepreciationRate) + + // Assuming updateQuality is called only once a day... + // "At the end of each day our system lowers both values [quality and sellIn] (...)" + this.sellIn-- + } } class Shop { @@ -86,9 +90,8 @@ class Shop { } } - // Unnecessary decrement of sellIn - // "Sulfuras", being a legendary item, never has to be sold or decreases in Quality" if (this.items[i].name !== 'Sulfuras, Hand of Ragnaros') { + // Decrement sellIn each time updateQuality is called this.items[i].sellIn = this.items[i].sellIn - 1 } diff --git a/js-jest/test/gilded_rose.test.js b/js-jest/test/gilded_rose.test.js index d615d7a9..2d1b29bb 100644 --- a/js-jest/test/gilded_rose.test.js +++ b/js-jest/test/gilded_rose.test.js @@ -9,10 +9,13 @@ const { RegularItem } = require('../src/gilded_rose') // }) describe('RegularItem', () => { - const getRegularItemFactory = (itemProps) => () => new RegularItem({ + const mockProperties = { name: 'Mock RegularItem', sellIn: 5, - quality: 25, + quality: 25 + } + const getRegularItemFactory = (itemProps) => () => new RegularItem({ + ...mockProperties, ...itemProps }) @@ -28,4 +31,22 @@ describe('RegularItem', () => { expect(getRegularItemFactory({ quality: -1 })).toThrow() expect(getRegularItemFactory({ quality: 51 })).toThrow() }) + + it('[updateQuality] should decrement sellIn by 1 and quality by 1 when sellIn is >= 0', () => { + const mockRegularItem = new RegularItem(mockProperties) + mockRegularItem.updateQuality() + expect(mockRegularItem.quality).toEqual(mockProperties.quality - 1) + }) + + it('[updateQuality] should decrement sellIn by 1 and quality by 2 when sellIn is < 0', () => { + const mockRegularItem = new RegularItem({ ...mockProperties, sellIn: -1 }) + mockRegularItem.updateQuality() + expect(mockRegularItem.quality).toEqual(mockProperties.quality - 2) + }) + + it('[updateQuality] should not decrement quality to a negative number', () => { + const mockRegularItem = new RegularItem({ ...mockProperties, quality: 0, sellIn: -1 }) + mockRegularItem.updateQuality() + expect(mockRegularItem.quality).toBeGreaterThanOrEqual(0) + }) }) diff --git a/js-jest/test/texttest_fixture.js b/js-jest/test/texttest_fixture.js index d634d3d5..051ac8ee 100644 --- a/js-jest/test/texttest_fixture.js +++ b/js-jest/test/texttest_fixture.js @@ -1,5 +1,5 @@ -const { Shop, Item, RegularItem } = require('../src/gilded_rose') +const { Shop, Item } = require('../src/gilded_rose') const items = [ new Item('+5 Dexterity Vest', 10, 20), @@ -15,16 +15,6 @@ const items = [ new Item('Conjured Mana Cake', 3, 6) ] -const mockRegularItem = new RegularItem({ - name: 'Mock RegularItem', - sellIn: 5, - quality: 10 -}) - -console.log('mockRegularItem.privateVar', mockRegularItem.privateVar) // bbarreto_debug -mockRegularItem.privateVar = 10 -console.log('mockRegularItem.privateVar', mockRegularItem.privateVar) // bbarreto_debug - const days = Number(process.argv[2]) || 2 const gildedRose = new Shop(items)