Skip to content
Snippets Groups Projects
libTEST.js 4.21 KiB
Newer Older
clemo's avatar
clemo committed
import Lib from '../../lib/lib.js';
import Math from '../../lib/math.js';
import {expect} from 'chai';
/*globals describe,it*/
let lib1;
describe('lib::LIB', () => {
  'use strict';
  describe('constructor', () => {

    let count = 0;
    // let l = Date.now();
    const maxMs = 1000 / 60;
    let loop;
    it('should store in lib', (done) => {
      lib1 = new Lib();
      let bob = lib1.add({name : 'bob'});
      expect(bob).to.have.ownProperty('id', 'should have property id');
      expect(bob).property('id').to.be.a('string', 'obj.id should be a string');
      expect(new Math().checkUuid(bob.id))
          .to.equal(true, 'obj.id should be a uuid');
      expect(bob)
          .to.have.ownProperty('name', 'should have property id')
          .property('name')
          .to.be.a('string', 'should be a string')
          .equal('bob', 'should equal bob');

      let max = lib1.add({name : 'max'});
      expect(max)
          .to.have.ownProperty('id', 'should have property id')
          .to.have.ownProperty('name', 'should have property id')
          .property('id')
          .to.be.a('string', 'obj.id should be a string');
      expect(max)
          .property('name')
          .to.be.a('string', 'should be a string')
          .equal('max', 'should equal max');
      expect(new Math().checkUuid(bob.id))
          .to.equal(true, 'obj.id should be a uuid');
      done();
    });
    it('should remove from lib', (done) => {
      let found = 0;
      lib1.each((obj) => {
        found++;
        expect(obj)
            .to.have.ownProperty('id', 'should have property id')
            .to.have.ownProperty('name', 'should have property id')
            .property('name')
            .to.be.a('string', 'should be a string');
        if (obj.name === 'bob') {
          lib1.remove(obj.id);
        }

      });
      expect(found).equal(2, 'should contain 2 records (bob,max)'); // bob,max
      expect(lib1.getArray().length)
          .to.equal(1, 'should contain only max'); // max
      done();

    });
    it('should store element by existing id', (done) => {
      const lib = new Lib();
      lib.add({id : 'one', name : 'max'});
      lib.add({id : 'two', name : 'max2'});
      expect(lib.getById('one'))
          .to.have.ownProperty('id', 'getById should return item with id')
          .to.have.ownProperty('name', 'item should have property name')
          .property('name')
          .to.be.a('string', 'name should be a string')
          .equal('max', 'should be expacted value - should use added id');
      expect(lib.getById('two'))
          .to.have.ownProperty('id', 'getById should return item with id')
          .to.have.ownProperty('name', 'item should have property name')
          .property('name')
          .to.be.a('string', 'name should be a string')
          .equal('max2', 'should be expacted value - should use added id');
      done();
    });
    it('should undefined or overwrite by adding double id', (done) => {
      let doublet = JSON.parse(JSON.stringify(lib1.getArray()[0]));
      let saveddoublet = JSON.parse(JSON.stringify(doublet));
      delete doublet.name;
      doublet.name2 = 'franz';
      // let overwrite = lib1.add(doublet, true);
      expect(lib1.add(doublet)).to.be.an('undefined');

      let dbrecord = lib1.getById(doublet.id);
      expect(dbrecord)
          .to.have.ownProperty('id', 'getById should return item with id')
          .to.have.ownProperty('name', 'item should have property name')
          .property('name')
          .equal(saveddoublet.name);
      let addedRecord = lib1.add(doublet, true);
      expect(addedRecord)
          .to.have.ownProperty('id', 'getById should return item with id')
          .to.have.ownProperty('name2', 'item should have property name')
          .property('name2')
          .to.be.a('string', 'name should be a string')
          .equal('franz');
      expect(addedRecord).to.not.ownProperty('name', 'name should not existis');

      done();

    });
    it('should call hook beforeAdd', (done) => {
      lib1.setHook('beforeAdd', (x) => {
        x.hooked = true;
        return x;
      });
      let item = lib1.add({id : 'hooktest'});
      expect(item).to.have.ownProperty('hooked').property('hooked').equal(true);
      done();
    });

  });
});