diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9d7d9f2..7ce41e4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,5 +1,23 @@ name: Run Tests on: + push: + branches: + - main + + pull_request: + branches: + - main jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Setup + uses: actions/setup-node@v2 + with: + node-version: '18' + + - name: Run the tests + run: npm run test:jest diff --git a/app/gilded-rose.ts b/app/gilded-rose.ts index ee55134..fd6dc46 100644 --- a/app/gilded-rose.ts +++ b/app/gilded-rose.ts @@ -17,53 +17,46 @@ export class GildedRose { this.items = items; } + updateConcertTicket(item: Item) { + item.quality += 1 + if (item.sellIn < 11) + item.quality += 1 + + if (item.sellIn < 6) + item.quality += 1 + + if (item.quality >= 50) + item.quality = 50 + } + 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 - } - } + if (this.items[i].name == 'Sulfuras, Hand of Ragnaros') + break + + this.items[i].sellIn -= 1 + + if (this.items[i].quality == 50 || this.items[i].quality == 0) + break + + if (this.items[i].name == 'Aged Brie') { + this.items[i].quality += 1 + break + } else if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') { + this.updateConcertTicket(this.items[i]) + } else if (this.items[i].name == 'Conjured') { + this.items[i].quality -= 2 } 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; + this.items[i].quality -= 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 - } - } + if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') + this.items[i].quality = 0 } + } return this.items; } -} \ No newline at end of file +} diff --git a/test/jest/gilded-rose.spec.ts b/test/jest/gilded-rose.spec.ts index 613639f..752a723 100644 --- a/test/jest/gilded-rose.spec.ts +++ b/test/jest/gilded-rose.spec.ts @@ -9,7 +9,7 @@ describe('Gilded Rose', () => { const items = gildedRose.updateQuality(); // Assert - expect(items[0].name).toBe('bar'); + expect(items[0].name).toBe('foo'); }); it('sword quality drops by 1', () => { @@ -20,6 +20,41 @@ describe('Gilded Rose', () => { const items = gildedRose.updateQuality(); // Assert - expect(items[0].quality).toBe(1); + expect(items[0].quality).toBe(0); + }) + + it('Sulfuras quality doesn\'t drop', () => { + const gildedRose = new GildedRose([new Item('Sulfuras, Hand of Ragnaros', 0, 80)]); + + const items = gildedRose.updateQuality(); + + expect(items[0].quality).toBe(80); + }) + + it('Aged brie increases in Quality', () => { + const gildedRose = new GildedRose([new Item('Aged Brie', 10, 30)]); + + const items = gildedRose.updateQuality(); + + expect(items[0].quality).toBe(31); + }) + + it('Item quality doesn\'t exceed 50/drop below 0', () => { + const gildedRose1 = new GildedRose([new Item('Shield', 10, 0)]); + const gildedRose2 = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 5, 48)]); + + const item1 = gildedRose1.updateQuality(); + const item2 = gildedRose2.updateQuality(); + + expect(item1[0].quality).toBe(0); + expect(item2[0].quality).toBe(50); + }) + + it('Conjured items degrade twice as fast', () => { + const gildedRose = new GildedRose([new Item('Conjured', 5, 20)]); + + const items = gildedRose.updateQuality(); + + expect(items[0].quality).toBe(18); }) });