Advertisement
Guest User

Warehouse

a guest
Jun 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.86 KB | None | 0 0
  1. describe('Warehouse tests', function(){
  2. describe('Constructor tests', function(){
  3. it('Should initialize with valid input capasity of type number', function(){
  4. let warehouse = new Warehouse(3);
  5.  
  6. expect(warehouse.hasOwnProperty('availableProducts')).to.be.true;
  7. expect(warehouse.capacity).to.be.equal(3);
  8. });
  9.  
  10. it('Should trow error with wrong type of input capacity', function(){
  11. expect(() => new Warehouse('3')).to.throw('Invalid given warehouse space');
  12. });
  13.  
  14. it('Should throw error with negative input capacity', function(){
  15. expect(() => new Warehouse(-4)).to.throw('Invalid given warehouse space');
  16. });
  17.  
  18. it('Should throw error with zero input capacity', function(){
  19. expect(() => new Warehouse(0)).to.throw('Invalid given warehouse space');
  20. })
  21. });
  22.  
  23. describe('Set method tests', function(){
  24. it('Should set new capasity if the givenSpace is a valid value', function(){
  25. let warehouse = new Warehouse(3);
  26.  
  27. warehouse.capacity = 5;
  28.  
  29. expect(warehouse.capacity).to.be.equal(5);
  30. });
  31.  
  32. it('Should throw error with wrong type of givenSpace', function(){
  33. let warehouse = new Warehouse(3);
  34.  
  35. expect(() => warehouse.capacity = '3').to.throw('Invalid given warehouse space');
  36. });
  37.  
  38. it('Should throw error with negative givenSpace', function(){
  39. let warehouse = new Warehouse(3);
  40.  
  41. expect(() => warehouse.capacity = -3).to.throw('Invalid given warehouse space');
  42. });
  43. it('Should throw error with zero givenSpace', function(){
  44. let warehouse = new Warehouse(3);
  45.  
  46. expect(() => warehouse.capacity = 0).to.throw('Invalid given warehouse space');
  47. });
  48. });
  49.  
  50. describe('addMethod tests', function(){
  51. it('Should add the given product if there is space', function(){
  52. let warehouse = new Warehouse(5);
  53.  
  54. warehouse.addProduct('Food', 'Pizza', 1);
  55.  
  56. let result = warehouse.availableProducts['Food'];
  57.  
  58. expect(result).to.be.haveOwnProperty('Pizza');
  59. expect(result['Pizza']).to.be.equal(1);
  60. });
  61.  
  62. it('Should sum quantity if product is added more than once', function(){
  63. let warehouse = new Warehouse(5);
  64.  
  65. warehouse.addProduct('Food', 'Pizza', 1);
  66. warehouse.addProduct('Food', 'Pizza', 3);
  67.  
  68. let result =warehouse.availableProducts['Food']['Pizza'];
  69.  
  70. expect(result).to.be.equal(4);
  71. });
  72.  
  73. it('Should throw error if there is allready full', function(){
  74. let warehouse = new Warehouse(5);
  75.  
  76. warehouse.addProduct('Food', 'Pizza', 5);
  77. let wrongFunc = () => warehouse.addProduct('Food', 'Pizza', 3);
  78.  
  79. expect(wrongFunc).to.throw('There is not enough space or the warehouse is already full');
  80. });
  81.  
  82. it('Should throw error if there is no enough place', function(){
  83. let warehouse = new Warehouse(5);
  84.  
  85. warehouse.addProduct('Food', 'Pizza', 3);
  86. let wrongFunc = () => warehouse.addProduct('Food', 'Pizza', 3);
  87.  
  88. expect(wrongFunc).to.throw('There is not enough space or the warehouse is already full');
  89. });
  90.  
  91. it('should return object with the given type when adding product successfully', () => {
  92. let warehouse = new Warehouse(2)
  93. expect(warehouse.addProduct('Food', 'bread', 1)).to.be.an('Object');
  94. expect(warehouse.addProduct('Drink', 'water', 1)).to.be.an('Object');
  95. expect(warehouse.availableProducts.Food).to.haveOwnProperty('bread');
  96. });
  97. });
  98.  
  99. describe('orderProducts method tests', function(){
  100. it('Should sort all product of given type in descending oreder', function(){
  101. let warehouse = new Warehouse(10);
  102.  
  103. warehouse.addProduct('Food', 'Pizza', 1);
  104. warehouse.addProduct('Food','Tomato', 5);
  105. warehouse.addProduct('Food', 'Apple', 2);
  106.  
  107. warehouse.orderProducts('Food');
  108. let expectedResult = 'Tomato Apple Pizza'
  109.  
  110. expect(Object.keys(warehouse.availableProducts['Food']).join(' ')).to.be.equal(expectedResult);
  111. });
  112.  
  113. it('Should sort all product of given type(witth more than one type) in descending oreder', function(){
  114. let warehouse = new Warehouse(100);
  115.  
  116. warehouse.addProduct('Food', 'Pizza', 1);
  117. warehouse.addProduct('Food','Tomato', 5);
  118. warehouse.addProduct('Food', 'Apple', 2);
  119. warehouse.addProduct('Drink', 'Water', 1);
  120. warehouse.addProduct('Drink','Tea', 5);
  121. warehouse.addProduct('Drink', 'Cola', 2);
  122.  
  123. warehouse.orderProducts('Food');
  124. warehouse.orderProducts('Drink');
  125. let expectedResultOne = 'Tomato Apple Pizza';
  126. let expectedResultTwo = 'Tea Cola Water';
  127.  
  128. expect(Object.keys(warehouse.availableProducts['Food']).join(' ')).to.be.equal(expectedResultOne);
  129. expect(Object.keys(warehouse.availableProducts['Drink']).join(' ')).to.be.equal(expectedResultTwo);
  130. });
  131. });
  132.  
  133. describe('occupiedCapacity method tests', function(){
  134. it('Should return a number, which represents the already occupied place in the warehouse with one kind', function(){
  135. let warehouse = new Warehouse(15);
  136.  
  137. warehouse.addProduct('Food', 'Pizza', 1);
  138. warehouse.addProduct('Food','Tomato', 5);
  139.  
  140. expect(warehouse.occupiedCapacity()).to.be.equal(6);
  141. });
  142.  
  143. it('Should return a number, which represents the already occupied place in the warehouse with two kinds', function(){
  144. let warehouse = new Warehouse(15);
  145.  
  146. warehouse.addProduct('Food', 'Pizza', 1);
  147. warehouse.addProduct('Food', 'Tomato', 5);
  148. warehouse.addProduct('Drink', 'Water', 1);
  149. warehouse.addProduct('Drink', 'Cola', 5);
  150.  
  151. expect(warehouse.occupiedCapacity()).to.be.equal(12);
  152. });
  153. });
  154.  
  155. describe('revision method tests', function(){
  156. it('Should return a message for empty if there is no products in warehouse', function(){
  157. let warehouse = new Warehouse(1);
  158.  
  159. expect(warehouse.revision()).to.be.equal('The warehouse is empty');
  160. });
  161.  
  162. it('Should return a message with products in warehouse', function(){
  163. let warehouse = new Warehouse(15);
  164.  
  165. warehouse.addProduct('Food', 'Pizza', 1);
  166. warehouse.addProduct('Food', 'Tomato', 5);
  167. warehouse.addProduct('Drink', 'Water', 1);
  168. warehouse.addProduct('Drink', 'Cola', 5);
  169.  
  170. let output = '';
  171. for (let type of Object.keys(warehouse.availableProducts)) {
  172. output += `Product type - [${type}]\n`;
  173. for (let product of Object.keys(warehouse.availableProducts[type])) {
  174. output += `- ${product} ${warehouse.availableProducts[type][product]}\n`;
  175. }
  176. }
  177.  
  178. expect(warehouse.revision()).to.be.equal(output.trim());
  179. });
  180.  
  181. it('Should return a message with products from one type in warehouse', function(){
  182. let warehouse = new Warehouse(15);
  183.  
  184. warehouse.addProduct('Food', 'Pizza', 1);
  185. warehouse.addProduct('Food', 'Tomato', 5);
  186.  
  187. let output = '';
  188. for (let type of Object.keys(warehouse.availableProducts)) {
  189. output += `Product type - [${type}]\n`;
  190. for (let product of Object.keys(warehouse.availableProducts[type])) {
  191. output += `- ${product} ${warehouse.availableProducts[type][product]}\n`;
  192. }
  193. }
  194.  
  195. expect(warehouse.revision()).to.be.equal(output.trim());
  196. });
  197.  
  198. it('with trimed output', function () {
  199. let initCapacity = 100;
  200. let warehouse = new Warehouse(initCapacity);
  201.  
  202. let type = 'Food';
  203. let product = 'TestProduct';
  204. let quantity = 5;
  205.  
  206. warehouse.addProduct(type, product, quantity);
  207.  
  208. let result = warehouse.revision();
  209.  
  210.  
  211. expect(result.endsWith('\n'), `Result must be trimmed`).to.be.false;
  212. });
  213. });
  214.  
  215. describe('scrapeAProduct method tests', function(){
  216. it('Should throw error if product do not exist', function(){
  217. let warehouse = new Warehouse(15);
  218.  
  219. warehouse.addProduct('Food', 'Pizza', 1);
  220. warehouse.addProduct('Food', 'Tomato', 5);
  221.  
  222. expect(() => warehouse.scrapeAProduct('Apple', 2)).to.throw('Apple do not exists');
  223. });
  224.  
  225. it('Should reduce quantity of product', function(){
  226. let warehouse = new Warehouse(15);
  227.  
  228. warehouse.addProduct('Food', 'Tomato', 5);
  229. warehouse.scrapeAProduct('Tomato', 2);
  230.  
  231. expect(warehouse.availableProducts['Food']['Tomato']).to.be.equal(3);
  232. });
  233.  
  234. it('Should reduce quantity of product to zero', function(){
  235. let warehouse = new Warehouse(15);
  236.  
  237. warehouse.addProduct('Food', 'Tomato', 5);
  238. warehouse.scrapeAProduct('Tomato', 5);
  239.  
  240. expect(warehouse.availableProducts['Food']['Tomato']).to.be.equal(0);
  241. });
  242.  
  243. it('Should reset quantity of product', function(){
  244. let warehouse = new Warehouse(15);
  245.  
  246. warehouse.addProduct('Food', 'Tomato', 5);
  247. warehouse.scrapeAProduct('Tomato', 6);
  248.  
  249. expect(warehouse.availableProducts['Food']['Tomato']).to.be.equal(0);
  250. });
  251. });
  252. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement