Advertisement
simeonshopov

Repository

Jun 16th, 2021
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let { Repository } = require('./solution.js');
  2. const { assert } = require('chai');
  3.  
  4. describe('Test Repository', function () {
  5.     let properties = { name: 'string', age: 'number', birthday: 'object' };
  6.     let repository = new Repository(properties);
  7.  
  8.     describe('Test instantiation', function () {
  9.  
  10.         it('Test constructor', function () {
  11.             assert.isObject(repository.props);
  12.             assert.instanceOf(repository, Repository);
  13.             assert.instanceOf(repository.data, Map);
  14.             assert.equal(repository.nextId(), 0);
  15.         });
  16.         it('Test if have all functions', function () {
  17.             assert.equal(typeof repository.add, 'function');
  18.             assert.isFunction(repository.getId);
  19.             assert.isFunction(repository.update);
  20.             assert.isFunction(repository.del);
  21.             assert.isFunction(repository._validate);
  22.         });
  23.     });
  24.  
  25.     describe('Test getter count', function () {
  26.         it('Returns correct output count', function () { assert.equal(repository.count, 0); });
  27.     });
  28.  
  29.     describe('Test add', function () {
  30.         it('Throw error for missing prop', function () {
  31.             let entity = { name: 'Pesho' };
  32.             assert.throw(() => { repository.add(entity); }, 'Property age is missing from the entity!');
  33.         });
  34.         it('Throw error for missing prop 2', function () {
  35.             let entity = { age: 22, birthday: new Date(1998, 0, 7) };
  36.             assert.throw(() => { repository.add(entity); }, 'Property name is missing from the entity!');
  37.         });
  38.         it('Throw error for invalid type of prop', function () {
  39.             let entity = { name: 'Pesho', age: '22', birthday: new Date(1998, 0, 7) };
  40.             assert.throw(() => { repository.add(entity); }, 'Property age is not of correct type!');
  41.         });
  42.         it('Throw error for invalid type of prop 2', function () {
  43.             let entity = { name: 'Pesho', age: 22, birthday: 'new Date(1998, 0, 7)' };
  44.             assert.throw(() => { repository.add(entity); }, 'Property birthday is not of correct type!');
  45.         });
  46.         it('Return correct output', function () {
  47.             let entity = { name: 'Pesho', age: 22, birthday: new Date(1998, 0, 7) };
  48.             assert.equal(repository.add(entity), 1);
  49.             assert.equal(repository.data.size, 1);
  50.             assert.isObject(repository.data.get(1));
  51.             assert.equal(repository.add(entity), 2);
  52.             assert.equal(repository.data.size, 2);
  53.         });
  54.     });
  55.  
  56.     describe('Test getId', function () {
  57.         it('Throw error for invalid id', function () {
  58.             assert.throw(() => { repository.getId(0);}, 'Entity with id: 0 does not exist!');
  59.             assert.throw(() => { repository.getId(3);}, 'Entity with id: 3 does not exist!');
  60.         });
  61.         it('return correct output', function() {
  62.             let entity = { name: 'Pesho', age: 22, birthday: new Date(1998, 0, 7) };
  63.             assert.deepEqual(repository.data.get(1), entity);
  64.             assert.deepEqual(repository.data.get(2), entity);
  65.         });
  66.     });
  67.  
  68.     describe('Test update', function() {
  69.         let entity = { name: 'Simo', age: 32, birthday: new Date(1988, 0, 7) };
  70.         it('Throw error for invalid id', function() {
  71.             assert.throw(() => { repository.update(10, entity);}, 'Entity with id: 10 does not exist!');
  72.         });
  73.         it ('Throw error for missing prop', function() {
  74.             let entity = {age: 32, birthday: new Date(1988, 0, 7) };
  75.             assert.throw(() => { repository.update(1, entity);}, 'Property name is missing from the entity!');
  76.         });
  77.         it('throw error for invalid prop type', function() {
  78.             let entity = { name: 10, age: 32, birthday: new Date(1988, 0, 7) };
  79.             assert.throw(() => { repository.update(1, entity);}, 'Property name is not of correct type!');
  80.         });
  81.     });
  82. });
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement