Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
340CT-TEACHINGMATERIALS/exercises/02_tdd/express/todo/__tests__/todo.test.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
112 lines (96 sloc)
2.5 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
/* eslint-disable no-magic-numbers */ | |
const todo = require('../modules/todo') | |
describe('add', () => { | |
//beforeEach( () => todo.add('bread', 1)) | |
afterEach( () => todo.clear()) | |
test('check there is a single item', done => { | |
expect.assertions(1) | |
todo.add('bread', 1) | |
const items = todo.getAll() | |
expect(items.length).toBe(1) | |
done() | |
}) | |
test('adding a single item', () => { | |
expect.assertions(1) | |
todo.add('bread', 1) | |
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) | |
}) | |
}) | |
describe('clear', () => { | |
test('clear list with items', () => { | |
expect.assertions(2) | |
try { | |
todo.add('bread', 1) | |
todo.add('butter', 2) | |
const data = todo.getAll() | |
expect(data.length).toBe(2) | |
todo.clear() | |
todo.getAll() | |
} catch(err) { | |
expect(err.message).toBe('empty list') | |
} | |
}) | |
test('clearing empty list should throw error', () => { | |
expect.assertions(1) | |
try { | |
todo.clear() | |
} catch(err) { | |
expect(err.message).toBe('trying to clear empty list') | |
} | |
}) | |
}) | |
describe('getAll', () => { | |
test('retrieving a single item', () => { | |
expect.assertions(2) | |
todo.add('bread', 1) | |
const items = todo.getAll() | |
expect(Array.isArray(items)).toBeTruthy() | |
expect(items.length).toBe(1) | |
}) | |
test('retrieving empty list should throw an error', () => { | |
expect.assertions(1) | |
try { | |
todo.clear() | |
todo.getAll() | |
} catch(err) { | |
expect(err.message).toBe('empty list') | |
} | |
}) | |
}) |