Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let { Repository } = require('./solution.js');
- const { assert } = require('chai');
- describe('Test Repository', function () {
- let properties = { name: 'string', age: 'number', birthday: 'object' };
- let repository = new Repository(properties);
- describe('Test instantiation', function () {
- it('Test constructor', function () {
- assert.isObject(repository.props);
- assert.instanceOf(repository, Repository);
- assert.instanceOf(repository.data, Map);
- assert.equal(repository.nextId(), 0);
- });
- it('Test if have all functions', function () {
- assert.equal(typeof repository.add, 'function');
- assert.isFunction(repository.getId);
- assert.isFunction(repository.update);
- assert.isFunction(repository.del);
- assert.isFunction(repository._validate);
- });
- });
- describe('Test getter count', function () {
- it('Returns correct output count', function () { assert.equal(repository.count, 0); });
- });
- describe('Test add', function () {
- it('Throw error for missing prop', function () {
- let entity = { name: 'Pesho' };
- assert.throw(() => { repository.add(entity); }, 'Property age is missing from the entity!');
- });
- it('Throw error for missing prop 2', function () {
- let entity = { age: 22, birthday: new Date(1998, 0, 7) };
- assert.throw(() => { repository.add(entity); }, 'Property name is missing from the entity!');
- });
- it('Throw error for invalid type of prop', function () {
- let entity = { name: 'Pesho', age: '22', birthday: new Date(1998, 0, 7) };
- assert.throw(() => { repository.add(entity); }, 'Property age is not of correct type!');
- });
- it('Throw error for invalid type of prop 2', function () {
- let entity = { name: 'Pesho', age: 22, birthday: 'new Date(1998, 0, 7)' };
- assert.throw(() => { repository.add(entity); }, 'Property birthday is not of correct type!');
- });
- it('Return correct output', function () {
- let entity = { name: 'Pesho', age: 22, birthday: new Date(1998, 0, 7) };
- assert.equal(repository.add(entity), 1);
- assert.equal(repository.data.size, 1);
- assert.isObject(repository.data.get(1));
- assert.equal(repository.add(entity), 2);
- assert.equal(repository.data.size, 2);
- });
- });
- describe('Test getId', function () {
- it('Throw error for invalid id', function () {
- assert.throw(() => { repository.getId(0);}, 'Entity with id: 0 does not exist!');
- assert.throw(() => { repository.getId(3);}, 'Entity with id: 3 does not exist!');
- });
- it('return correct output', function() {
- let entity = { name: 'Pesho', age: 22, birthday: new Date(1998, 0, 7) };
- assert.deepEqual(repository.data.get(1), entity);
- assert.deepEqual(repository.data.get(2), entity);
- });
- });
- describe('Test update', function() {
- let entity = { name: 'Simo', age: 32, birthday: new Date(1988, 0, 7) };
- it('Throw error for invalid id', function() {
- assert.throw(() => { repository.update(10, entity);}, 'Entity with id: 10 does not exist!');
- });
- it ('Throw error for missing prop', function() {
- let entity = {age: 32, birthday: new Date(1988, 0, 7) };
- assert.throw(() => { repository.update(1, entity);}, 'Property name is missing from the entity!');
- });
- it('throw error for invalid prop type', function() {
- let entity = { name: 10, age: 32, birthday: new Date(1988, 0, 7) };
- assert.throw(() => { repository.update(1, entity);}, 'Property name is not of correct type!');
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement