Advertisement
didkoslawow

Untitled

May 29th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { expect } = require('chai');
  2. const { PaymentPackage } = require('./12paymentPackage');
  3.  
  4. describe('Tests for PaymentPackage class', () => {
  5.   it('Constructor tests', () => {
  6.     const instance = new PaymentPackage('Name', 200);
  7.     const expected = [
  8.       `Package: Name`,
  9.       `- Value (excl. VAT): 200`,
  10.       `- Value (VAT 20%): 240`,
  11.     ];
  12.     expect(instance._name).to.equal('Name');
  13.     expect(instance._value).to.equal(200);
  14.     expect(instance._VAT).to.equal(20);
  15.     expect(instance._active).to.equal(true);
  16.     expect(instance.toString()).to.equal(expected.join('\n'));
  17.   });
  18.  
  19.   describe('Tests for the Name property', () => {
  20.     it('Should throw error if passed Name is empty string', () => {
  21.       expect(() => new PaymentPackage('', 123)).to.throw(
  22.         'Name must be a non-empty string'
  23.       );
  24.     });
  25.     it('Should throw error if passed Name is number', () => {
  26.       expect(() => new PaymentPackage(1234, 123)).to.throw(
  27.         'Name must be a non-empty string'
  28.       );
  29.     });
  30.     it('Should throw error if passed Name is array', () => {
  31.       expect(() => new PaymentPackage([], 123)).to.throw(
  32.         'Name must be a non-empty string'
  33.       );
  34.     });
  35.     it('Should throw error if passed Name is object', () => {
  36.       expect(() => new PaymentPackage({}, 123)).to.throw(
  37.         'Name must be a non-empty string'
  38.       );
  39.     });
  40.     it('Should not throw error if passed Name is not empty string', () => {
  41.       expect(() => new PaymentPackage('Test', 123)).to.not.throw(
  42.         'Name must be a non-empty string'
  43.       );
  44.     });
  45.     it('Should change name if the passed name is correct', () => {
  46.       const test = new PaymentPackage('Test', 123);
  47.       test.name = 'TestName';
  48.       expect(test.name).to.equal('TestName');
  49.     });
  50.   });
  51.  
  52.   describe('Tests for the Value property', () => {
  53.     it('Should throw error if passed Value is string', () => {
  54.       expect(() => new PaymentPackage('Test', '123')).to.throw(
  55.         'Value must be a non-negative number'
  56.       );
  57.     });
  58.     it('Should throw error if passed Value is array', () => {
  59.       expect(() => new PaymentPackage('Test', [])).to.throw(
  60.         'Value must be a non-negative number'
  61.       );
  62.     });
  63.     it('Should throw error if passed Value is object', () => {
  64.       expect(() => new PaymentPackage('Test', {})).to.throw(
  65.         'Value must be a non-negative number'
  66.       );
  67.     });
  68.     it('Should throw error if passed Value is negative number', () => {
  69.       expect(() => new PaymentPackage('Test', -123)).to.throw(
  70.         'Value must be a non-negative number'
  71.       );
  72.     });
  73.     it('Should not throw error if passed Value is possitive number', () => {
  74.       expect(() => new PaymentPackage('Test', 123)).to.not.throw(
  75.         'Value must be a non-negative number'
  76.       );
  77.     });
  78.     it('Should change Value to 0', () => {
  79.       expect(() => new PaymentPackage('Test', 0)).to.not.throw('Value must be a non-negative number');
  80.     });
  81.   });
  82.  
  83.   describe('Tests for the Vat property', () => {
  84.     it('Should throw error if passed Vat is string', () => {
  85.       const testVat = new PaymentPackage('TestName', 123);
  86.       expect(() => (testVat.VAT = '123')).to.throw(
  87.         'VAT must be a non-negative number'
  88.       );
  89.     });
  90.     it('Should throw error if passed Vat is array', () => {
  91.       const testVat = new PaymentPackage('TestName', 123);
  92.       expect(() => (testVat.VAT = [])).to.throw(
  93.         'VAT must be a non-negative number'
  94.       );
  95.     });
  96.     it('Should throw error if passed Vat is object', () => {
  97.       const testVat = new PaymentPackage('TestName', 123);
  98.       expect(() => (testVat.VAT = {})).to.throw(
  99.         'VAT must be a non-negative number'
  100.       );
  101.     });
  102.     it('Should throw error if passed Vat is negative number', () => {
  103.       const testVat = new PaymentPackage('TestName', 123);
  104.       expect(() => (testVat.VAT = -123)).to.throw(
  105.         'VAT must be a non-negative number'
  106.       );
  107.     });
  108.     it('Should not throw error if passed Vat is possitive number', () => {
  109.       const testVat = new PaymentPackage('TestName', 123);
  110.       expect(() => (testVat.VAT = 123)).to.not.throw(
  111.         'VAT must be a non-negative number'
  112.       );
  113.     });
  114.   });
  115.  
  116.   describe('Tests for the Active property', () => {
  117.     it('Should throw error if passed Active is string', () => {
  118.       const testVat = new PaymentPackage('TestName', 123);
  119.       expect(() => (testVat.active = '123')).to.throw(
  120.         'Active status must be a boolean'
  121.       );
  122.     });
  123.     it('Should throw error if passed Active is array', () => {
  124.       const testVat = new PaymentPackage('TestName', 123);
  125.       expect(() => (testVat.active = [])).to.throw(
  126.         'Active status must be a boolean'
  127.       );
  128.     });
  129.     it('Should throw error if passed Active is object', () => {
  130.       const testVat = new PaymentPackage('TestName', 123);
  131.       expect(() => (testVat.active = {})).to.throw(
  132.         'Active status must be a boolean'
  133.       );
  134.     });
  135.     it('Should throw error if passed Active is negative number', () => {
  136.       const testVat = new PaymentPackage('TestName', 123);
  137.       expect(() => (testVat.active = -123)).to.throw(
  138.         'Active status must be a boolean'
  139.       );
  140.     });
  141.     it('Should not throw error if passed Active is possitive number', () => {
  142.       const testVat = new PaymentPackage('TestName', 123);
  143.       expect(() => (testVat.active = true)).to.not.throw(
  144.         'Active status must be a boolean'
  145.       );
  146.     });
  147.   });
  148.  
  149.   describe('Tests for the toString() method of the class', () => {
  150.     it('Should return a string if the Name and Value are correct', () => {
  151.       const test = new PaymentPackage('TestName', 123);
  152.       const expected = [
  153.         `Package: TestName`,
  154.         `- Value (excl. VAT): 123`,
  155.         `- Value (VAT 20%): 147.6`,
  156.       ];
  157.  
  158.       expect(test.toString()).to.equal(expected.join('\n'));
  159.     });
  160.     it('Should return a string if the Name, Value and VAT are correct', () => {
  161.       const test = new PaymentPackage('TestName', 123);
  162.       test.VAT = 30;
  163.       const expected = [
  164.         `Package: TestName`,
  165.         `- Value (excl. VAT): 123`,
  166.         `- Value (VAT 30%): 159.9`,
  167.       ];
  168.  
  169.       expect(test.toString()).to.equal(expected.join('\n'));
  170.     });
  171.     it('Should return a string if the Name, Value and Active are correct', () => {
  172.       const test = new PaymentPackage('TestName', 123);
  173.       test.active = false;
  174.       const expected = [
  175.         `Package: TestName (inactive)`,
  176.         `- Value (excl. VAT): 123`,
  177.         `- Value (VAT 20%): 147.6`,
  178.       ];
  179.  
  180.       expect(test.toString()).to.equal(expected.join('\n'));
  181.     });
  182.     it('Should return a string if the Name, Value, VAT and Active are correct', () => {
  183.       const test = new PaymentPackage('TestName', 123);
  184.       test.VAT = 30;
  185.       test.active = false;
  186.       const expected = [
  187.         `Package: TestName (inactive)`,
  188.         `- Value (excl. VAT): 123`,
  189.         `- Value (VAT 30%): 159.9`,
  190.       ];
  191.  
  192.       expect(test.toString()).to.equal(expected.join('\n'));
  193.     });
  194.   });
  195. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement