mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
refactor: refactor the existing function and added test cases
This commit is contained in:
parent
54cf2e5da8
commit
c67a0393e4
@ -1,67 +1,68 @@
|
||||
class Item {
|
||||
constructor(name, sellIn, quality){
|
||||
this.name = name;
|
||||
this.sellIn = sellIn;
|
||||
this.quality = quality;
|
||||
constructor(name, sellIn, quality) {
|
||||
this.name = name
|
||||
this.sellIn = sellIn
|
||||
this.quality = quality
|
||||
}
|
||||
}
|
||||
|
||||
class Shop {
|
||||
constructor(items=[]){
|
||||
this.items = items;
|
||||
}
|
||||
updateQuality() {
|
||||
for (let i = 0; i < this.items.length; i++) {
|
||||
if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
|
||||
if (this.items[i].quality > 0) {
|
||||
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
|
||||
this.items[i].quality = this.items[i].quality - 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (this.items[i].quality < 50) {
|
||||
this.items[i].quality = this.items[i].quality + 1;
|
||||
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') {
|
||||
if (this.items[i].sellIn < 11) {
|
||||
if (this.items[i].quality < 50) {
|
||||
this.items[i].quality = this.items[i].quality + 1;
|
||||
}
|
||||
}
|
||||
if (this.items[i].sellIn < 6) {
|
||||
if (this.items[i].quality < 50) {
|
||||
this.items[i].quality = this.items[i].quality + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
|
||||
this.items[i].sellIn = this.items[i].sellIn - 1;
|
||||
}
|
||||
if (this.items[i].sellIn < 0) {
|
||||
if (this.items[i].name != 'Aged Brie') {
|
||||
if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
|
||||
if (this.items[i].quality > 0) {
|
||||
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
|
||||
this.items[i].quality = this.items[i].quality - 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.items[i].quality = this.items[i].quality - this.items[i].quality;
|
||||
}
|
||||
} else {
|
||||
if (this.items[i].quality < 50) {
|
||||
this.items[i].quality = this.items[i].quality + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const calculateQualityDiffForNormalItem = ({ sellIn, quality }) => {
|
||||
const isQualityGreaterThanZero = quality > 0
|
||||
const cannotSell = sellIn < 0
|
||||
|
||||
return this.items;
|
||||
if (isQualityGreaterThanZero && cannotSell) return -2
|
||||
if (isQualityGreaterThanZero) return -1
|
||||
return 0
|
||||
}
|
||||
|
||||
const calculateQualityDiffForBackstage = ({ sellIn, quality }) => {
|
||||
const tenDaysOrLessToSell = sellIn <= 10
|
||||
const fiveDaysOrLessToSell = sellIn <= 5
|
||||
const cannotSell = sellIn < 0
|
||||
|
||||
if (cannotSell) return -quality
|
||||
if (fiveDaysOrLessToSell) return +3
|
||||
if (tenDaysOrLessToSell) return +2
|
||||
|
||||
return +1
|
||||
}
|
||||
|
||||
const calculateSellinDifference = ({ name }) => {
|
||||
const isSulfuras = name == 'Sulfuras'
|
||||
return !isSulfuras ? -1 : 0
|
||||
}
|
||||
|
||||
const calculateQualityDifference = (item) => {
|
||||
const isSulfuras = item.name == 'Sulfuras'
|
||||
const isAgedBrie = item.name == 'Aged Brie'
|
||||
const isConjuredItem = item.name.includes('Conjured')
|
||||
const isBackstagePasses = item.name == 'Backstage passes'
|
||||
const isQualityLessThan50 = item.quality < 50
|
||||
const isNormalItem =
|
||||
!isAgedBrie && !isBackstagePasses && !isSulfuras && !isConjuredItem
|
||||
|
||||
if (isConjuredItem) return calculateQualityDiffForNormalItem(item) * 2
|
||||
if (isNormalItem) return calculateQualityDiffForNormalItem(item)
|
||||
if (isBackstagePasses) return calculateQualityDiffForBackstage(item)
|
||||
if (isAgedBrie && isQualityLessThan50) return +1
|
||||
return 0
|
||||
}
|
||||
|
||||
class Shop {
|
||||
constructor(items = []) {
|
||||
this.items = items
|
||||
}
|
||||
|
||||
updateQuality() {
|
||||
return this.items.map((item) => {
|
||||
item.sellIn += calculateSellinDifference(item)
|
||||
item.quality += calculateQualityDifference(item)
|
||||
return item
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Item,
|
||||
Shop
|
||||
Shop,
|
||||
}
|
||||
|
||||
@ -1,9 +1,115 @@
|
||||
const {Shop, Item} = require("../src/gilded_rose");
|
||||
const { Shop, Item } = 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('foo')
|
||||
})
|
||||
|
||||
it('should return correct result', () => {
|
||||
const storeItems = [
|
||||
new Item('Test Potion', 10, 20),
|
||||
new Item('Aged Brie', 2, 0),
|
||||
new Item('Healing Salve', 5, 7),
|
||||
new Item('Sulfuras', 0, 80),
|
||||
new Item('Backstage passes', 15, 20),
|
||||
]
|
||||
|
||||
const expectedResult = [
|
||||
new Item('Test Potion', 9, 19),
|
||||
new Item('Aged Brie', 1, 1),
|
||||
new Item('Healing Salve', 4, 6),
|
||||
new Item('Sulfuras', 0, 80),
|
||||
new Item('Backstage passes', 14, 21),
|
||||
]
|
||||
const gildedRose = new Shop(storeItems)
|
||||
const items = gildedRose.updateQuality()
|
||||
|
||||
expect(items).toStrictEqual(expectedResult)
|
||||
})
|
||||
|
||||
it('Should normal items quality never be below 0', () => {
|
||||
const storeItems = [new Item('Oblivion staff', 10, 0)]
|
||||
const expectedResult = [new Item('Oblivion staff', 9, 0)]
|
||||
const gildedRose = new Shop(storeItems)
|
||||
const items = gildedRose.updateQuality()
|
||||
|
||||
expect(items).toStrictEqual(expectedResult)
|
||||
})
|
||||
|
||||
it('Should quality degrade twice as fast when the sellIn date passes', () => {
|
||||
const storeItems = [new Item('Oblivion staff', 0, 4)]
|
||||
const expectedResult = [new Item('Oblivion staff', -1, 2)]
|
||||
const gildedRose = new Shop(storeItems)
|
||||
const items = gildedRose.updateQuality()
|
||||
|
||||
expect(items).toStrictEqual(expectedResult)
|
||||
})
|
||||
|
||||
it('Should the quality of an item can never be more than 50', () => {
|
||||
const storeItems = [new Item('Aged Brie', 1, 50)]
|
||||
const expectedResult = [new Item('Aged Brie', 0, 50)]
|
||||
const gildedRose = new Shop(storeItems)
|
||||
const items = gildedRose.updateQuality()
|
||||
|
||||
expect(items).toStrictEqual(expectedResult)
|
||||
})
|
||||
|
||||
it('Should quality of an aged brie should increase by 1', () => {
|
||||
const storeItems = [new Item('Aged Brie', 1, 0)]
|
||||
const expectedResult = [new Item('Aged Brie', 0, 1)]
|
||||
const gildedRose = new Shop(storeItems)
|
||||
const items = gildedRose.updateQuality()
|
||||
|
||||
expect(items).toStrictEqual(expectedResult)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Backstage passes', () => {
|
||||
it("increases in Quality as it's SellIn value approaches", () => {
|
||||
const storeItems = [new Item('Backstage passes', 14, 0)]
|
||||
const expectedResult = [new Item('Backstage passes', 13, 1)]
|
||||
const gildedRose = new Shop(storeItems)
|
||||
const items = gildedRose.updateQuality()
|
||||
|
||||
expect(items).toStrictEqual(expectedResult)
|
||||
})
|
||||
|
||||
it('Quality increases by 2 when there are 10 days or less', () => {
|
||||
const storeItems = [new Item('Backstage passes', 10, 0)]
|
||||
const expectedResult = [new Item('Backstage passes', 9, 2)]
|
||||
const gildedRose = new Shop(storeItems)
|
||||
const items = gildedRose.updateQuality()
|
||||
|
||||
expect(items).toStrictEqual(expectedResult)
|
||||
})
|
||||
|
||||
it('Quality increases by 3 when there are 5 days or less', () => {
|
||||
const storeItems = [new Item('Backstage passes', 5, 0)]
|
||||
const expectedResult = [new Item('Backstage passes', 4, 3)]
|
||||
const gildedRose = new Shop(storeItems)
|
||||
const items = gildedRose.updateQuality()
|
||||
|
||||
expect(items).toStrictEqual(expectedResult)
|
||||
})
|
||||
|
||||
it('Quality drops to 0 after concert', () => {
|
||||
const storeItems = [new Item('Backstage passes', 0, 30)]
|
||||
const expectedResult = [new Item('Backstage passes', -1, 0)]
|
||||
const gildedRose = new Shop(storeItems)
|
||||
const items = gildedRose.updateQuality()
|
||||
|
||||
expect(items).toStrictEqual(expectedResult)
|
||||
})
|
||||
})
|
||||
describe('Conjured items', () => {
|
||||
it('Should quality of conjured items decrease twice as fast', () => {
|
||||
const storeItems = [new Item('Conjured Test Cake', 10, 20)]
|
||||
const expectedResult = [new Item('Conjured Test Cake', 9, 18)]
|
||||
const gildedRose = new Shop(storeItems)
|
||||
const items = gildedRose.updateQuality()
|
||||
|
||||
expect(items).toStrictEqual(expectedResult)
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
Reference in New Issue
Block a user