-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathfluent-api-specs.js
More file actions
240 lines (236 loc) · 9.7 KB
/
Copy pathfluent-api-specs.js
File metadata and controls
240 lines (236 loc) · 9.7 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* suppress jshint warnings for chai syntax - https://github.com/chaijs/chai/issues/41#issuecomment-14904150 */
/* jshint -W024 */
/* jshint expr:true */
define([
"underscore", "backbone", "geppetto"
], function(_, Backbone, Geppetto) {
var expect = chai.expect;
describe("Backbone.Geppetto fluent API", function() {
var context;
beforeEach(function() {
context = new Geppetto.Context();
});
afterEach(function() {
context.destroy();
context = undefined;
});
describe("when retrieving objects", function() {
it("should poll the parent if no corresponding mapping was found", function(){
var value = {};
var child = new Geppetto.Context({
parentContext : context
});
context.wire(value).asValue('value');
var actual = child.getObject('value');
expect(actual).to.equal(value);
});
});
describe("when mapping a singleton", function() {
var key = 'a singleton';
var foo = {};
var contextEventSpy = sinon.spy();
var SingletonClass = function() {};
_.extend(SingletonClass.prototype, Backbone.Events);
SingletonClass.prototype.contextEvents = {
};
beforeEach(function() {
context.wire(foo).asValue('foo');
context.wire(SingletonClass)
.asSingleton(key)
.withWiring({
foo: 'foo'
})
.withContextEvents({
"event:foo" : function(){
contextEventSpy();
}
});
});
it('should be determinable', function() {
expect(context.hasWiring(key)).to.be.true;
});
it('should produce an instance of the mapped class', function() {
var actual = context.getObject(key);
expect(actual).to.be.an.instanceOf(SingletonClass);
});
it('should produce a single, unique instance', function() {
var first = context.getObject(key);
var second = context.getObject(key);
expect(second).to.equal(first);
});
it("should be instantiatable by brute force", function() {
var first = context.getObject(key);
var second = context.instantiate(key);
expect(second).to.not.equal(first);
});
it("should be injected with its dependencies when instantiated", function() {
var actual = context.getObject(key);
expect(actual.foo).to.equal(foo);
});
it("should optionally allow wiring configuration", function() {
var dependerClass = function() {};
context.wire(dependerClass)
.asSingleton('depender')
.withWiring( {
dependency: key
});
var depender = context.getObject('depender');
expect(depender.dependency).to.equal(context.getObject(key));
});
it("should map context events when configured", function(){
var actual = context.getObject(key);
context.dispatch('event:foo');
expect(contextEventSpy).to.have.been.called;
});
});
describe("when mapping a value", function() {
var key = 'a value';
var value = {};
beforeEach(function() {
context.wire(value ).asValue(key);
});
it('should be determinable', function() {
expect(context.hasWiring(key)).to.be.true;
});
it("should be retrievable", function() {
expect(context.getObject(key)).to.equal(value);
});
it("it should always return the same value", function() {
var first = context.getObject(key);
var second = context.getObject(key);
expect(second).to.equal(first);
});
});
describe("when mapping a class", function() {
var key = 'a class';
var clazz = function() {};
clazz.prototype.wiring = ['foo'];
var foo = {};
var contextEventSpy = sinon.spy();
_.extend(clazz.prototype, Backbone.Events);
beforeEach(function() {
context.wire(foo ).asValue('foo');
context.wire(clazz)
.asClass(key)
.withWiring({
foo : "foo"
})
.withContextEvents({
"event:foo" : function(){
contextEventSpy();
}
});
});
it('should be determinable', function() {
expect(context.hasWiring(key)).to.be.true;
});
it('should produce an instance of the mapped class', function() {
var actual = context.getObject(key);
expect(actual).to.be.an.instanceOf(clazz);
});
it('should produce a new instance every time', function() {
var first = context.getObject(key);
var second = context.getObject(key);
expect(second).to.not.equal(first);
});
it("should be injected with its dependencies when instantiated", function() {
var actual = context.getObject(key);
expect(actual.foo).to.equal(foo);
});
it("should optionally allow wiring configuration", function() {
var dependerClass = function() {};
context.wire(dependerClass)
.asClass('depender')
.withWiring({
dependency: key
});
var depender = context.getObject('depender');
expect(depender.dependency).to.be.an.instanceOf(clazz);
});
it("should map context events when instantiated", function(){
var actual = context.getObject(key);
context.dispatch('event:foo');
expect(contextEventSpy).to.have.been.called;
});
});
describe("when mapping a view", function() {
var key = 'a class';
var clazz;
var foo = {};
var contextEventSpy = sinon.spy();
beforeEach(function() {
contextEventSpy.reset();
clazz = Backbone.View.extend();
context.wire(clazz)
.asView(key)
.withWiring({
foo: 'foo'
})
.withContextEvents({
"event:foo" : function(){
contextEventSpy();
}
});
context.wire(foo).asValue('foo');
});
it('should be determinable', function() {
expect(context.hasWiring(key)).to.be.true;
});
it('should extend the view constructor', function() {
var actual = context.getObject(key);
expect(actual).to.be.a("function");
});
it('should retrieve the same class every time', function() {
var first = context.getObject(key);
var second = context.getObject(key);
expect(second).to.equal(first);
});
it("should call the view's original 'initialize' function when instantiated", function() {
var initializeSpy = sinon.spy();
expect(initializeSpy).not.to.have.been.called;
clazz.prototype.initialize = function() {
initializeSpy();
};
var ViewConstructor = context.getObject(key);
var viewInstance = new ViewConstructor();
expect(initializeSpy).to.have.been.calledOnce;
});
it("should be injected with its dependencies when instantiated", function() {
var ViewConstructor = context.getObject(key);
var viewInstance = new ViewConstructor();
expect(viewInstance.foo).to.equal(foo);
});
it("should map context events when instantiated", function(){
var ViewCtor = context.getObject(key);
var view = new ViewCtor();
context.dispatch('event:foo');
expect(contextEventSpy).to.have.been.called;
});
});
describe('when configuring wirings', function(){
var key = "key";
var passed;
var ctor = function(){
passed = _.toArray(arguments);
};
var payload = {};
var a = {};
var b = {};
beforeEach(function(){
passed = null;
context.wire(ctor)
.asClass(key)
.withParameters(payload, a, b)
});
afterEach(function(){
context.destroy();
});
it('should pass all arguments as payload to the constructor function', function(){
context.getObject(key);
expect(passed[0]).to.equal(payload);
expect(passed[1]).to.equal(a);
expect(passed[2]).to.equal(b);
});
});
});
});