Skip to content

Instantly share code, notes, and snippets.

@chriscarlsondev
Created July 1, 2019 17:40
Show Gist options
  • Save chriscarlsondev/4c211464d681895a5bdd152e26d52b8d to your computer and use it in GitHub Desktop.
Save chriscarlsondev/4c211464d681895a5bdd152e26d52b8d to your computer and use it in GitHub Desktop.
Service and test for shopping list
const ShoppingListService = {
getAllItems(knex){
return knex.select('*').from('shopping_list')
},
insertItem(knex, newItem) {
return knex
.insert(newItem)
.into('shopping_list')
.returning('*')
.then(rows => {
return rows[0]
})
},
getById(knex, id) {
return knex.from('shopping_list').select('*').where('id', id).first()
},
deleteItem(knex, id) {
return knex('shopping_list')
.where({ id })
.delete()
},
updateItem(knex, id, newItemFields) {
return knex('shopping_list')
.where({ id })
.update(newItemFields)
},
}
module.exports = ShoppingListService
const ShoppingListService = require('../src/shopping-list-service')
const knex = require('knex')
describe(`Item service object`, function () {
let db
let testItems = [
{
id: 1,
name: 'Item A',
price: '1.23',
date_added: new Date('2029-01-22T16:28:32.615Z'),
checked: false,
category: 'Main'
},
{
id: 2,
name: 'Item B',
price: '4.56',
date_added: new Date('1919-12-22T16:28:32.615Z'),
checked: true,
category: 'Lunch'
},
{
id: 3,
name: 'Item C',
price: '7.89',
date_added: new Date('2019-12-22T16:28:32.615Z'),
checked: true,
category: 'Breakfast'
},
]
before(() => {
db = knex({
client: 'pg',
connection: process.env.TEST_DB_URL,
})
})
after(() => db.destroy())
before(() => db('shopping_list').truncate())
afterEach(() => db('shopping_list').truncate())
context(`Given 'shopping_list' has data`, () => {
beforeEach(() => {
return db
.into('shopping_list')
.insert(testItems)
})
it(`getAllItems() resolves all items from 'shopping_list' table`, () => {
return ShoppingListService.getAllItems(db)
.then(actual => {
expect(actual).to.eql(testItems)
})
})
it(`getById() resolves an item by id from 'shopping_list' table`, () => {
const thirdId = 3
const thirdTestItem = testItems[thirdId - 1]
return ShoppingListService.getById(db, thirdId)
.then(actual => {
expect(actual).to.eql({
id: thirdId,
name: thirdTestItem.name,
price: thirdTestItem.price,
date_added: thirdTestItem.date_added,
checked: thirdTestItem.checked,
category: thirdTestItem.category
})
})
})
it(`deleteItem() removes an item by id from 'shopping_list' table`, () => {
const thirdId = 3
return ShoppingListService.deleteItem(db, thirdId)
.then(() => ShoppingListService.getAllItems(db))
.then(allItems => {
// copy the test items array without the "deleted" item
const expected = testItems.filter(item => item.id !== thirdId)
expect(allItems).to.eql(expected)
})
})
it(`updateItem() updates an item from the 'shopping_list' table`, () => {
const idOfItemToUpdate = 3
const newItemData = {
name: 'updated name',
price: '9.10',
date_added: new Date(),
checked: true,
category: 'Main'
}
return ShoppingListService.updateItem(db, idOfItemToUpdate, newItemData)
.then(() => ShoppingListService.getById(db, idOfItemToUpdate))
.then(item => {
expect(item).to.eql({
id: idOfItemToUpdate,
...newItemData,
})
})
})
})
context(`Given 'shopping_list' has no data`, () => {
it(`getAllItems() resolves an empty array`, () => {
return ShoppingListService.getAllItems(db)
.then(actual => {
expect(actual).to.eql([])
})
})
it(`insertItem() inserts a new item and resolves the new item with an 'id'`, () => {
const newItem = {
name: 'updated name',
price: '9.10',
date_added: new Date(),
checked: true,
category: 'Main'
}
return ShoppingListService.insertItem(db, newItem)
.then(actual => {
expect(actual).to.eql({
id: 1,
name: newItem.name,
price: newItem.price,
date_added: newItem.date_added,
checked: newItem.checked,
category: newItem.category
})
})
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment