Skip to content
Permalink
Browse files
Added tests up to delete
  • Loading branch information
mitroio committed Oct 16, 2018
1 parent 05d6ab7 commit f1a05b0083f28c85c2b89ea64b6eb3e736e20df8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
@@ -24,6 +24,41 @@ describe('add', () => {
const items = todo.getAll()
expect(items.length).toBe(1)
})
test('adding a blank string should throw an error', () => {
expect.assertions(1)
try {
todo.add('bread', 1)
todo.add('', '')
} catch(err) {
expect(err.message).toBe('item is blank string')
}
})
test('Adding an invalid value in the quantity field should default to a value of 1', () => {
expect.assertions(2)
todo.add('bread')
todo.add('plums','invalid')
const items = todo.getAll()
const bread = items.find(elem => elem.item == 'bread')
const plums = items.find(elem => elem.item == 'plums')
expect(bread.qty).toBe(1)
expect(plums.qty).toBe(1)
})
test('Adding same value twice should increase the quantity instead of inserting a new element', ()=> {
expect.assertions(1)
todo.add('bread',1)
todo.add('bread',1)
const items = todo.getAll()
const bread = items.find(elem => elem.item == 'bread')
expect(bread.qty).toBe(2)
})
test('item names should be case insensitive',() => {
expect.assertions(1)
todo.add('bread',1)
todo.add('BRead',1)
const items = todo.getAll()
const bread = items.find(elem => elem.item == 'bread')
expect(bread.qty).toBe(2)
})

})

@@ -9,7 +9,18 @@ module.exports.clear = () => {
}

module.exports.add = (item, qty) => {
data.push({item: item, qty: qty})
item = item.toLowerCase()
if(item.length === 0) {
throw new Error('item is blank string')
}
if (typeof qty !== 'number') qty = 1
let listItem = data.find(elem => elem.item==item)
if (listItem) {
listItem.qty +=qty
}
else {
data.push({item: item, qty: qty})
}
}

module.exports.getAll = () => {

0 comments on commit f1a05b0

Please sign in to comment.