From 816665ed03b5296713d939be09d51c38fb512183 Mon Sep 17 00:00:00 2001 From: joelrindfleisch Date: Wed, 19 Apr 2023 21:44:40 -0500 Subject: [PATCH] chore(refactor): green tests and conjuring functionality --- js-jest/package-lock.json | 6 ++ js-jest/package.json | 1 + js-jest/src/gilded_rose.js | 114 +++++++++++------------ js-jest/test/gilded_rose.test.js | 153 ++++++++++++++++++++++++++++++- 4 files changed, 214 insertions(+), 60 deletions(-) diff --git a/js-jest/package-lock.json b/js-jest/package-lock.json index b86cc1da..d592fd89 100644 --- a/js-jest/package-lock.json +++ b/js-jest/package-lock.json @@ -885,6 +885,12 @@ "supports-color": "^5.3.0" } }, + "chance": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/chance/-/chance-1.1.11.tgz", + "integrity": "sha512-kqTg3WWywappJPqtgrdvbA380VoXO2eu9VCV895JgbyHsaErXdyHK9LOZ911OvAk6L0obK7kDk9CGs8+oBawVA==", + "dev": true + }, "ci-info": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-2.0.0.tgz", diff --git a/js-jest/package.json b/js-jest/package.json index 2cbdbb45..ba97d183 100644 --- a/js-jest/package.json +++ b/js-jest/package.json @@ -23,6 +23,7 @@ }, "homepage": "https://github.com/emilybache/GildedRose-Refactoring-Kata", "devDependencies": { + "chance": "^1.1.11", "jest": "^24.9.0" } } diff --git a/js-jest/src/gilded_rose.js b/js-jest/src/gilded_rose.js index 48746965..9b990fad 100644 --- a/js-jest/src/gilded_rose.js +++ b/js-jest/src/gilded_rose.js @@ -1,67 +1,65 @@ 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; - } - } - } + constructor(items = []) { + this.items = items; } - return this.items; - } + updateQuality() { + for (let i = 0; i < this.items.length; i++) { + const item = this.items[i]; + if (item.name.toLowerCase().includes('aged brie')) { + if(item.quality < 50) { + item.quality++; + } + item.sellIn--; + } else if (item.name.toLowerCase().includes('backstage passes')) { + if(item.sellIn === 0) { + item.quality = 0; + } else if(item.sellIn >=1 && item.sellIn <= 3) { + const newValue = item.quality + 3; + item.quality = newValue > 50 ? 50 : newValue; + } else if(item.sellIn >= 4 && item.sellIn <= 10) { + const newValue = item.quality + 2; + item.quality = newValue > 50 ? 50 : newValue; + } else { + item.quality--; + } + item.sellIn--; + } else if (item.name.toLowerCase().includes('sulfuras')) { + // do nothing! + } else if (item.name.toLowerCase().includes('conjured')) { + const qualityDecrement = item.sellIn <=0 ? 4 : 2; + const result = item.quality - qualityDecrement; + if(result < 0) { + item.quality = 0; + } else { + item.quality = result; + } + item.sellIn--; + } else { + // normal item + const qualityDecrement = item.sellIn <=0 ? 2 : 1; + const result = item.quality - qualityDecrement; + if(result < 0) { + item.quality = 0; + } else { + item.quality = result; + } + item.sellIn--; + } + } + return this.items; + } } module.exports = { - Item, - Shop -} + Item, + Shop +}; diff --git a/js-jest/test/gilded_rose.test.js b/js-jest/test/gilded_rose.test.js index c8e0e3d7..5b8a6a97 100644 --- a/js-jest/test/gilded_rose.test.js +++ b/js-jest/test/gilded_rose.test.js @@ -1,9 +1,158 @@ const {Shop, Item} = require("../src/gilded_rose"); +const Chance = require('chance'); describe("Gilded Rose", function() { - it("should foo", function() { + let chance; + + beforeEach(() => { + chance = new Chance(); + }); + + it('should decrement quality and sellIn', () => { + const quality = chance.integer({min: 1, max: 50}); + const sellIn = chance.integer({min: 1}); + + const gildedRose = new Shop([new Item("foo", sellIn, quality)]); + const items = gildedRose.updateQuality(); + + expect(items[0].name).toBe("foo"); + expect(items[0].sellIn).toBe(sellIn - 1); + expect(items[0].quality).toBe(quality - 1); + }); + + it('should never have a negative quality', () => { const gildedRose = new Shop([new Item("foo", 0, 0)]); const items = gildedRose.updateQuality(); - expect(items[0].name).toBe("fixme"); + + expect(items[0].name).toBe("foo"); + expect(items[0].sellIn).toBe(-1); + expect(items[0].quality).toBe(0); }); + + + describe('backstage passes', () => { + it('should drop quality to 0 after sell date', () => { + const quality = chance.integer({min: 4, max: 50}); + + const gildedRose = new Shop([new Item("Backstage passes", 0, quality)]); + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(sellIn - 1); + expect(items[0].quality).toBe(quality + 3); + }); + + it.only('should increase quality by 2 when there are between 4 and 10 days to sell', () => { + const quality = chance.integer({min: 1, max: 48}); + const sellIn = chance.integer({min: 4, max: 10}); + + const gildedRose = new Shop([new Item("Backstage passes", sellIn, quality)]); + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(sellIn - 1); + expect(items[0].quality).toBe(quality + 2); + }); + + it('should increase quality by 3 when there are less than 4 days to sell date', () => { + const quality = chance.integer({min: 1, max: 47}); + const sellIn = chance.integer({min: 1, max: 3}); + + const gildedRose = new Shop([new Item("backstage passes", sellIn, quality)]); + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(sellIn - 1); + expect(items[0].quality).toBe(quality + 3); + }); + + it('should not exceed 50 in quality', () => { + const sellIn = chance.integer({min: 1, max: 3}); + + const gildedRose = new Shop([new Item("Backstage passes", sellIn, 49)]); + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(sellIn - 1); + expect(items[0].quality).toBe(50); + }); + }); + + describe('sulfuras', () => { + it('should have no sell by date and quality does not change', () => { + const gildedRose = new Shop([new Item("Sulfuras", null, 80)]); + + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(null); + expect(items[0].quality).toBe(80); + }) + }); + + describe('aged brie', () => { + it('should increase by quality as it gets older', () => { + const quality = chance.integer({min: 1, max: 49}); + const sellIn = chance.integer({min: 1}); + + const gildedRose = new Shop([new Item("Aged Brie", sellIn, quality)]); + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(sellIn - 1); + expect(items[0].quality).toBe(quality + 1); + }); + + it('should not exceed 50 quality', () => { + const sellIn = chance.integer({min: 1}); + + const gildedRose = new Shop([new Item("Aged Brie", sellIn, 50)]); + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(sellIn - 1); + expect(items[0].quality).toBe(50); + }); + }); + + describe('after sell by date', () => { + it('should decrease quality twice as fast', () => { + const quality = chance.integer({min: 2, max: 50}); + + const gildedRose = new Shop([new Item("foo", 0, quality)]); + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(-1); + expect(items[0].quality).toBe(quality - 2); + }); + }); + + describe('conjured items', () => { + it('should degrade quality twice as fast as normal items', () => { + const quality = chance.integer({min: 2, max: 50}); + const sellIn = chance.integer({min: 1}); + + const gildedRose = new Shop([new Item("Conjured", sellIn, quality)]); + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(sellIn - 1); + expect(items[0].quality).toBe(quality - 2); + }); + + it('should degrade quality twice as fast as normal expired items', () => { + const quality = chance.integer({min: 4, max: 50}); + const sellIn = chance.integer({max: 0}); + + const gildedRose = new Shop([new Item("Conjured", sellIn, quality)]); + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(sellIn - 1); + expect(items[0].quality).toBe(quality - 4); + }); + + it('should never have a negative quality', () => { + const gildedRose = new Shop([new Item("Conjured", 0, 0)]); + const items = gildedRose.updateQuality(); + + expect(items[0].sellIn).toBe(-1); + expect(items[0].quality).toBe(0); + }); + + }); + + + });