diff --git a/js-jest/src/gilded_rose.js b/js-jest/src/gilded_rose.js index 48746965..99afbf73 100644 --- a/js-jest/src/gilded_rose.js +++ b/js-jest/src/gilded_rose.js @@ -12,51 +12,31 @@ class Shop { } 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].name !== 'Sulfuras') { + if (this.items[i].name !== 'Aged Brie' && this.items[i].name !== 'Backstage passes') { + if (this.items[i].sellIn <= 0) { + if(this.items[i].name === 'Conjured' && this.items[i].quality - 4 > 0) { + this.items[i].quality = this.items[i].quality - 4; + } else if(this.items[i].quality - 2 > 0){ + this.items[i].quality = this.items[i].quality - 2; } - 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) { + if ((this.items[i].sellIn <= 10 && this.items[i].sellIn >5) && this.items[i].quality + 2 <= 50) { + this.items[i].quality = this.items[i].quality + 2; + } else if ((this.items[i].sellIn <= 5 && this.items[i].sellIn > 0) && this.items[i].quality + 3 <= 50) { + this.items[i].quality = this.items[i].quality + 3; + } else if(this.items[i].quality < 50) { this.items[i].quality = this.items[i].quality + 1; } } + } else { + if(this.items[i].quality < 80) { + this.items[i].quality = 80; + } } } - return this.items; } } diff --git a/js-jest/test/gilded_rose.test.js b/js-jest/test/gilded_rose.test.js index c8e0e3d7..b1e4be52 100644 --- a/js-jest/test/gilded_rose.test.js +++ b/js-jest/test/gilded_rose.test.js @@ -1,9 +1,68 @@ const {Shop, Item} = require("../src/gilded_rose"); -describe("Gilded Rose", function() { - it("should foo", function() { - const gildedRose = new Shop([new Item("foo", 0, 0)]); +describe("Gilded Rose with SellIn Zero", function() { + it("Check Sulfuras quantity", function() { + const gildedRose = new Shop([new Item("Sulfuras", 0, 78)]); const items = gildedRose.updateQuality(); - expect(items[0].name).toBe("fixme"); + expect(items[0].quality).toBe(80); + }) + + it("Aged Brie to increase if SellIn is 0", function() { + const gildedRose = new Shop([new Item("Aged Brie", 0, 20)]); + const items = gildedRose.updateQuality(); + expect(items[0].quality).toBe(21); }); + + it("Other item to decrease twice if SellIn is 0", function() { + const gildedRose = new Shop([new Item("Other item 1", 0, 20)]); + const items = gildedRose.updateQuality(); + expect(items[0].quality).toBe(18); + }); + + it("Conjured to decrease twice of normal items if SellIn is 0", function() { + const gildedRose = new Shop([new Item("Conjured", 0, 20)]); + const items = gildedRose.updateQuality(); + expect(items[0].quality).toBe(16); + }); + + it("Backstage passes to increase if SellIn is 0", function() { + const gildedRose = new Shop([new Item("Backstage passes", 0, 20)]); + const items = gildedRose.updateQuality(); + expect(items[0].quality).toBe(21); + }); + }); + + +describe("Gilded Rose with SellIn Non Zero", function() { + it("Check Sulfuras quantity", function() { + const gildedRose = new Shop([new Item("Sulfuras", 2, 78)]); + const items = gildedRose.updateQuality(); + expect(items[0].quality).toBe(80); + }) + + it("Aged Brie to increase if SellIn is 4", function() { + const gildedRose = new Shop([new Item("Aged Brie", 4, 20)]); + const items = gildedRose.updateQuality(); + expect(items[0].quality).toBe(23); + }); + + it("Other item quantity no change if SellIn is greater than 0", function() { + const gildedRose = new Shop([new Item("Other item 1", 5, 20)]); + const items = gildedRose.updateQuality(); + expect(items[0].quality).toBe(20); + }); + + it("Conjured quantity no change if SellIn is greater than 0", function() { + const gildedRose = new Shop([new Item("Conjured", 6, 20)]); + const items = gildedRose.updateQuality(); + expect(items[0].quality).toBe(20); + }); + + it("Backstage passes to increase if SellIn is 8", function() { + const gildedRose = new Shop([new Item("Backstage passes", 8, 20)]); + const items = gildedRose.updateQuality(); + expect(items[0].quality).toBe(22); + }); + +}); \ No newline at end of file