mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 14:31:28 +00:00
Implemented RegularItem.updateQuality and tests
This commit is contained in:
parent
5367b03ff5
commit
fe72eddab1
@ -16,17 +16,7 @@ class RegularItem extends Item {
|
|||||||
const { name, sellIn, quality } = itemProps
|
const { name, sellIn, quality } = itemProps
|
||||||
super(name, sellIn, quality)
|
super(name, sellIn, quality)
|
||||||
this.validateItemProps(itemProps)
|
this.validateItemProps(itemProps)
|
||||||
}
|
this.depreciationRate = 1
|
||||||
|
|
||||||
_privateVar = 0
|
|
||||||
|
|
||||||
get privateVar () {
|
|
||||||
return this._privateVar
|
|
||||||
}
|
|
||||||
|
|
||||||
set privateVar (value) {
|
|
||||||
console.log(`entered set privateVar with value: ${value}`)
|
|
||||||
this._privateVar = value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
validateItemProps ({ name, sellIn, quality }) {
|
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(', ')}`)
|
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 {
|
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') {
|
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
|
this.items[i].sellIn = this.items[i].sellIn - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -9,10 +9,13 @@ const { RegularItem } = require('../src/gilded_rose')
|
|||||||
// })
|
// })
|
||||||
|
|
||||||
describe('RegularItem', () => {
|
describe('RegularItem', () => {
|
||||||
const getRegularItemFactory = (itemProps) => () => new RegularItem({
|
const mockProperties = {
|
||||||
name: 'Mock RegularItem',
|
name: 'Mock RegularItem',
|
||||||
sellIn: 5,
|
sellIn: 5,
|
||||||
quality: 25,
|
quality: 25
|
||||||
|
}
|
||||||
|
const getRegularItemFactory = (itemProps) => () => new RegularItem({
|
||||||
|
...mockProperties,
|
||||||
...itemProps
|
...itemProps
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -28,4 +31,22 @@ describe('RegularItem', () => {
|
|||||||
expect(getRegularItemFactory({ quality: -1 })).toThrow()
|
expect(getRegularItemFactory({ quality: -1 })).toThrow()
|
||||||
expect(getRegularItemFactory({ quality: 51 })).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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
const { Shop, Item, RegularItem } = require('../src/gilded_rose')
|
const { Shop, Item } = require('../src/gilded_rose')
|
||||||
|
|
||||||
const items = [
|
const items = [
|
||||||
new Item('+5 Dexterity Vest', 10, 20),
|
new Item('+5 Dexterity Vest', 10, 20),
|
||||||
@ -15,16 +15,6 @@ const items = [
|
|||||||
new Item('Conjured Mana Cake', 3, 6)
|
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 days = Number(process.argv[2]) || 2
|
||||||
const gildedRose = new Shop(items)
|
const gildedRose = new Shop(items)
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user