git ssb

1+

clemo / helperjs



Tree: fbad0279dd4439c8eb8da96d385e27acd5755c74

Files: fbad0279dd4439c8eb8da96d385e27acd5755c74 / test / units / libTEST.js

4310 bytesRaw
1import Lib from '../../lib/lib.js';
2import Math from '../../lib/math.js';
3import {expect} from 'chai';
4/*globals describe,it*/
5let lib1;
6describe('lib::LIB', () => {
7 'use strict';
8 describe('constructor', () => {
9
10 let count = 0;
11 // let l = Date.now();
12 const maxMs = 1000 / 60;
13 let loop;
14 it('should store in lib', (done) => {
15 lib1 = new Lib();
16 let bob = lib1.add({name : 'bob'});
17 expect(bob).to.have.ownProperty('id', 'should have property id');
18 expect(bob).property('id').to.be.a('string', 'obj.id should be a string');
19 expect(new Math().checkUuid(bob.id))
20 .to.equal(true, 'obj.id should be a uuid');
21 expect(bob)
22 .to.have.ownProperty('name', 'should have property id')
23 .property('name')
24 .to.be.a('string', 'should be a string')
25 .equal('bob', 'should equal bob');
26
27 let max = lib1.add({name : 'max'});
28 expect(max)
29 .to.have.ownProperty('id', 'should have property id')
30 .to.have.ownProperty('name', 'should have property id')
31 .property('id')
32 .to.be.a('string', 'obj.id should be a string');
33 expect(max)
34 .property('name')
35 .to.be.a('string', 'should be a string')
36 .equal('max', 'should equal max');
37 expect(new Math().checkUuid(bob.id))
38 .to.equal(true, 'obj.id should be a uuid');
39 done();
40 });
41 it('should remove from lib', (done) => {
42 let found = 0;
43 lib1.each((obj) => {
44 found++;
45 expect(obj)
46 .to.have.ownProperty('id', 'should have property id')
47 .to.have.ownProperty('name', 'should have property id')
48 .property('name')
49 .to.be.a('string', 'should be a string');
50 if (obj.name === 'bob') {
51 lib1.remove(obj.id);
52 }
53
54 });
55 expect(found).equal(2, 'should contain 2 records (bob,max)'); // bob,max
56 expect(lib1.getArray().length)
57 .to.equal(1, 'should contain only max'); // max
58 done();
59
60 });
61 it('should store element by existing id', (done) => {
62 const lib = new Lib();
63 lib.add({id : 'one', name : 'max'});
64 lib.add({id : 'two', name : 'max2'});
65 expect(lib.getById('one'))
66 .to.have.ownProperty('id', 'getById should return item with id')
67 .to.have.ownProperty('name', 'item should have property name')
68 .property('name')
69 .to.be.a('string', 'name should be a string')
70 .equal('max', 'should be expacted value - should use added id');
71 expect(lib.getById('two'))
72 .to.have.ownProperty('id', 'getById should return item with id')
73 .to.have.ownProperty('name', 'item should have property name')
74 .property('name')
75 .to.be.a('string', 'name should be a string')
76 .equal('max2', 'should be expacted value - should use added id');
77 done();
78 });
79 it('should undefined or overwrite by adding double id', (done) => {
80 let doublet = JSON.parse(JSON.stringify(lib1.getArray()[0]));
81 let saveddoublet = JSON.parse(JSON.stringify(doublet));
82 delete doublet.name;
83 doublet.name2 = 'franz';
84 // let overwrite = lib1.add(doublet, true);
85 expect(lib1.add(doublet)).to.be.an('undefined');
86
87 let dbrecord = lib1.getById(doublet.id);
88 expect(dbrecord)
89 .to.have.ownProperty('id', 'getById should return item with id')
90 .to.have.ownProperty('name', 'item should have property name')
91 .property('name')
92 .equal(saveddoublet.name);
93 let addedRecord = lib1.add(doublet, true);
94 expect(addedRecord)
95 .to.have.ownProperty('id', 'getById should return item with id')
96 .to.have.ownProperty('name2', 'item should have property name')
97 .property('name2')
98 .to.be.a('string', 'name should be a string')
99 .equal('franz');
100 expect(addedRecord).to.not.ownProperty('name', 'name should not existis');
101
102 done();
103
104 });
105 it('should call hook beforeAdd', (done) => {
106 lib1.setHook('beforeAdd', (x) => {
107 x.hooked = true;
108 return x;
109 });
110 let item = lib1.add({id : 'hooktest'});
111 expect(item).to.have.ownProperty('hooked').property('hooked').equal(true);
112 done();
113 });
114
115 });
116});
117

Built with git-ssb-web