Advertisement
nikolapetkov824

WareHouseUnitTest

Jul 6th, 2019
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Warehouse = require('./Warehouse.js');
  2.  
  3. const expect = require('chai').expect;
  4.  
  5. describe("Warehouse …", function () {
  6.     let wh;
  7.     beforeEach(() => {
  8.         wh = new Warehouse(10);
  9.     })
  10.  
  11.     describe('cotr', () => {
  12.  
  13.         it("initialize correctly", function () {
  14.             const num = 10;
  15.             const expected = { 'Food': {}, 'Drink': {} };
  16.             expect(num).to.equal(wh.capacity);
  17.             expect(expected).to.deep.equal(wh.availableProducts);
  18.         });
  19.  
  20.         it('should throw error msgs', () => {
  21.             expect(() => wh = new Warehouse('').to.throw(`Invalid given warehouse space`));
  22.             expect(() => wh = new Warehouse(0).to.throw(`Invalid given warehouse space`));
  23.             expect(() => wh = new Warehouse(-10).to.throw(`Invalid given warehouse space`));
  24.             expect(() => wh = new Warehouse(' ').to.throw(`Invalid given warehouse space`));
  25.         });
  26.     })
  27.  
  28.     describe('addProduct', () => {
  29.  
  30.         it("should throw not enough space msg", function () {
  31.  
  32.             expect(() => wh.addProduct()).to.throw('There is not enough space or the warehouse is already full');
  33.         });
  34.     })
  35.  
  36.     describe('orderProducts', () => {
  37.  
  38.         it("should return correct output", function () {
  39.             wh.addProduct('Drink', 'Cola', 2);
  40.             wh.addProduct('Drink', 'Fanta', 3);
  41.             wh.addProduct('Drink', 'Sprite', 1);
  42.             wh.orderProducts('Drink');
  43.  
  44.             const expected = '{"Fanta":3,"Cola":2,"Sprite":1}';
  45.             const actual = JSON.stringify(wh.availableProducts.Drink);
  46.  
  47.  
  48.             expect(expected).to.equal(actual);
  49.         });
  50.     })
  51.  
  52.     describe('occupiedCapacity', () => {
  53.         it("should return quantity in stock", function () {
  54.             wh.addProduct('Drink', 'Cola', 2);
  55.             wh.addProduct('Drink', 'Fanta', 3);
  56.             wh.addProduct('Drink', 'Sprite', 1);
  57.  
  58.             expect(wh.occupiedCapacity()).to.equal(6);
  59.         });
  60.     })
  61.  
  62.     describe('revision', () => {
  63.         it("should return empty warehouse", function () {
  64.  
  65.             expect('The warehouse is empty').to.equal(wh.revision());
  66.         });
  67.  
  68.         it("should work", function () {
  69.             wh.addProduct('Drink', 'Cola', 2);
  70.             wh.addProduct('Food', 'Pasta', 2);
  71.             const capacity = wh.occupiedCapacity();
  72.             let output = "";
  73.  
  74.             for (let type of Object.keys(wh.availableProducts)) {
  75.                 output += `Product type - [${type}]\n`;
  76.                 for (let product of Object.keys(wh.availableProducts[type])) {
  77.                     output += `- ${product} ${wh.availableProducts[type][product]}\n`;
  78.                 }
  79.             }
  80.  
  81.             expect(output.trim()).to.deep.equal(wh.revision());
  82.         });
  83.     })
  84.  
  85.     describe('scrapeAProduct', () => {
  86.         it("should work", function () {
  87.             let product = 'Pasta';
  88.             let quantity = 10;
  89.             wh.addProduct('Drink', 'Cola', 2);
  90.             wh.addProduct('Food', 'Pasta', 2);
  91.  
  92.             expect(wh.scrapeAProduct(product,quantity)).to.deep.equal({Pasta: 0});
  93.         });
  94.  
  95.         it("should work", function () {
  96.             let product = 'Pasta';
  97.             let quantity = 1;
  98.             wh.addProduct('Drink', 'Cola', 2);
  99.             wh.addProduct('Food', 'Pasta', 2);
  100.  
  101.             expect(wh.scrapeAProduct(product,quantity)).to.deep.equal({Pasta: 1});
  102.         });
  103.  
  104.         it("should return error msg", function () {
  105.             let product = undefined;
  106.             let quantity = 10;
  107.             wh.availableProducts['Food'] = { pasta: 9 };
  108.             wh.availableProducts['Drink'] = { coke: 9 };
  109.  
  110.             expect(() => wh.scrapeAProduct(product, quantity)).to.throw(`undefined do not exists`);
  111.         });
  112.     })
  113. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement