diff --git a/js/spec/gilded_rose_spec.js b/js/spec/gilded_rose_spec.js index 44c3838b..1d9c7502 100644 --- a/js/spec/gilded_rose_spec.js +++ b/js/spec/gilded_rose_spec.js @@ -1,9 +1,98 @@ -describe("Gilded Rose", function() { - - it("should foo", function() { - const gildedRose = new Shop([ new Item("foo", 0, 0) ]); +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).toEqual("fixme"); + expect(items[0].name).toEqual('foo'); }); + it('can updateQuality for Backstage Passes', () => { + const gildedRose = new Shop([ + new Item('Backstage passes to a TAFKAL80ETC concert', 15, 20), + new Item('Backstage passes to a TAFKAL80ETC concert', 10, 46), + new Item('Backstage passes to a TAFKAL80ETC concert', 5, 49) + ]); + const items = gildedRose.updateQuality(); + expect(items[0].name).toEqual('Backstage passes to a TAFKAL80ETC concert'); + expect(items[0].sellIn).toEqual(14); + expect(items[0].quality).toEqual(21); + + expect(items[1].name).toEqual('Backstage passes to a TAFKAL80ETC concert'); + expect(items[1].sellIn).toEqual(9); + expect(items[1].quality).toEqual(48); + + expect(items[2].name).toEqual('Backstage passes to a TAFKAL80ETC concert'); + expect(items[2].sellIn).toEqual(4); + expect(items[2].quality).toEqual(50); + }); + + it('can updateQuality for Aged Brie', () => { + const gildedRose = new Shop([new Item('Aged Brie', 2, 0)]); + const items = gildedRose.updateQuality(); + expect(items[0].name).toEqual('Aged Brie'); + expect(items[0].sellIn).toEqual(1); + expect(items[0].quality).toEqual(1); + }); + + it('can updateQuality for Sulfuras', () => { + const gildedRose = new Shop([ + new Item('Sulfuras, Hand of Ragnaros', 0, 80), + new Item('Sulfuras, Hand of Ragnaros', -1, 80) + ]); + const items = gildedRose.updateQuality(); + expect(items[0].name).toEqual('Sulfuras, Hand of Ragnaros'); + expect(items[0].sellIn).toEqual(-1); + expect(items[0].quality).toEqual(80); + + expect(items[1].name).toEqual('Sulfuras, Hand of Ragnaros'); + expect(items[1].sellIn).toEqual(-2); + expect(items[1].quality).toEqual(80); + }); + + it('can updateQuality for basic items', () => { + const gildedRose = new Shop([ + new Item('+5 Dexterity Vest', 10, 20), + new Item('Elixir of the Mongoose', 5, 7) + ]); + + const items = gildedRose.updateQuality(); + expect(items[0].name).toEqual('+5 Dexterity Vest'); + expect(items[0].sellIn).toEqual(9); + expect(items[0].quality).toEqual(19); + + expect(items[1].name).toEqual('Elixir of the Mongoose'); + expect(items[1].sellIn).toEqual(4); + expect(items[1].quality).toEqual(6); + }); + + it('can updateQuality for conjured items', () => { + const gildedRose = new Shop([new Item('Conjured Mana Cake', 3, 6)]); + const items = gildedRose.updateQuality(); + expect(items[0].sellIn).toEqual(2); + expect(items[0].quality).toEqual(4); + }); + + it('Once the sell by date has passed, Quality degrades twice as fast', () => { + const gildedRose = new Shop([new Item('+5 Dexterity Vest', -2, 20)]); + + const items = gildedRose.updateQuality(); + expect(items[0].sellIn).toEqual(-3); + expect(items[0].quality).toEqual(18); + }); + + it('The Quality of an item is never negative', () => { + const gildedRose = new Shop([new Item('+5 Dexterity Vest', -2, 1)]); + + const items = gildedRose.updateQuality(); + expect(items[0].name).toEqual('+5 Dexterity Vest'); + expect(items[0].sellIn).toEqual(-3); + expect(items[0].quality).toEqual(0); + }); + + it('will not increase quality over 50', () => { + const gildedRose = new Shop([new Item('Aged Brie', -1, 50)]); + const items = gildedRose.updateQuality(); + expect(items[0].name).toEqual('Aged Brie'); + expect(items[0].sellIn).toEqual(-2); + expect(items[0].quality).toEqual(50); + }); }); diff --git a/js/src/gilded_rose.js b/js/src/gilded_rose.js index 5358125e..ce27e06b 100644 --- a/js/src/gilded_rose.js +++ b/js/src/gilded_rose.js @@ -1,5 +1,5 @@ class Item { - constructor(name, sellIn, quality){ + constructor(name, sellIn, quality) { this.name = name; this.sellIn = sellIn; this.quality = quality; @@ -7,56 +7,55 @@ class Item { } class Shop { - constructor(items=[]){ + constructor(items = []) { this.items = items; } - updateQuality() { - for (var 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; - } - } + increaseQuality(item) { + if (item.quality < 50) { + item.quality++; + } + return item; + } + decreaseQuality(item) { + if (item.quality > 0) { + if (item.sellIn < 0 || item.name === 'Conjured Mana Cake') { + item.quality = item.quality - 2 > 0 ? item.quality - 2 : 0; } 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; - } - } + item.quality--; } } + return item; + } + updateQuality() { + this.items.map(item => { + switch (item.name) { + case 'Aged Brie': + this.increaseQuality(item); + item.sellIn--; + break; + case 'Backstage passes to a TAFKAL80ETC concert': + if (item.sellIn > 0) { + this.increaseQuality(item); + if (item.sellIn < 11) { + this.increaseQuality(item); + } + if (item.sellIn < 6) { + this.increaseQuality(item); + } + } else { + item.quality = 0; + } + item.sellIn--; + break; + case 'Sulfuras, Hand of Ragnaros': + item.sellIn--; + break; + default: + this.decreaseQuality(item); + item.sellIn--; + } + }); return this.items; } }