Added starndjs for linting, resolved initial errors, tested inheritance approach to tackle constraint of not touching the Item class

This commit is contained in:
Benjamin Barreto 2020-10-21 13:42:37 +02:00
parent e2c5826e71
commit 9cbeedd736
6 changed files with 2976 additions and 52 deletions

1
js-jest/.gitignore vendored
View File

@ -1 +1,2 @@
/node_modules/ /node_modules/
.vscode

2882
js-jest/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -23,6 +23,18 @@
}, },
"homepage": "https://github.com/emilybache/GildedRose-Refactoring-Kata", "homepage": "https://github.com/emilybache/GildedRose-Refactoring-Kata",
"devDependencies": { "devDependencies": {
"jest": "^24.9.0" "babel-eslint": "^10.1.0",
"jest": "^24.9.0",
"standard": "^5.0.0"
},
"standard": {
"parser": "babel-eslint",
"env": [
"jest",
"node"
],
"ignore": [
"node_modules"
]
} }
} }

View File

@ -1,67 +1,95 @@
/**
* Do not alter the Item class or Items property as those belong to the goblin in the corner who
* will insta-rage and one-shot you as he doesn't believe in shared code ownership
*/
class Item { class Item {
constructor(name, sellIn, quality){ constructor (name, sellIn, quality) {
this.name = name; this.name = name
this.sellIn = sellIn; this.sellIn = sellIn
this.quality = quality; this.quality = quality
}
}
class Blade extends Item {
constructor(...args) {
super(...args)
}
whatAmI() {
console.log(`[Blade] I am a Blade with: name: ${this.name}, sellIn: ${this.sellIn}, quality: ${this.quality}`)
}
}
class Sword extends Blade {
constructor(...args) {
super(...args)
}
whoAmI() {
console.log('[Sword] I am a sword! AND...')
this.whatAmI()
} }
} }
class Shop { class Shop {
constructor(items=[]){ constructor (items = []) {
this.items = items; this.items = items
} }
updateQuality() {
updateQuality () {
for (let i = 0; i < this.items.length; i++) { 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].name !== 'Aged Brie' && this.items[i].name !== 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].quality > 0) { if (this.items[i].quality > 0) {
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { if (this.items[i].name !== 'Sulfuras, Hand of Ragnaros') {
this.items[i].quality = this.items[i].quality - 1; this.items[i].quality = this.items[i].quality - 1
} }
} }
} else { } else {
if (this.items[i].quality < 50) { if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1; this.items[i].quality = this.items[i].quality + 1
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') { if (this.items[i].name === 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].sellIn < 11) { if (this.items[i].sellIn < 11) {
if (this.items[i].quality < 50) { if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1; this.items[i].quality = this.items[i].quality + 1
} }
} }
if (this.items[i].sellIn < 6) { if (this.items[i].sellIn < 6) {
if (this.items[i].quality < 50) { if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1; this.items[i].quality = this.items[i].quality + 1
} }
} }
} }
} }
} }
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { if (this.items[i].name !== 'Sulfuras, Hand of Ragnaros') {
this.items[i].sellIn = this.items[i].sellIn - 1; this.items[i].sellIn = this.items[i].sellIn - 1
} }
if (this.items[i].sellIn < 0) { if (this.items[i].sellIn < 0) {
if (this.items[i].name != 'Aged Brie') { if (this.items[i].name !== 'Aged Brie') {
if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') { if (this.items[i].name !== 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].quality > 0) { if (this.items[i].quality > 0) {
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { if (this.items[i].name !== 'Sulfuras, Hand of Ragnaros') {
this.items[i].quality = this.items[i].quality - 1; this.items[i].quality = this.items[i].quality - 1
} }
} }
} else { } else {
this.items[i].quality = this.items[i].quality - this.items[i].quality; this.items[i].quality = this.items[i].quality - this.items[i].quality
} }
} else { } else {
if (this.items[i].quality < 50) { if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1; this.items[i].quality = this.items[i].quality + 1
} }
} }
} }
} }
return this.items; return this.items
} }
} }
module.exports = { module.exports = {
Item, Item,
Shop Shop,
Sword
} }

View File

@ -1,9 +1,9 @@
const {Shop, Item} = require("../src/gilded_rose"); const { Shop, Item } = require('../src/gilded_rose')
describe("Gilded Rose", function() { describe('Gilded Rose', function () {
it("should foo", function() { it('should foo', function () {
const gildedRose = new Shop([new Item("foo", 0, 0)]); const gildedRose = new Shop([new Item('foo', 0, 0)])
const items = gildedRose.updateQuality(); const items = gildedRose.updateQuality()
expect(items[0].name).toBe("fixme"); expect(items[0].name).toBe('fixme')
}); })
}); })

View File

@ -1,27 +1,30 @@
const { Shop, Item } = require("../src/gilded_rose"); const { Shop, Item, Sword } = require('../src/gilded_rose')
const items = [ const items = [
new Item("+5 Dexterity Vest", 10, 20), new Item('+5 Dexterity Vest', 10, 20),
new Item("Aged Brie", 2, 0), new Item('Aged Brie', 2, 0),
new Item("Elixir of the Mongoose", 5, 7), new Item('Elixir of the Mongoose', 5, 7),
new Item("Sulfuras, Hand of Ragnaros", 0, 80), new Item('Sulfuras, Hand of Ragnaros', 0, 80),
new Item("Sulfuras, Hand of Ragnaros", -1, 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', 15, 20),
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49), new Item('Backstage passes to a TAFKAL80ETC concert', 10, 49),
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49), new Item('Backstage passes to a TAFKAL80ETC concert', 5, 49),
// This Conjured item does not work properly yet // This Conjured item does not work properly yet
new Item("Conjured Mana Cake", 3, 6), new Item('Conjured Mana Cake', 3, 6)
]; ]
const days = Number(process.argv[2]) || 2; const mySword = new Sword('Awesome Sword', 10, 20)
const gildedRose = new Shop(items); mySword.whoAmI()
console.log("OMGHAI!"); const days = Number(process.argv[2]) || 2
const gildedRose = new Shop(items)
console.log('OMGHAI!')
for (let day = 0; day < days; day++) { for (let day = 0; day < days; day++) {
console.log(`\n-------- day ${day} --------`); console.log(`\n-------- day ${day} --------`)
console.log("name, sellIn, quality"); 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(); gildedRose.updateQuality()
} }