Advertisement
Btwonu

Payment Package

Oct 29th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const assert = require('chai').assert;
  2. const PaymentPackage = require('./PaymentPackage');
  3.  
  4. describe('Payment Package class', () => {
  5.   describe('Constructor', () => {
  6.     it('Should instantiate a new object if provided with a non-empty string and a non-negative number as arguments.', () => {
  7.       let obj = new PaymentPackage('Non-empty', 0);
  8.  
  9.       assert.isObject(obj, 'Not an object.');
  10.       assert.instanceOf(obj, PaymentPackage, 'Not of the correct instance');
  11.     });
  12.  
  13.     it('Should throw an error if input is not of the correct type.', () => {
  14.       new PaymentPackage('Non-empty', 0);
  15.       // different data types
  16.       let nameInput = [
  17.         [],
  18.         {},
  19.         3,
  20.         undefined,
  21.         Symbol(),
  22.         function () {},
  23.         '',
  24.         Boolean(),
  25.       ];
  26.  
  27.       let valueInput = [
  28.         [],
  29.         {},
  30.         -3,
  31.         undefined,
  32.         Symbol(),
  33.         function () {},
  34.         '',
  35.         Boolean(),
  36.       ];
  37.  
  38.       nameInput.forEach((arg) => {
  39.         assert.throw(() => {
  40.           new PaymentPackage(arg, 0);
  41.         });
  42.       });
  43.  
  44.       valueInput.forEach((arg) => {
  45.         assert.throw(() => {
  46.           new PaymentPackage('Name', arg);
  47.         });
  48.       });
  49.     });
  50.   });
  51.  
  52.   // Before Each
  53.   let obj;
  54.   beforeEach(() => {
  55.     obj = new PaymentPackage('name', 100);
  56.   });
  57.  
  58.   // Accessors
  59.   describe('Accessor name', () => {
  60.     it('Should get and set the name', () => {
  61.       // set
  62.       obj.name = 'Imaginary Name';
  63.       // get
  64.       let value = obj.name;
  65.       assert.equal(value, 'Imaginary Name');
  66.     });
  67.  
  68.     it('Should throw the specified error if passed anything other than non-empty string.', () => {
  69.       // different data types
  70.       let nameInput = [
  71.         [],
  72.         {},
  73.         3,
  74.         undefined,
  75.         Symbol(),
  76.         function () {},
  77.         '',
  78.         Boolean(),
  79.       ];
  80.  
  81.       nameInput.forEach((arg) => {
  82.         assert.throw(() => {
  83.           obj.name = arg;
  84.         }, 'Name must be a non-empty string');
  85.       });
  86.     });
  87.   });
  88.  
  89.   describe('Accessor value', () => {
  90.     it('Should get and set the value', () => {
  91.       // set
  92.       obj.value = 1000.328;
  93.       // get
  94.       let value = obj.value;
  95.       assert.equal(value, 1000.328);
  96.     });
  97.  
  98.     it('Should throw the specified error if passed anything other than a positive number.', () => {
  99.       // different data types
  100.       let valueInput = [
  101.         [],
  102.         {},
  103.         -3,
  104.         undefined,
  105.         Symbol(),
  106.         function () {},
  107.         '',
  108.         Boolean(),
  109.       ];
  110.  
  111.       valueInput.forEach((arg) => {
  112.         assert.throw(() => {
  113.           obj.value = arg;
  114.         }, 'Value must be a non-negative number');
  115.       });
  116.     });
  117.   });
  118.  
  119.   describe('Accessor VAT', () => {
  120.     it('Should get and set the VAT', () => {
  121.       // set
  122.       obj.VAT = 1000.328;
  123.       // get
  124.       let value = obj.VAT;
  125.       assert.equal(value, 1000.328);
  126.     });
  127.  
  128.     it('Should throw the specified error if passed anything other than a positive number.', () => {
  129.       // different data types
  130.       let valueInput = [
  131.         [],
  132.         {},
  133.         -3,
  134.         undefined,
  135.         Symbol(),
  136.         function () {},
  137.         '',
  138.         Boolean(),
  139.       ];
  140.  
  141.       valueInput.forEach((arg) => {
  142.         assert.throw(() => {
  143.           obj.VAT = arg;
  144.         }, 'VAT must be a non-negative number');
  145.       });
  146.     });
  147.   });
  148.  
  149.   describe('Accessor active', () => {
  150.     it('Should get and set the active', () => {
  151.       // set
  152.       obj.active = true;
  153.       // get
  154.       let value = obj.active;
  155.       assert.equal(value, true);
  156.       assert.isBoolean(value);
  157.     });
  158.  
  159.     it('Should throw the specified error if passed anything other than a boolean.', () => {
  160.       // different data types
  161.       let activeInput = [[], {}, -3, undefined, Symbol(), function () {}, ''];
  162.  
  163.       activeInput.forEach((arg) => {
  164.         assert.throw(() => {
  165.           obj.active = arg;
  166.         }, 'Active status must be a boolean');
  167.       });
  168.     });
  169.   });
  170.  
  171.   describe('Function toString', () => {
  172.     it('Should return a string containing an overview of the package.', () => {
  173.       obj.name = 'HR Services';
  174.       obj.value = 1500;
  175.  
  176.       let result = obj.toString();
  177.       let expectedOutput = `Package: HR Services
  178. - Value (excl. VAT): 1500
  179. - Value (VAT 20%): 1800`;
  180.  
  181.       assert.equal(result, expectedOutput, 'Strings do not match.');
  182.     });
  183.  
  184.     it('Should append "(inactive)" label to the returned string if active is set to false.', () => {
  185.       obj.name = 'HR Services';
  186.       obj.value = 1500;
  187.       // set to false
  188.       obj.active = false;
  189.  
  190.       let result = obj.toString();
  191.       let expectedOutput = `Package: HR Services (inactive)
  192. - Value (excl. VAT): 1500
  193. - Value (VAT 20%): 1800`;
  194.  
  195.       assert.equal(result, expectedOutput, 'Strings do not match.');
  196.     });
  197.   });
  198. });
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement