Dissected original logic, tested private variables, fixed stardjs linting

This commit is contained in:
Benjamin Barreto 2020-10-21 16:20:22 +02:00
parent b2954e27ff
commit 79844c8a50
5 changed files with 1385 additions and 2443 deletions

3743
js-jest/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,7 @@
"version": "1.0.0", "version": "1.0.0",
"description": "Gilded Rose kata in JavaScript with Jest", "description": "Gilded Rose kata in JavaScript with Jest",
"scripts": { "scripts": {
"start": "node ./test/texttest_fixture.js",
"test": "jest", "test": "jest",
"test:watch": "jest --watch", "test:watch": "jest --watch",
"test:coverage": "jest --coverage" "test:coverage": "jest --coverage"
@ -25,13 +26,12 @@
"devDependencies": { "devDependencies": {
"babel-eslint": "^10.1.0", "babel-eslint": "^10.1.0",
"jest": "^24.9.0", "jest": "^24.9.0",
"standard": "^5.0.0" "standardx": "^5.0.0"
}, },
"standard": { "standardx": {
"parser": "babel-eslint", "parser": "babel-eslint",
"env": [ "env": [
"jest", "node", "jest"
"node"
], ],
"ignore": [ "ignore": [
"node_modules" "node_modules"

View File

@ -18,15 +18,29 @@ class RegularItem extends Item {
this.validateItemProps(itemProps) this.validateItemProps(itemProps)
} }
validateItemProps({ name, sellIn, quality }) { _privateVar = 0
const errors = [];
get privateVar () {
return this._privateVar
}
set privateVar (value) {
console.log(`entered set privateVar with value: ${value}`)
this._privateVar = value
}
validateItemProps ({ name, sellIn, quality }) {
const errors = []
const isNameValid = typeof name === 'string' && name.length const isNameValid = typeof name === 'string' && name.length
const isSellInValid = typeof sellIn === 'number' && sellIn > 0 const isSellInValid = typeof sellIn === 'number'
const isQualityValid = typeof quality === 'number' && quality >= 0 && quality <=50
// "The Quality of an item is never negative"
// "The Quality of an item is never more than 50"
const isQualityValid = typeof quality === 'number' && quality >= 0 && quality <= 50
!isNameValid && errors.push('"name" must be a valid, non-empty string') !isNameValid && errors.push('"name" must be a valid, non-empty string')
!isSellInValid && errors.push('"sellIn" must be an integer, greater than zero') !isSellInValid && errors.push('"sellIn" must be an integer')
!isQualityValid && errors.push('"qualityValid" must be an integer, between 0 and 50') !isQualityValid && errors.push('"qualityValid" must be an integer, between 0 and 50')
if (errors.length) { if (errors.length) {
@ -35,60 +49,50 @@ class RegularItem extends Item {
} }
} }
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 () {
// Iterate list of items
for (let i = 0; i < this.items.length; i++) { for (let i = 0; i < this.items.length; i++) {
// NOT cheese && NOT pass && quality > 0 && NOT sulfuras, therefore RegularItem
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') {
// RegularItems decrement quality by 1
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) {
// Increment quality by 1 for all non RegularItems
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 sellIn is less than 11 for passes, net quality increase is 2
} }
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 sellIn is less than 6 for passes, net quality increase is 3
} }
} }
} }
} }
// Unnecessary decrement of sellIn
// "Sulfuras", being a legendary item, never has to be sold or decreases in Quality"
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') {
@ -115,6 +119,5 @@ class Shop {
module.exports = { module.exports = {
Item, Item,
Shop, Shop,
Sword,
RegularItem RegularItem
} }

View File

@ -1,4 +1,4 @@
const { Shop, Item, RegularItem } = require('../src/gilded_rose') const { RegularItem } = require('../src/gilded_rose')
// describe('Gilded Rose', function () { // describe('Gilded Rose', function () {
// it('should foo', function () { // it('should foo', function () {
@ -21,11 +21,10 @@ describe('RegularItem', () => {
}) })
it('should throw an error if initialized with an invalid sellIn property', () => { it('should throw an error if initialized with an invalid sellIn property', () => {
expect(getRegularItemFactory({ sellIn: -1 })).toThrow() expect(getRegularItemFactory({ sellIn: '' })).toThrow()
expect(getRegularItemFactory({ sellIn: 0 })).toThrow()
}) })
it('should throw an error if initialized with a quality property < 0 || > 50', () => { it('should throw an error if initialized with a quality property < 0 OR > 50', () => {
expect(getRegularItemFactory({ quality: -1 })).toThrow() expect(getRegularItemFactory({ quality: -1 })).toThrow()
expect(getRegularItemFactory({ quality: 51 })).toThrow() expect(getRegularItemFactory({ quality: 51 })).toThrow()
}) })

View File

@ -1,5 +1,5 @@
const { Shop, Item, Sword } = require('../src/gilded_rose') const { Shop, Item, RegularItem } = require('../src/gilded_rose')
const items = [ const items = [
new Item('+5 Dexterity Vest', 10, 20), new Item('+5 Dexterity Vest', 10, 20),
@ -15,8 +15,15 @@ const items = [
new Item('Conjured Mana Cake', 3, 6) new Item('Conjured Mana Cake', 3, 6)
] ]
const mySword = new Sword('Awesome Sword', 10, 20) const mockRegularItem = new RegularItem({
mySword.whoAmI() name: 'Mock RegularItem',
sellIn: 5,
quality: 10
})
console.log('mockRegularItem.privateVar', mockRegularItem.privateVar) // bbarreto_debug
mockRegularItem.privateVar = 10
console.log('mockRegularItem.privateVar', mockRegularItem.privateVar) // bbarreto_debug
const days = Number(process.argv[2]) || 2 const days = Number(process.argv[2]) || 2
const gildedRose = new Shop(items) const gildedRose = new Shop(items)