Advertisement
nikolayneykov92

Untitled

Jun 24th, 2019
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. describe('revision() method test', function () {
  2.   let warehouse
  3.   beforeEach(function () {
  4.     warehouse = new Warehouse(5)
  5.   })
  6.  
  7.   it('should give correct message for empty warehouse', function () {
  8.     expect(warehouse.revision()).to.be.equal('The warehouse is empty')
  9.   })
  10. })
  11. describe('Warehouse', () => {
  12.   describe('constructor', () => {
  13.     it('should throw string if givenSpace <= 0 or non-number argument',
  14.       () => {
  15.         assert.throws(() => new Warehouse(-1))
  16.         assert.throws(() => new Warehouse(0))
  17.         assert.throws(() => new Warehouse('not number'))
  18.       })
  19.  
  20.     it('should set Warehouse capacty for valid givenSpace', () => {
  21.       let warehouse = new Warehouse(5)
  22.       assert.equal(warehouse.capacity, 5)
  23.       assert.equal(JSON.stringify(warehouse.availableProducts), '{"Food":{},"Drink":{}}')
  24.     })
  25.   })
  26.  
  27.   describe('addProduct', () => {
  28.     it('should throw string no free space is available', () => {
  29.       let warehouse = new Warehouse(1)
  30.       assert.throws(() => warehouse.addProduct('Food', 'bread', 2))
  31.     })
  32.  
  33.     it('should add products if has enough free space', () => {
  34.       let warehouse = new Warehouse(4)
  35.       warehouse.addProduct('Food', 'bread', 1)
  36.       warehouse.addProduct('Food', 'potatoes', 1)
  37.       warehouse.addProduct('Drink', 'water', 1)
  38.       warehouse.addProduct('Drink', 'cola', 1)
  39.  
  40.       assert.hasAllKeys(warehouse.availableProducts.Food, ['bread', 'potatoes'])
  41.       assert.hasAllKeys(warehouse.availableProducts.Drink, ['water', 'cola'])
  42.     })
  43.  
  44.     it('should return object when adding product successfully', () => {
  45.       let warehouse = new Warehouse(2)
  46.       assert.isObject(warehouse.addProduct('Food', 'bread', 1))
  47.       assert.isObject(warehouse.addProduct('Drink', 'water', 1))
  48.     })
  49.   })
  50.  
  51.   describe('orderProducts', () => {
  52.     it('should sort foods in descending order by quantity', () => {
  53.       let warehouse = new Warehouse(12)
  54.       warehouse.addProduct('Food', 'bread', 1)
  55.       warehouse.addProduct('Food', 'potatoes', 2)
  56.       warehouse.addProduct('Food', 'mushrooms', 3)
  57.       warehouse.orderProducts('Food')
  58.       let foods = JSON.stringify(warehouse.availableProducts.Food)
  59.       assert.equal(foods, '{"mushrooms":3,"potatoes":2,"bread":1}')
  60.     })
  61.  
  62.     it('should sort drinks in descending order by quantity', () => {
  63.       let warehouse = new Warehouse(12)
  64.       warehouse.addProduct('Drink', 'water', 1)
  65.       warehouse.addProduct('Drink', 'cola', 2)
  66.       warehouse.addProduct('Drink', 'lemonade', 3)
  67.       warehouse.orderProducts('Drink')
  68.       let drinks = JSON.stringify(warehouse.availableProducts.Drink)
  69.       assert.equal(drinks, '{"lemonade":3,"cola":2,"water":1}')
  70.     })
  71.   })
  72.  
  73.   describe('occupiedCapacity', () => {
  74.     it('should return a number, which represents the occupied place in the warehouse', () => {
  75.       let warehouse = new Warehouse(10)
  76.       warehouse.addProduct('Food', 'bread', 4)
  77.       assert.equal(warehouse.occupiedCapacity(), 4)
  78.     })
  79.   })
  80.  
  81.   describe('revision', () => {
  82.     it('should return string "The warehouse is empty" for 0 products', () => {
  83.       let warehouse = new Warehouse(4)
  84.       assert.equal(warehouse.revision(), 'The warehouse is empty')
  85.     })
  86.  
  87.     it('should return string with all listed products', () => {
  88.       let warehouse = new Warehouse(4)
  89.       warehouse.addProduct('Food', 'bread', 1)
  90.       warehouse.addProduct('Food', 'potatoes', 1)
  91.       warehouse.addProduct('Drink', 'water', 1)
  92.       warehouse.addProduct('Drink', 'cola', 1)
  93.  
  94.       assert.equal(warehouse.revision(),
  95.         'Product type - [Food]' +
  96.         '\n- bread 1' +
  97.         '\n- potatoes 1' +
  98.         '\nProduct type - [Drink]' +
  99.         '\n- water 1' +
  100.         '\n- cola 1')
  101.     })
  102.   })
  103.  
  104.   describe('scrapeAProduct', () => {
  105.     it('throw error for non existing type', function () {
  106.       expect(() => {
  107.         let warehouse = new Warehouse(5)
  108.         warehouse.addProduct('Food', 'banana', 4)
  109.         warehouse.addProduct('Food', 'apple', 1)
  110.         warehouse.scrapeAProduct('tomato', 1)
  111.       }).to.throw('tomato do not exists')
  112.     })
  113.  
  114.     it('should reduce product quantity', () => {
  115.       let warehouse = new Warehouse(10)
  116.       warehouse.addProduct('Food', 'bread', 10)
  117.       warehouse.scrapeAProduct('bread', 9)
  118.       assert.equal(warehouse.availableProducts.Food.bread, 1)
  119.     })
  120.  
  121.     it('should reduce product quantity', () => {
  122.       let warehouse = new Warehouse(10)
  123.       warehouse.addProduct('Food', 'bread', 10)
  124.       warehouse.scrapeAProduct('bread', 9)
  125.       assert.equal(warehouse.availableProducts.Food.bread, 1)
  126.     })
  127.  
  128.     it('should reset product quantity if goes below 0', () => {
  129.       let warehouse = new Warehouse(10)
  130.       warehouse.addProduct('Food', 'bread', 10)
  131.       warehouse.scrapeAProduct('bread', 11)
  132.       assert.equal(warehouse.availableProducts.Food.bread, 0)
  133.     })
  134.   })
  135. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement