From 748b1a0313d5851fa04615d183cc3e71c4caebaa Mon Sep 17 00:00:00 2001 From: Audrius Baltusis Date: Tue, 8 Sep 2020 18:02:37 +0300 Subject: [PATCH] Make tests great again --- js-jasmine/spec/gilded_rose_spec.js | 85 ++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 7 deletions(-) diff --git a/js-jasmine/spec/gilded_rose_spec.js b/js-jasmine/spec/gilded_rose_spec.js index 9f267c13..5f1a9e65 100644 --- a/js-jasmine/spec/gilded_rose_spec.js +++ b/js-jasmine/spec/gilded_rose_spec.js @@ -1,10 +1,81 @@ -var {Shop, Item} = require('../src/gilded_rose.js'); -describe("Gilded Rose", function() { +const {Shop, Item} = require('../src/gilded_rose.js'); - it("should foo", function() { - const gildedRose = new Shop([ new Item("foo", 0, 0) ]); - const items = gildedRose.updateQuality(); - expect(items[0].name).toEqual("fixme"); - }); +const items = [ + new Item("+5 Dexterity Vest", 10, 20), + new Item("Aged Brie", 2, 0), + new Item("Elixir of the Mongoose", 5, 7), + new Item("Sulfuras, Hand of Ragnaros", 0, 80), + new Item("Sulfuras, Hand of Ragnaros", -1, 80), + new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20), + new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49), + new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49), + // This Conjured item does not work properly yet + new Item("Conjured Mana Cake", 3, 6), +]; + +describe("Gilded Rose", function () { + it("should day 1", function () { + const gildedRose = new Shop(deepCopy(items)); + const result = getItems(gildedRose, items, 1); + + expect(result).toEqual([ + jasmine.objectContaining({name: '+5 Dexterity Vest', sellIn: 9, quality: 19}), + jasmine.objectContaining({name: 'Aged Brie', sellIn: 1, quality: 1}), + jasmine.objectContaining({name: 'Elixir of the Mongoose', sellIn: 4, quality: 6}), + jasmine.objectContaining({name: 'Sulfuras, Hand of Ragnaros', sellIn: 0, quality: 80}), + jasmine.objectContaining({name: 'Sulfuras, Hand of Ragnaros', sellIn: -1, quality: 80}), + jasmine.objectContaining({name: 'Backstage passes to a TAFKAL80ETC concert', sellIn: 14, quality: 21}), + jasmine.objectContaining({name: 'Backstage passes to a TAFKAL80ETC concert', sellIn: 9, quality: 50}), + jasmine.objectContaining({name: 'Backstage passes to a TAFKAL80ETC concert', sellIn: 4, quality: 50}), + jasmine.objectContaining({name: 'Conjured Mana Cake', sellIn: 2, quality: 5}) + ]); + }); + + it("should day 2", function () { + const gildedRose = new Shop(deepCopy(items)); + const result = getItems(gildedRose, items, 2); + + expect(result).toEqual([ + jasmine.objectContaining({name: '+5 Dexterity Vest', sellIn: 8, quality: 18}), + jasmine.objectContaining({name: 'Aged Brie', sellIn: 0, quality: 2}), + jasmine.objectContaining({name: 'Elixir of the Mongoose', sellIn: 3, quality: 5}), + jasmine.objectContaining({name: 'Sulfuras, Hand of Ragnaros', sellIn: 0, quality: 80}), + jasmine.objectContaining({name: 'Sulfuras, Hand of Ragnaros', sellIn: -1, quality: 80}), + jasmine.objectContaining({name: 'Backstage passes to a TAFKAL80ETC concert', sellIn: 13, quality: 22}), + jasmine.objectContaining({name: 'Backstage passes to a TAFKAL80ETC concert', sellIn: 8, quality: 50}), + jasmine.objectContaining({name: 'Backstage passes to a TAFKAL80ETC concert', sellIn: 3, quality: 50}), + jasmine.objectContaining({name: 'Conjured Mana Cake', sellIn: 1, quality: 4}) + ]); + }); + + function getItems(shop, items, days) { + return days ? [...Array(days)].map(() => shop.updateQuality())[days - 1] : items; + } + + function deepCopy(obj) { + // Prevent original items from mutation and make tests independent + // https://stackoverflow.com/a/53771927 + if (typeof obj !== 'object' || obj === null) { + return obj; + } + + if (obj instanceof Date) { + return new Date(obj.getTime()); + } + + if (obj instanceof Array) { + return obj.reduce((arr, item, i) => { + arr[i] = deepCopy(item); + return arr; + }, []); + } + + if (obj instanceof Object) { + return Object.keys(obj).reduce((newObj, key) => { + newObj[key] = deepCopy(obj[key]); + return newObj; + }, {}) + } + } });