-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
205 lines (165 loc) · 5.76 KB
/
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
var DS = require('../.'),
expect = require('chai').expect,
sinon = require('sinon'),
fs = require('fs'),
vm = require('vm');
var MockElement = function () {
this.nodeName = 'nodeNameString';
this.nodeType = 1;
};
MockElement.prototype = {
addEventListener: function () {},
querySelectorAll: function () {}
};
describe('', function () {
var sandbox,
fakeName = 'jkfldsa',
anotherFakeName = 'kalsdew',
yetAnotherFakeName = 'sdkljgre',
el = new MockElement(),
anotherEl = new MockElement();
beforeEach(function () {
sandbox = sinon.sandbox.create();
});
afterEach(function () {
sandbox.restore();
});
it('returns singleton', function () {
expect(DS).to.deep.equal(require('../.'));
});
it('throws if name does not exist', function () {
expect(function () {
DS.get(fakeName);
}).to.throw('Cannot find module');
});
it('throws if definition does not start with name as a string', function () {
expect(function () {
DS.controller(function () { return function () {}; }, fakeName);
}).to.throw(Error);
});
it('throws if definition is missing second argument', function () {
expect(function () {
DS.controller(fakeName);
}).to.throw(Error);
});
it('throws if definition function is not last in array', function () {
expect(function () {
DS.controller(fakeName, ['thing', function () {}, 'thing']);
}).to.throw(Error);
});
it('controller fails get without element', function () {
DS.controller(fakeName, function () { return function () {}; });
expect(function () {
DS.get(fakeName);
}).to.throw(Error);
});
it('controller fails on missing service', function () {
DS.controller(fakeName, [anotherFakeName, function () { return function () {}; }]);
expect(function () {
DS.get(fakeName);
}).to.throw(Error);
});
it('controller succeeds with element', function () {
DS.controller(fakeName, function () { return function () {}; });
expect(function () {
DS.get(fakeName, el);
}).to.not.throw(Error);
});
it('requires controller if not defined', function () {
expect(function () {
DS.get(__dirname + '/testctrl', el);
}).to.not.throw(Error);
});
it('controller creates service lazily', function () {
//this would fail if the service is created too early
DS.controller(fakeName, [anotherFakeName, function () { return function () {}; }]);
DS.service(anotherFakeName, function () {});
expect(function () {
DS.get(fakeName, el);
}).to.not.throw(Error);
});
it('controller creates service already created', function () {
//this would fail if the service wasn't shared
DS.controller(fakeName, [anotherFakeName, function (ref) { return function () { this.service = ref; }; }]);
DS.service(anotherFakeName, function () {});
DS.controller(yetAnotherFakeName, [anotherFakeName, function (ref) { return function () { this.service = ref; }; }]);
expect(DS.get(fakeName, el).service === DS.get(yetAnotherFakeName, el).service).to.equal(true);
});
it('controller binds events to first element in list of arguments only', function () {
sandbox.mock(el).expects('addEventListener').once();
sandbox.mock(anotherEl).expects('addEventListener').never();
DS.controller(fakeName, function () {
var constructor = function () {};
constructor.prototype = {
events: {
click: 'someFunctionName'
},
someFunctionName: function () {}
};
return constructor;
});
DS.get(fakeName, 'ytes', el, 8905, anotherEl);
sandbox.verify();
});
it('controller can bind events to selector', function () {
sandbox.mock(el).expects('querySelectorAll')
.withArgs('some selector.of #some[length=*]').once()
.returns([anotherEl]);
sandbox.mock(anotherEl).expects('addEventListener')
.withArgs('click', sinon.match.func).once();
DS.controller(fakeName, function () {
var constructor = function () {};
constructor.prototype = {
events: {
'some selector.of #some[length=*] click': 'someFunctionName'
},
someFunctionName: function () {}
};
return constructor;
});
DS.get(fakeName, el);
sandbox.verify();
});
it('getting a controller twice returns two different instances', function () {
DS.controller(fakeName, function () { return function () {}; });
expect(DS.get(fakeName, el)).to.not.equal(DS.get(fakeName, el));
});
it('getting a service twice returns the same instance', function () {
DS.service(fakeName, function () {});
expect(DS.get(fakeName, el)).to.equal(DS.get(fakeName, el));
});
it('service can be injected with other services', function () {
DS.service(fakeName, [anotherFakeName, function () {}]);
DS.service(anotherFakeName, function () {});
expect(function () {
DS.get(fakeName);
}).to.not.throw(Error);
});
it('can get values within controllers', function () {
var thing = {};
DS.value(fakeName, thing);
DS.controller(anotherFakeName, [fakeName, function (f) {
expect(f).to.equal(thing);
return function () {};
}]);
DS.get(anotherFakeName, el);
});
it('can get values within services', function () {
var thing = {};
DS.value(fakeName, thing);
DS.service(anotherFakeName, [fakeName, function (f) {
expect(f).to.equal(thing);
}]);
DS.get(anotherFakeName);
});
it('when require is missing', function () {
var scriptText = fs.readFileSync('index.js', {encoding: 'UTF8'}) + '; DS.get("hi")',
sandbox = {},
context = new vm.createContext(sandbox),
script = new vm.Script(scriptText);
expect(function () {
script.runInContext(context);
}).to.throw('hi is not defined');
});
});