-
Notifications
You must be signed in to change notification settings - Fork 19
/
child-apps.spec.js
450 lines (354 loc) · 13.7 KB
/
child-apps.spec.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
describe('ChildAppMixin', function() {
beforeEach(function() {
this.MyApp = Marionette.Toolkit.App.extend({
fooOption: 'bar'
});
this.myApp = new this.MyApp();
this.childApps = {
cA1: Marionette.Toolkit.App,
cA2: Marionette.Toolkit.App,
cA3: Marionette.Toolkit.App
};
});
describe('when instantiating', function() {
describe('without declared child apps', function() {
describe('childApps object', function() {
it('should not be created', function() {
expect(this.myApp.childApps).to.be.undefined;
});
});
describe('_childApps object', function() {
it('should be created but empty', function() {
expect(this.myApp.childApps).to.not.be.null;
expect(_.keys(this.myApp._childApps)).to.have.length(0);
});
});
describe('_initChildApps', function() {
it('should not have error', function() {
this.sinon.spy(this.myApp, '_initChildApps');
this.myApp._initChildApps();
expect(this.myApp._initChildApps.called);
expect(_.bind(this.myApp._initChildApps, this.myApp)).to.not.throw(Error);
});
});
describe('addChildApp function', function() {
it('should raise an error message', function() {
const errMessage = 'App build failed. Incorrect configuration.';
// Bind function because `this` needs to be the other describe block
expect(_.bind(this.myApp.addChildApp, this.myApp)).to.throw(errMessage);
});
});
});
describe('with declared child apps', function() {
describe('constructor', function() {
beforeEach(function() {
const childApps = {
cA1: Marionette.Toolkit.App,
cA2: Marionette.Toolkit.App,
cA3: Marionette.Toolkit.App
};
this.myApp = new Marionette.Toolkit.App({ childApps: childApps });
});
it('should create childApps and _childApps attributes with childApps inside', function() {
expect(_.keys(this.myApp.childApps)).to.have.length(3);
expect(_.keys(this.myApp._childApps)).to.have.length(3);
});
});
describe('_initChildApps', function() {
it('should accept a hash', function() {
const childApps = {
cA1: Marionette.Toolkit.App,
cA2: Marionette.Toolkit.App,
cA3: Marionette.Toolkit.App
};
const MyApp = Marionette.Toolkit.App.extend();
this.sinon.spy(MyApp.prototype, '_initChildApps');
this.myApp = new MyApp({ childApps: childApps });
expect(this.myApp._initChildApps.called);
expect(_.keys(this.myApp._childApps)).to.have.length(3);
});
describe('when passing childApps as a function', function() {
beforeEach(function() {
const childApps = function() {
return {
cA1: Marionette.Toolkit.App,
cA2: Marionette.Toolkit.App
};
};
this.MyApp2 = Marionette.Toolkit.App.extend({
childApps: childApps
});
});
it('should use the results of the childApps function', function() {
this.myApp = new this.MyApp2();
expect(_.keys(this.myApp._childApps)).to.have.length(2);
});
it('should pass options to childApps', function() {
const opts = { fooOption: 'bar' };
this.sinon.stub(this.MyApp2.prototype, 'childApps');
this.myApp = new this.MyApp2(opts);
expect(this.MyApp2.prototype.childApps)
.to.have.been.calledOnce
.and.calledWith(opts);
});
});
});
});
describe('when passing options using childAppOptions on parent app', function() {
beforeEach(function() {
this.MyApp = Marionette.Toolkit.App;
this.myApp = new this.MyApp({
childAppOptions: {
myChildOption: 'bar',
bazOption: false
}
});
this.childApps = {
cA1: Marionette.Toolkit.App,
cA2: {
AppClass: Marionette.Toolkit.App,
bazOption: true,
fooOption: true
}
};
this.myApp.addChildApps(this.childApps);
});
it('should attach options to each child', function() {
expect(this.myApp.getChildApp('cA1').getOption('myChildOption')).to.equal('bar');
expect(this.myApp.getChildApp('cA2').getOption('myChildOption')).to.equal('bar');
});
it('should not override options passed in with childApp definition', function() {
expect(this.myApp.getChildApp('cA2').getOption('bazOption')).to.equal(true);
});
});
});
describe('when adding a child app', function() {
describe('using addChildApp with an object literal', function() {
beforeEach(function() {
this.myApp.addChildApp('newChildApp', {
AppClass: Marionette.Toolkit.App,
bazOption: true
});
});
it('should contain the options from the initial MyApp definition', function() {
expect(this.myApp.getOption('fooOption')).to.equal('bar');
});
it('should contain the options on the added childApp', function() {
expect(this.myApp.getChildApp('newChildApp').getOption('bazOption')).to.equal(true);
});
});
describe('using addChildApps', function() {
beforeEach(function() {
this.sinon.spy(this.myApp, 'addChildApps');
this.myApp.addChildApps(this.childApps);
});
it('should be called', function() {
expect(this.myApp.addChildApps.called).to.be.true;
});
it('should have arguments', function() {
expect(this.myApp.addChildApps.calledWith(this.childApps)).to.be.true;
});
});
});
describe('_ensureAppIsUnique', function() {
beforeEach(function() {
this.thirdChildAppName = 'cA3';
this.fourthChildAppName = 'cA4';
this.errMessage = 'A child App with name "cA3" has already been added.';
this.myApp = new Marionette.Toolkit.App();
this.myApp.addChildApps(this.childApps);
});
it('should throw error if a duplicate child exists', function() {
expect(_.bind(function() {
this.myApp._ensureAppIsUnique(this.thirdChildAppName);
}, this)).to.throw(this.errMessage);
});
it('should not throw an error if new child app is not a duplicate', function() {
expect(_.bind(function() {
this.myApp._ensureAppIsUnique(this.fourthChildAppName);
}, this)).to.not.throw(Error);
});
});
describe('addChildApp', function() {
beforeEach(function() {
this.sinon.spy(this.myApp, 'addChildApp');
this.myApp.addChildApps(this.childApps);
});
it('should be called three times', function() {
expect(this.myApp.addChildApp.callCount === 3).to.be.true;
});
it('should assign the childApp name to the passed in appName', function() {
expect(this.myApp.addChildApp('cA4', Marionette.Toolkit.App)).to.have.property('_name', 'cA4');
});
describe('when the child startAfterInitialize', function() {
it('should remove the child if destroyed', function() {
this.myApp.addChildApp('foo', Marionette.Toolkit.App.extend({ startAfterInitialized: true }));
this.myApp.getChildApp('foo').destroy();
expect(this.myApp.getChildApp('foo')).to.be.undefined;
});
});
});
describe('getName', function() {
beforeEach(function() {
this.sinon.spy(this.myApp, 'addChildApp');
this.myApp.addChildApps(this.childApps);
});
it('should return the name of the childApp', function() {
expect(this.myApp.getChildApp('cA1').getName()).to.equal('cA1');
});
it('should return undefined for an app that is not a child app', function() {
expect(this.myApp.getName()).to.be.undefined;
});
it('should return undefined if a childApp has been removed from parent', function() {
const childApp = this.myApp.getChildApp('cA3');
this.myApp.removeChildApp('cA3');
expect(childApp.getName()).to.be.undefined;
});
});
describe('buildApp', function() {
describe('when passing an object', function() {
it('should return and instance of the class', function() {
const foo = this.myApp.buildApp(Marionette.Toolkit.App);
expect(foo).to.be.instanceOf(Marionette.Toolkit.App);
});
});
});
describe('getChildApps', function() {
before(function() {
this.myApp = new Marionette.Toolkit.App({ childApps: this.childApps });
});
it('should return are registered childApps', function() {
const childAppKeys = _.keys(this.myApp.getChildApps());
expect(childAppKeys).to.have.length(3);
expect(childAppKeys).to.eql(_.keys(this.childApps));
});
});
describe('getChildApp', function() {
beforeEach(function() {
this.myApp = new Marionette.Toolkit.App({ childApps: this.childApps });
});
describe('with existing childApp', function() {
it('should return childApp object', function() {
const existingChildApp = this.myApp._childApps.cA1;
expect(this.myApp.getChildApp('cA1')).to.eql(existingChildApp);
});
});
describe('with nonexisting childApp', function() {
it('should not return a childApp object', function() {
expect(this.myApp.getChildApp('cA4')).to.not.exist;
this.myApp.addChildApp('cA4', Marionette.Toolkit.App);
expect(this.myApp.getChildApp('cA4')).to.exist;
});
});
});
describe('removeChildApps', function() {
beforeEach(function() {
this.myApp = new Marionette.Toolkit.App({ childApps: this.childApps });
});
it('should remove all childApps', function() {
expect(this.myApp._childApps).to.not.be.empty;
this.myApp.removeChildApps();
expect(this.myApp._childApps).to.be.empty;
});
});
describe('removeChildApp', function() {
beforeEach(function() {
this.myApp = new Marionette.Toolkit.App({ childApps: this.childApps });
this.spy = sinon.spy(this.myApp, 'removeChildApps');
});
describe('when childApp is not present', function() {
it('should return undefined', function() {
expect(this.myApp.removeChildApp('cA4')).to.eql(undefined);
});
});
describe('when childApp is present', function() {
it('should remove childApp and return it', function() {
this.myApp.addChildApp('cA4', Marionette.Toolkit.App);
expect(this.myApp.removeChildApp('cA4')).to.not.eql(undefined);
});
});
});
describe('startChildApp', function() {
before(function() {
const childApps = {
cA1: Marionette.Toolkit.App,
cA2: Marionette.Toolkit.App.extend({
onStart(options) {
this.mergeOptions(options, ['foo']);
}
}),
cA3: Marionette.Toolkit.App,
cA4: {
AppClass: Marionette.Toolkit.App,
regionName: 'region'
},
cA5: {
AppClass: Marionette.Toolkit.App,
getOptions: ['foo', 'bar']
}
};
this.myApp = new Marionette.Toolkit.App({ childApps });
});
it('should start specified childApp', function() {
this.myChildApp = this.myApp.startChildApp('cA1');
expect(this.myChildApp.isRunning()).to.be.true;
});
it('should start childApp with options', function() {
this.myChildApp = this.myApp.startChildApp('cA2', { foo: 'bar' });
expect(this.myChildApp.getOption('foo')).to.eq('bar');
});
it('should return childApp instance', function() {
const spy = sinon.spy(this.myApp, 'startChildApp');
this.myChildApp = this.myApp.startChildApp('cA1');
expect(spy.returned(this.myChildApp)).to.be.true;
});
describe('when regionName is defined', function() {
it('should set the region from the app view on the child app', function() {
const view = new Marionette.View({
template: _.template('<div id="region"></div>'),
regions: { region: '#region' }
});
this.myApp.setView(view);
this.myChildApp = this.myApp.startChildApp('cA4');
expect(this.myChildApp.getRegion()).to.equal(this.myApp.getRegion('region'));
});
});
describe('when getOptions is defined', function() {
it('should set the options from the app on the child app', function() {
this.myApp.foo = 'foo';
this.myApp.bar = 'bar';
this.myChildApp = this.myApp.getChildApp('cA5');
const spy = sinon.spy(this.myChildApp, 'start');
this.myApp.startChildApp('cA5', { bar: 'bar2', baz: 'baz' });
expect(spy)
.to.be.calledOnce
.and.calledWith({ foo: 'foo', bar: 'bar2', baz: 'baz', region: undefined });
});
});
});
describe('stopChildApp', function() {
before(function() {
const childApps = {
cA1: Marionette.Toolkit.App,
cA2: Marionette.Toolkit.App,
cA3: Marionette.Toolkit.App
};
this.myApp = new Marionette.Toolkit.App({ childApps: childApps });
this.myChildApp = this.myApp.startChildApp('cA1');
});
it('should stop specified childApp', function() {
this.myApp.stopChildApp('cA1');
expect(this.myChildApp.isRunning()).to.be.false;
});
it('should stop childApp with options', function() {
const spy = sinon.spy(this.myChildApp, 'stop');
this.myApp.stopChildApp('cA1', { foo: 'bar' });
expect(spy.calledWith({ foo: 'bar' })).to.be.true;
});
it('should return parentApp instance', function() {
const spy = sinon.spy(this.myApp, 'stopChildApp');
this.myApp.stopChildApp('cA1');
expect(spy.returned(this.myChildApp)).to.be.true;
});
});
});