rimus_cx

Test

Nov 17th, 2021 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const expect = require('chai').expect;
  2.  
  3. const calc = require('../../app/calc');
  4.  
  5. describe('Calc method', () => {
  6.  
  7.   const testData = {
  8.     negative: [
  9.       { a: true },
  10.       { a: 'foo', b: 2 },
  11.       { a: 1, b: { name: 'John' } },
  12.       { a: null, b: 'bar' }
  13.     ],
  14.     sumPositive: [
  15.       { a: -1, b: 1, sum: 0 },
  16.       { a: 10, b: 101, sum: 111 },
  17.       { a: 11, b: -3, sum: 8 },
  18.       { a: -42, b: 12, sum: -30 },
  19.     ],
  20.     multiplicationPositive: [
  21.       { a: -1, b: 1, multiplication: -1 },
  22.       { a: 10, b: 101, multiplication: 1010 },
  23.       { a: 11, b: -3, multiplication: -33 },
  24.       { a: -42, b: 12, multiplication: -504 },
  25.     ],
  26.   };
  27.  
  28.   testData.negative.forEach(({ a, b }) => {
  29.     it(`should throw an error if not a number parameters a = ${a} and b = ${b} passed`, () => {
  30.       const addWithError = () => calc.add(a, b);
  31.       expect(addWithError).to.throw(TypeError);
  32.     });
  33.   });
  34.  
  35.   testData.sumPositive.forEach(({ a, b, sum }) => {
  36.     it(`should return valid value ${sum} for sum of numbers ${a} and ${b}`, () => {
  37.       expect(calc.add(a, b)).to.equal(sum);
  38.     });
  39.   });
  40.  
  41.   testData.negative.forEach(({ a, b }) => {
  42.     it(`should throw an error if not a number parameters a = ${a} and b = ${b} passed`, () => {
  43.       const multiplyWithError = () => calc.multiply(a, b);
  44.       expect(multiplyWithError).to.throw(TypeError);
  45.     });
  46.   });
  47.  
  48.   testData.multiplicationPositive.forEach(({ a, b, multiplication }) => {
  49.     it(`should return valid value ${multiplication} for multiplication of numbers ${a} and ${b}`, () => {
  50.       expect(calc.multiply(a, b)).to.equal(multiplication);
  51.     });
  52.   });
  53. });
  54.  
Add Comment
Please, Sign In to add comment