Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const assert = require('chai').assert;
- const PaymentPackage = require('./PaymentPackage');
- describe('Payment Package class', () => {
- describe('Constructor', () => {
- it('Should instantiate a new object if provided with a non-empty string and a non-negative number as arguments.', () => {
- let obj = new PaymentPackage('Non-empty', 0);
- assert.isObject(obj, 'Not an object.');
- assert.instanceOf(obj, PaymentPackage, 'Not of the correct instance');
- });
- it('Should throw an error if input is not of the correct type.', () => {
- new PaymentPackage('Non-empty', 0);
- // different data types
- let nameInput = [
- [],
- {},
- 3,
- undefined,
- Symbol(),
- function () {},
- '',
- Boolean(),
- ];
- let valueInput = [
- [],
- {},
- -3,
- undefined,
- Symbol(),
- function () {},
- '',
- Boolean(),
- ];
- nameInput.forEach((arg) => {
- assert.throw(() => {
- new PaymentPackage(arg, 0);
- });
- });
- valueInput.forEach((arg) => {
- assert.throw(() => {
- new PaymentPackage('Name', arg);
- });
- });
- });
- });
- // Before Each
- let obj;
- beforeEach(() => {
- obj = new PaymentPackage('name', 100);
- });
- // Accessors
- describe('Accessor name', () => {
- it('Should get and set the name', () => {
- // set
- obj.name = 'Imaginary Name';
- // get
- let value = obj.name;
- assert.equal(value, 'Imaginary Name');
- });
- it('Should throw the specified error if passed anything other than non-empty string.', () => {
- // different data types
- let nameInput = [
- [],
- {},
- 3,
- undefined,
- Symbol(),
- function () {},
- '',
- Boolean(),
- ];
- nameInput.forEach((arg) => {
- assert.throw(() => {
- obj.name = arg;
- }, 'Name must be a non-empty string');
- });
- });
- });
- describe('Accessor value', () => {
- it('Should get and set the value', () => {
- // set
- obj.value = 1000.328;
- // get
- let value = obj.value;
- assert.equal(value, 1000.328);
- });
- it('Should throw the specified error if passed anything other than a positive number.', () => {
- // different data types
- let valueInput = [
- [],
- {},
- -3,
- undefined,
- Symbol(),
- function () {},
- '',
- Boolean(),
- ];
- valueInput.forEach((arg) => {
- assert.throw(() => {
- obj.value = arg;
- }, 'Value must be a non-negative number');
- });
- });
- });
- describe('Accessor VAT', () => {
- it('Should get and set the VAT', () => {
- // set
- obj.VAT = 1000.328;
- // get
- let value = obj.VAT;
- assert.equal(value, 1000.328);
- });
- it('Should throw the specified error if passed anything other than a positive number.', () => {
- // different data types
- let valueInput = [
- [],
- {},
- -3,
- undefined,
- Symbol(),
- function () {},
- '',
- Boolean(),
- ];
- valueInput.forEach((arg) => {
- assert.throw(() => {
- obj.VAT = arg;
- }, 'VAT must be a non-negative number');
- });
- });
- });
- describe('Accessor active', () => {
- it('Should get and set the active', () => {
- // set
- obj.active = true;
- // get
- let value = obj.active;
- assert.equal(value, true);
- assert.isBoolean(value);
- });
- it('Should throw the specified error if passed anything other than a boolean.', () => {
- // different data types
- let activeInput = [[], {}, -3, undefined, Symbol(), function () {}, ''];
- activeInput.forEach((arg) => {
- assert.throw(() => {
- obj.active = arg;
- }, 'Active status must be a boolean');
- });
- });
- });
- describe('Function toString', () => {
- it('Should return a string containing an overview of the package.', () => {
- obj.name = 'HR Services';
- obj.value = 1500;
- let result = obj.toString();
- let expectedOutput = `Package: HR Services
- - Value (excl. VAT): 1500
- - Value (VAT 20%): 1800`;
- assert.equal(result, expectedOutput, 'Strings do not match.');
- });
- it('Should append "(inactive)" label to the returned string if active is set to false.', () => {
- obj.name = 'HR Services';
- obj.value = 1500;
- // set to false
- obj.active = false;
- let result = obj.toString();
- let expectedOutput = `Package: HR Services (inactive)
- - Value (excl. VAT): 1500
- - Value (VAT 20%): 1800`;
- assert.equal(result, expectedOutput, 'Strings do not match.');
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement