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?
foundation/solutions/07_unit_testing/database/todo.spec.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
163 lines (140 sloc)
3.91 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' | |
const ToDo = require('../modules/todo.js') | |
beforeAll( async() => { | |
// stuff to do before any of the tests run | |
}) | |
afterAll( async() => { | |
// runs after all the tests have completed | |
}) | |
describe('add()', () => { | |
// block of tests | |
// beforeEach( async() => { | |
// todo.clear() | |
// }) | |
afterEach( async() => { | |
// runs after each test completes | |
}) | |
test('add a single item', async done => { | |
expect.assertions(1) | |
// ARRANGE | |
const todo = await new ToDo() // DB runs in-memory if no name supplied | |
// ACT | |
await todo.add('bread', 3) | |
const count = await todo.countItems() | |
// ASSERT | |
expect(count).toBe(1) | |
done() | |
}) | |
test('qty must be a number', async done => { | |
expect.assertions(1) | |
const todo = await new ToDo() | |
await expect( todo.add('bread', 'three') ).rejects.toEqual( Error('the quantity must be a number') ) | |
done() | |
}) | |
test('duplicates should increase qty', async done => { | |
expect.assertions(2) | |
// ARRANGE | |
const todo = await new ToDo() | |
// ACT | |
await todo.add('bread', 4) | |
await todo.add('bread', 2) | |
const data = await todo.getAll() | |
const qty = data[0].qty | |
// ASSERT (note there are two assertions as stated on line 42) | |
expect(data).toHaveLength(1) | |
expect(qty).toEqual(6) | |
done() | |
}) | |
test('item cannot be empty', async done => { | |
expect.assertions(1) | |
const todo = await new ToDo() | |
await expect( todo.add('', 1) ).rejects.toEqual( Error('item cannot be empty') ) | |
done() | |
}) | |
test('qty cannot be empty', async done => { | |
expect.assertions(1) | |
const todo = await new ToDo() | |
await expect( todo.add('bread', '') ).rejects.toEqual( Error('qty cannot be empty') ) | |
done() | |
}) | |
}) | |
describe('delete()', () => { | |
beforeEach( async() => { | |
this.todo = await new ToDo() | |
await this.todo.add('bread', 5) | |
}) | |
test('id cannot be undefined', async done => { | |
expect.assertions(1) | |
await expect( this.todo.delete() ).rejects.toEqual( Error('id cannot be undefined') ) | |
done() | |
}) | |
test('id must be a number', async done => { | |
expect.assertions(1) | |
await expect( this.todo.delete('a') ).rejects.toEqual( Error('id must be a number') ) | |
done() | |
}) | |
test('id has to exist', async done => { | |
expect.assertions(1) | |
await expect( this.todo.delete(2) ).rejects.toEqual( Error('id has to exist') ) | |
done() | |
}) | |
test('delete an item', async done => { | |
expect.assertions(2) | |
await this.todo.delete(1) | |
expect(await this.todo.countItems()).toEqual(0) | |
expect(await this.todo.getAll()).toEqual([]) | |
done() | |
}) | |
}) | |
describe('getAll()', () => { | |
beforeEach( async() => { | |
this.todo = await new ToDo() | |
}) | |
test('getAll() with no items', async done => { | |
expect.assertions(1) | |
expect(await this.todo.getAll()).toEqual([]) | |
done() | |
}) | |
test('getAll() with a single item', async done => { | |
expect.assertions(1) | |
await this.todo.add('bread', 2) | |
expect(await this.todo.getAll()).toEqual([{"item":"bread", "qty":2, "id":1}]) | |
done() | |
}) | |
test('getAll() with two items', async done => { | |
expect.assertions(1) | |
await this.todo.add('bread', 2) | |
await this.todo.add('ham', 3) | |
expect(await this.todo.getAll()).toEqual([{"item":"bread", "qty":2, "id":1}, {"item":"ham", "qty":3, "id":2}]) | |
done() | |
}) | |
}) | |
describe('clear()', () => { | |
beforeEach( async() => { | |
this.todo = await new ToDo() | |
}) | |
test('clear() with no items', async done => { | |
expect.assertions(2) | |
await this.todo.clear() | |
expect(await this.todo.countItems()).toEqual(0) | |
expect(await this.todo.getAll()).toEqual([]) | |
done() | |
}) | |
test('clear() with a single item', async done => { | |
expect.assertions(2) | |
await this.todo.add('bread', 2) | |
await this.todo.clear() | |
expect(await this.todo.countItems()).toEqual(0) | |
expect(await this.todo.getAll()).toEqual([]) | |
done() | |
}) | |
test('clear() with two items', async done => { | |
expect.assertions(2) | |
await this.todo.add('bread', 2) | |
await this.todo.add('ham', 3) | |
await this.todo.clear() | |
expect(await this.todo.countItems()).toEqual(0) | |
expect(await this.todo.getAll()).toEqual([]) | |
done() | |
}) | |
}) |