-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseful-dicts-tests.js
133 lines (119 loc) · 3.52 KB
/
useful-dicts-tests.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { assert } from 'meteor/practicalmeteor:chai';
import { _ } from 'meteor/underscore';
import {
Dict,
StringsDict,
FunctionsDict,
NumbersDict,
BooleansDict,
HooksDict
} from 'meteor/hotello:useful-dicts';
describe('dict', function() {
const dict = new Dict({
'test.pair': true
});
it('should set pairs', function() {
assert.throws(function() {
dict.set(null);
}, Error);
assert.equal(dict.pairs['test.pair'], true);
});
it('should get pairs', function() {
assert.throws(function() {
dict.get(false);
}, Error);
assert.throws(function() {
dict.get('test.notExising');
}, Error);
assert.equal(dict.get('test.pair'), true);
});
it('should not throw error on get when asked', function() {
const dictTwo = new Dict({}, {throwingGet: false});
assert.doesNotThrow(function() {
dictTwo.get('test.notExising');
}, Error);
assert.isUndefined(dictTwo.get('test.notExising'));
});
});
describe('strings dict', function() {
it('should set only strings', function() {
const stringsDict = new StringsDict();
assert.doesNotThrow(function() {
stringsDict.set({
'test.string': new String()
});
}, Error);
assert.throws(function() {
stringsDict.set({'test.string': false});
}, Error);
assert.isString(stringsDict.pairs['test.string']);
});
});
describe('numbers dict', function() {
it('should set only numbers', function() {
const numbersDict = new NumbersDict();
assert.doesNotThrow(function() {
numbersDict.set({
'test.number': new Number()
});
}, Error);
assert.throws(function() {
numbersDict.set({'test.number': false});
}, Error);
assert.isNumber(numbersDict.pairs['test.number']);
});
});
describe('booleans dict', function() {
it('should set only booleans', function() {
const booleansDict = new BooleansDict();
assert.doesNotThrow(function() {
booleansDict.set({
'test.boolean': true
});
}, Error);
assert.throws(function() {
booleansDict.set({'test.boolean': 'false'});
}, Error);
assert.isBoolean(booleansDict.pairs['test.boolean']);
});
});
describe('functions dict', function() {
it('should set only functions', function() {
const functionsDict = new FunctionsDict({});
assert.doesNotThrow(function() {
functionsDict.set({
'test.function': new Function()
});
}, Error);
assert.throws(function() {
functionsDict.set({'test.function': false});
}, Error);
assert.isFunction(functionsDict.pairs['test.function']);
});
});
describe('hooks dict', function() {
const hooks = new HooksDict({});
it('should accept only arrays of functions', function() {
assert.doesNotThrow(function() {
hooks.set({'test.hooks': [new Function]});
}, Error);
assert.throws(function() {
hooks.set({'test.hooks': new Function});
}, Error);
});
it('should setup a group of hooks', function() {
hooks._setup('test.hooks');
assert.isArray(hooks.pairs['test.hooks']);
assert.equal(hooks.pairs['test.hooks'].length, 0);
});
it('should add a hook to an array of hooks', function() {
hooks.add('test.hooks', new Function());
assert.isFunction(hooks.get('test.hooks')[0]);
});
it('should run all the hooks and return', function() {
const fn = (arg) => arg + 1;
assert.equal(hooks.run('test.noHooks', 'noHooks'), 'noHooks');
_.times(3, () => { hooks.add('test.sum', fn); });
assert.equal(hooks.run('test.sum', 0), 3);
});
});