From f1a05b0083f28c85c2b89ea64b6eb3e736e20df8 Mon Sep 17 00:00:00 2001 From: mitroio Date: Tue, 16 Oct 2018 12:39:47 +0100 Subject: [PATCH] Added tests up to delete --- .../express/todo/__tests__/todo.test.js | 35 +++++++++++++++++++ exercises/02_tdd/express/todo/modules/todo.js | 13 ++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/exercises/02_tdd/express/todo/__tests__/todo.test.js b/exercises/02_tdd/express/todo/__tests__/todo.test.js index 5f51d36..ea20611 100644 --- a/exercises/02_tdd/express/todo/__tests__/todo.test.js +++ b/exercises/02_tdd/express/todo/__tests__/todo.test.js @@ -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) + }) }) diff --git a/exercises/02_tdd/express/todo/modules/todo.js b/exercises/02_tdd/express/todo/modules/todo.js index f064afc..5f3288f 100644 --- a/exercises/02_tdd/express/todo/modules/todo.js +++ b/exercises/02_tdd/express/todo/modules/todo.js @@ -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 = () => {