mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
refactor(js-jest): add tests, refactor and fix conjured
This commit is contained in:
parent
1a81118c63
commit
b7c207ae49
6080
js-jest/package-lock.json
generated
6080
js-jest/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,67 +1,68 @@
|
||||
class Item {
|
||||
constructor(name, sellIn, quality){
|
||||
constructor(name, sellIn, quality) {
|
||||
this.name = name;
|
||||
this.sellIn = sellIn;
|
||||
this.quality = quality;
|
||||
}
|
||||
}
|
||||
|
||||
const rules = [
|
||||
{
|
||||
pattern: /^Aged Brie/,
|
||||
quality: (itemQuality, itemSellIn) => itemQuality + 1,
|
||||
sellIn: (itemQuality, itemSellIn) => itemSellIn - 1,
|
||||
},
|
||||
{
|
||||
pattern: /^Sulfuras/,
|
||||
quality: (itemQuality, itemSellIn) => itemQuality,
|
||||
sellIn: (itemQuality, itemSellIn) => itemSellIn,
|
||||
},
|
||||
{
|
||||
pattern: /^Backstage passes/,
|
||||
quality: (itemQuality, itemSellIn) => {
|
||||
if (itemSellIn <= 0) {
|
||||
return 0;
|
||||
} else if (itemSellIn < 6) {
|
||||
return itemQuality + 3;
|
||||
} else if (itemSellIn < 11) {
|
||||
return itemQuality + 2;
|
||||
} else {
|
||||
return itemQuality + 1;
|
||||
}
|
||||
},
|
||||
sellIn: (itemQuality, itemSellIn) => itemSellIn - 1,
|
||||
},
|
||||
{
|
||||
pattern: /^Conjured/,
|
||||
quality: (itemQuality, itemSellIn) => itemQuality - 2,
|
||||
sellIn: (itemQuality, itemSellIn) => itemSellIn - 1,
|
||||
},
|
||||
{
|
||||
pattern: "*",
|
||||
quality: (itemQuality, itemSellIn) => itemQuality - 1,
|
||||
sellIn: (itemQuality, itemSellIn) => itemSellIn - 1,
|
||||
},
|
||||
];
|
||||
class Shop {
|
||||
constructor(items=[]){
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.items = this.items.map((item) => {
|
||||
for (let i = 0; i < rules.length; i++) {
|
||||
const rule = rules[i];
|
||||
if (rule.pattern === "*" || item.name.match(rule.pattern)) {
|
||||
item.sellIn = rule.sellIn(item.quality, item.sellIn);
|
||||
item.quality = rule.quality(item.quality, item.sellIn);
|
||||
break;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return item;
|
||||
});
|
||||
return this.items;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Item,
|
||||
Shop
|
||||
}
|
||||
Shop,
|
||||
};
|
||||
|
||||
@ -1,9 +1,114 @@
|
||||
const {Shop, Item} = require("../src/gilded_rose");
|
||||
const { Shop, Item } = require("../src/gilded_rose");
|
||||
// require("./texttest_fixture");
|
||||
|
||||
describe("Gilded Rose", function() {
|
||||
it("should foo", function() {
|
||||
describe("Gilded Rose", function () {
|
||||
let gildedRose;
|
||||
beforeEach(function () {
|
||||
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),
|
||||
];
|
||||
gildedRose = new Shop(items);
|
||||
});
|
||||
it("should preserve the name of items when updating quality", function () {
|
||||
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");
|
||||
});
|
||||
describe("plain items", function () {
|
||||
it("should decrease quality every day", function () {
|
||||
gildedRose.updateQuality();
|
||||
const updatedItems = gildedRose.updateQuality();
|
||||
expect(updatedItems[0].quality).toBe(18);
|
||||
});
|
||||
it("should decrease sellIn days every day", function () {
|
||||
gildedRose.updateQuality();
|
||||
const updatedItems = gildedRose.updateQuality();
|
||||
expect(updatedItems[0].sellIn).toBe(8);
|
||||
});
|
||||
});
|
||||
describe("aged Bried", function () {
|
||||
it("should increase quality every day", function () {
|
||||
gildedRose.updateQuality();
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[1].quality).toBe(2);
|
||||
});
|
||||
|
||||
it("should decrease sellIn days every day", function () {
|
||||
gildedRose.updateQuality();
|
||||
const updatedItems = gildedRose.updateQuality();
|
||||
expect(updatedItems[1].sellIn).toBe(0);
|
||||
});
|
||||
});
|
||||
describe("Sulfuras", function () {
|
||||
it("should never decrease in quality", function () {
|
||||
gildedRose.updateQuality();
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[3].quality).toBe(80);
|
||||
expect(items[4].quality).toBe(80);
|
||||
});
|
||||
|
||||
it("should never decrease sellIn days", function () {
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[3].sellIn).toBe(0);
|
||||
expect(items[4].sellIn).toBe(-1);
|
||||
});
|
||||
});
|
||||
describe("Backstage passes", function () {
|
||||
it("should increase in quality by 3 every day when there are 5 days or less left", function () {
|
||||
gildedRose.updateQuality();
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[7].quality).toBe(55);
|
||||
});
|
||||
it("should increase in quality by 2 every day when there are between 6 and 10 days left", function () {
|
||||
gildedRose.updateQuality();
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[6].quality).toBe(53);
|
||||
});
|
||||
it("should increase in quality by 1 every day when there are more than 10 days left", function () {
|
||||
gildedRose.updateQuality();
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[5].quality).toBe(22);
|
||||
});
|
||||
it("should drop quality to zero after the concert", function () {
|
||||
gildedRose.updateQuality();
|
||||
gildedRose.updateQuality();
|
||||
gildedRose.updateQuality();
|
||||
gildedRose.updateQuality();
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[7].sellIn).toBe(0);
|
||||
expect(items[7].quality).toBe(0);
|
||||
});
|
||||
|
||||
it("should decrease sellIn days every day", function () {
|
||||
gildedRose.updateQuality();
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[5].sellIn).toBe(13);
|
||||
expect(items[6].sellIn).toBe(8);
|
||||
expect(items[7].sellIn).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Conjured", function () {
|
||||
it("should increase in quality by 2 every day", function () {
|
||||
gildedRose.updateQuality();
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[8].quality).toBe(2);
|
||||
});
|
||||
|
||||
it("should decrease sellIn days every day", function () {
|
||||
gildedRose.updateQuality();
|
||||
const items = gildedRose.updateQuality();
|
||||
expect(items[8].sellIn).toBe(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
|
||||
const { Shop, Item } = require("../src/gilded_rose");
|
||||
|
||||
const items = [
|
||||
@ -22,6 +21,8 @@ console.log("OMGHAI!");
|
||||
for (let day = 0; day < days; day++) {
|
||||
console.log(`\n-------- day ${day} --------`);
|
||||
console.log("name, sellIn, quality");
|
||||
items.forEach(item => console.log(`${item.name}, ${item.sellIn}, ${item.quality}`));
|
||||
items.forEach((item) =>
|
||||
console.log(`${item.name}, ${item.sellIn}, ${item.quality}`)
|
||||
);
|
||||
gildedRose.updateQuality();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user