SHARE
TWEET

Untitled

a guest Jul 16th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ShoppingListService = require('../src/shopping-list-service')
  2. const knex = require('knex')
  3.  
  4. describe(`Shopping List Service object`, function () {
  5.   let db
  6.   let testItems = [
  7.     {
  8.       id: 1,
  9.       name: 'Grapes',
  10.       date_added: new Date('2029-01-22T16:28:32.615Z'),
  11.       price: '12.00',
  12.       category: 'Main'
  13.     },
  14.     {
  15.       id: 2,
  16.       name: 'Peanuts',
  17.       date_added: new Date('2100-05-22T16:28:32.615Z'),
  18.       price: '21.00',
  19.       category: 'Snack'
  20.     },
  21.     {
  22.       id: 3,
  23.       name: 'Ham!',
  24.       date_added: new Date('1919-12-22T16:28:32.615Z'),
  25.       price: '3.00',
  26.       category: 'Lunch'
  27.     },
  28.     {
  29.       id: 4,
  30.       name: 'Bacon!',
  31.       date_added: new Date('1919-12-22T16:28:32.615Z'),
  32.       price: '0.99',
  33.       category: 'Breakfast'
  34.     },
  35.   ]
  36.  
  37.   before(() => {
  38.     db = knex({
  39.       client: 'pg',
  40.       connection: process.env.TEST_DB_URL,
  41.     })
  42.   })
  43.  
  44.   before(() => db('shopping_list').truncate())
  45.  
  46.   afterEach(() => db('shopping_list').truncate())
  47.  
  48.   after(() => db.destroy())
  49.  
  50.   context(`Given 'shopping_list' has data`, () => {
  51.     beforeEach(() => {
  52.       return db
  53.         .into('shopping_list')
  54.         .insert(testItems)
  55.     })
  56.  
  57.     it(`getAllItems() resolves all items from 'shopping_list' table`, () => {
  58.       const expectedItems = testItems.map(item => ({
  59.         ...item,
  60.         checked: false,
  61.       }))
  62.       return ShoppingListService.getAllItems(db)
  63.         .then(actual => {
  64.           expect(actual).to.eql(expectedItems)
  65.         })
  66.     })
  67.  
  68.     it(`getById() resolves an item by id from 'shopping_list' table`, () => {
  69.       const idToGet = 3
  70.       const thirdItem = testItems[idToGet - 1]
  71.       return ShoppingListService.getById(db, idToGet)
  72.         .then(actual => {
  73.           expect(actual).to.eql({
  74.             id: idToGet,
  75.             name: thirdItem.name,
  76.             date_added: thirdItem.date_added,
  77.             price: thirdItem.price,
  78.             category: thirdItem.category,
  79.             checked: false,
  80.           })
  81.         })
  82.     })
  83.  
  84.     it(`deleteItem() removes an item by id from 'shopping_list' table`, () => {
  85.       const itemId = 3
  86.       return ShoppingListService.deleteItem(db, itemId)
  87.         .then(() => ShoppingListService.getAllItems(db))
  88.         .then(allItems => {
  89.           const expected = testItems
  90.             .filter(item => item.id !== itemId)
  91.             .map(item => ({
  92.               ...item,
  93.               checked: false,
  94.             }))
  95.           expect(allItems).to.eql(expected)
  96.         })
  97.     })
  98.  
  99.     it(`updateItem() updates an item from the 'shopping_list' table`, () => {
  100.       const idOfItemToUpdate = 3
  101.       const newItemData = {
  102.         name: 'updated title',
  103.         price: '99.99',
  104.         date_added: new Date(),
  105.         checked: true,
  106.       }
  107.       const originalItem = testItems[idOfItemToUpdate - 1]
  108.       return ShoppingListService.updateItem(db, idOfItemToUpdate, newItemData)
  109.         .then(() => ShoppingListService.getById(db, idOfItemToUpdate))
  110.         .then(item => {
  111.           expect(item).to.eql({
  112.             id: idOfItemToUpdate,
  113.             ...originalItem,
  114.             ...newItemData,
  115.           })
  116.         })
  117.     })
  118.   })
  119.  
  120.   context(`Given 'shopping_list' has no data`, () => {
  121.     it(`getAllItems() resolves an empty array`, () => {
  122.       return ShoppingListService.getAllItems(db)
  123.         .then(actual => {
  124.           expect(actual).to.eql([])
  125.         })
  126.     })
  127.  
  128.     it(`insertItem() inserts an item and resolves the item with an 'id'`, () => {
  129.       const newItem = {
  130.         name: 'Test new name name',
  131.         price: '5.05',
  132.         date_added: new Date('2020-01-01T00:00:00.000Z'),
  133.         checked: true,
  134.         category: 'Lunch',
  135.       }
  136.       return ShoppingListService.insertItem(db, newItem)
  137.         .then(actual => {
  138.           expect(actual).to.eql({
  139.             id: 1,
  140.             name: newItem.name,
  141.             price: newItem.price,
  142.             date_added: newItem.date_added,
  143.             checked: newItem.checked,
  144.             category: newItem.category,
  145.           })
  146.         })
  147.     })
  148.   })
  149. })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top