From cae34b63aaecffb72e83cbd0d94a54d460420d13 Mon Sep 17 00:00:00 2001 From: Anir Nathwani Date: Tue, 8 Oct 2019 14:25:37 +0100 Subject: [PATCH] push --- .../07_unit_testing/todo/modules/todo.js | 11 +++++++++- .../todo/unit tests/todo.spec.js | 20 ++++++++++++++++++- npm-debug.log | 19 ++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 npm-debug.log diff --git a/exercises/07_unit_testing/todo/modules/todo.js b/exercises/07_unit_testing/todo/modules/todo.js index 5a034683..c03b8174 100644 --- a/exercises/07_unit_testing/todo/modules/todo.js +++ b/exercises/07_unit_testing/todo/modules/todo.js @@ -10,7 +10,16 @@ module.exports.clear = () => { module.exports.add = (item, qty) => { qty = Number(qty) if(isNaN(qty)) throw new Error('the quantity must be a number') - data.push({item: item, qty: qty}) + let flag = false + for(let index in data) { + if (data[index].item === item) { + data[index].qty+= qty + flag = true + } + } + if(flag === false) { + data.push({item: item, qty: qty}) + } } module.exports.getAll = () => { diff --git a/exercises/07_unit_testing/todo/unit tests/todo.spec.js b/exercises/07_unit_testing/todo/unit tests/todo.spec.js index 55eb70db..2c46bac0 100644 --- a/exercises/07_unit_testing/todo/unit tests/todo.spec.js +++ b/exercises/07_unit_testing/todo/unit tests/todo.spec.js @@ -41,8 +41,26 @@ describe('add()', () => { done() } }) - + // New test goes HERE! + test('duplicates should increase qty', async done => { + expect.assertions(2) + try { + // ACT + todo.add('bread', 4) + todo.add('bread', 2) + // ASSERT + const count = todo.countItems() + expect(count).toBe(1) + const data = todo.getAll() + const qty = data[0].qty + expect(qty).toEqual(6) + } catch(err) { + done.fail('test failed') + } finally { + done() + } + }) }) diff --git a/npm-debug.log b/npm-debug.log new file mode 100644 index 00000000..74e9b8d4 --- /dev/null +++ b/npm-debug.log @@ -0,0 +1,19 @@ +0 info it worked if it ends with ok +1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'run', 'test' ] +2 info using npm@3.5.2 +3 info using node@v8.10.0 +4 verbose stack Error: ENOENT: no such file or directory, open '/home/anir/foundation/package.json' +5 verbose cwd /home/anir/foundation +6 error Linux 5.0.0-23-generic +7 error argv "/usr/bin/node" "/usr/bin/npm" "run" "test" +8 error node v8.10.0 +9 error npm v3.5.2 +10 error path /home/anir/foundation/package.json +11 error code ENOENT +12 error errno -2 +13 error syscall open +14 error enoent ENOENT: no such file or directory, open '/home/anir/foundation/package.json' +15 error enoent ENOENT: no such file or directory, open '/home/anir/foundation/package.json' +15 error enoent This is most likely not a problem with npm itself +15 error enoent and is related to npm not being able to find a file. +16 verbose exit [ -2, true ]