forked from Automattic/mongoose
/
utils.test.js
131 lines (109 loc) · 3.52 KB
/
utils.test.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
/**
* Module dependencies.
*/
require('./common');
var utils = require('mongoose/utils')
, StateMachine = utils.StateMachine;
/**
* Setup.
*/
var ActiveRoster = StateMachine.ctor('require', 'init', 'modify');
/**
* Test.
*/
module.exports = {
'test array erasing util': function(){
function fn(){};
var arr = [fn, 'test', 1]
, arr2 = [fn, 'a', 'b', fn, 'c'];
utils.erase(arr, fn);
arr.should.have.length(2);
arr.should.eql(['test', 1]);
utils.erase(arr2, fn);
arr2.should.have.length(3);
arr2.should.eql(['a', 'b', 'c']);
},
'should detect a path as required if it has been required': function () {
var ar = new ActiveRoster();
ar.require('hello');
ar.stateOf('hello').should.equal('require');
},
'should detect a path as inited if it has been inited': function () {
var ar = new ActiveRoster();
ar.init('hello');
ar.stateOf('hello').should.equal('init');
},
'should detect a path as modified': function () {
var ar = new ActiveRoster();
ar.modify('hello');
ar.stateOf('hello').should.equal('modify');
},
'should remove a path from an old state upon a state change': function () {
var ar = new ActiveRoster();
ar.init('hello');
ar.modify('hello');
ar.states.init.should.not.have.property('hello');
ar.states.modify.should.have.property('hello');
},
'forEach should be able to iterate through the paths belonging to one state': function () {
var ar = new ActiveRoster();
ar.init('hello');
ar.init('goodbye');
ar.modify('world');
ar.require('foo');
ar.forEach('init', function (path) {
['hello', 'goodbye'].should.contain(path);
});
},
'forEach should be able to iterate through the paths in the union of two or more states': function () {
var ar = new ActiveRoster();
ar.init('hello');
ar.init('goodbye');
ar.modify('world');
ar.require('foo');
ar.forEach('modify', 'require', function (path) {
['world', 'foo'].should.contain(path);
});
},
'forEach should iterate through all paths that have any state if given no state arguments': function () {
var ar = new ActiveRoster();
ar.init('hello');
ar.init('goodbye');
ar.modify('world');
ar.require('foo');
ar.forEach(function (path) {
['hello', 'goodbye', 'world', 'foo'].should.contain(path);
});
},
'should be able to detect if at least one path exists in a set of states': function () {
var ar = new ActiveRoster();
ar.init('hello');
ar.modify('world');
ar.some('init').should.be.true;
ar.some('modify').should.be.true;
ar.some('require').should.be.false;
ar.some('init', 'modify').should.be.true;
ar.some('init', 'require').should.be.true;
ar.some('modify', 'require').should.be.true;
},
'should be able to `map` over the set of paths in a given state': function () {
var ar = new ActiveRoster();
ar.init('hello');
ar.modify('world');
ar.require('iAmTheWalrus');
var suffixedPaths = ar.map('init', 'modify', function (path) {
return path + '-suffix';
});
suffixedPaths.should.eql(['hello-suffix', 'world-suffix']);
},
"should `map` over all states' paths if no states are specified in a `map` invocation": function () {
var ar = new ActiveRoster();
ar.init('hello');
ar.modify('world');
ar.require('iAmTheWalrus');
var suffixedPaths = ar.map(function (path) {
return path + '-suffix';
});
suffixedPaths.should.eql(['iAmTheWalrus-suffix', 'hello-suffix', 'world-suffix']);
}
};