Make tests great again

This commit is contained in:
Audrius Baltusis 2020-09-08 18:02:37 +03:00
parent 1c94a43a2d
commit 748b1a0313

View File

@ -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;
}, {})
}
}
});