Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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();
});
});
});