UNPKG

16.9 kBPlain TextView Raw
1import { expect } from 'chai';
2import sinon from 'sinon';
3import Reflux from 'reflux';
4import { AppRegistry } from './';
5import { Actions } from './actions';
6
7describe('AppRegistry', function () {
8 describe('getStore', function () {
9 context('when the store does not exist', function () {
10 let registry: AppRegistry;
11 let storeLike;
12
13 before(function () {
14 registry = new AppRegistry();
15 storeLike = registry.getStore('Test.Store');
16 });
17
18 it('does not return undefined', function () {
19 expect(storeLike).to.not.equal(undefined);
20 });
21
22 it('returns a store-like object', function (done) {
23 const unsubscribe = storeLike.listen((value) => {
24 expect(value).to.equal('test');
25 unsubscribe();
26 done();
27 });
28 storeLike.trigger('test');
29 });
30
31 it('flags the non-existant request', function () {
32 expect(registry.storeMisses['Test.Store']).to.equal(1);
33 });
34
35 context('when asking for a missing store more than once', function () {
36 before(function () {
37 registry.getStore('Test.Store');
38 });
39
40 it('updates the miss count', function () {
41 expect(registry.storeMisses['Test.Store']).to.equal(2);
42 });
43 });
44 });
45 });
46
47 describe('#onActivated', function () {
48 context('when the method is defined on the store', function () {
49 let registry: AppRegistry;
50 let spyOne: any;
51 let spyTwo: any;
52 let storeOne: any;
53 let storeTwo: any;
54
55 beforeEach(function () {
56 spyOne = sinon.spy();
57 spyTwo = sinon.spy();
58 storeOne = Reflux.createStore({
59 onActivated: (reg) => {
60 spyOne(reg);
61 },
62 });
63 storeTwo = Reflux.createStore({
64 onActivated: (reg) => {
65 spyTwo(reg);
66 },
67 });
68 registry = new AppRegistry()
69 .registerStore('TestStore1', storeOne)
70 .registerStore('TestStore2', storeTwo);
71 });
72
73 it('calls onActivated on the store', function () {
74 registry.onActivated();
75 expect(spyOne.calledWith(registry)).to.equal(true);
76 expect(spyTwo.calledWith(registry)).to.equal(true);
77 });
78 });
79
80 context('when the method is not defined on the store', function () {
81 let registry: AppRegistry;
82 let store: any;
83
84 beforeEach(function () {
85 store = Reflux.createStore({});
86 registry = new AppRegistry().registerStore('TestStore', store);
87 });
88
89 it('does not call onActivated on the store', function () {
90 expect(registry.onActivated()).to.equal(registry);
91 });
92 });
93 });
94
95 describe('#emit data-service-connected', function () {
96 context('when a listener exists', function () {
97 let registry: AppRegistry;
98 let spy: any;
99 let store: any;
100
101 beforeEach(function () {
102 spy = sinon.spy();
103 store = Reflux.createStore({
104 onActivated: (ar) => {
105 ar.on('data-service-connected', (error, ds) => {
106 spy(error, ds);
107 });
108 },
109 });
110 registry = new AppRegistry().registerStore('TestStore', store);
111 registry.onActivated();
112 });
113
114 it('calls onConnected on the store', function () {
115 registry.emit('data-service-connected', 'error', 'ds');
116 expect(spy.callCount).to.equal(1);
117 });
118 });
119 });
120
121 describe('#registerAction', function () {
122 let registry: AppRegistry;
123
124 beforeEach(function () {
125 registry = new AppRegistry().registerAction('TestAction', 'testing');
126 });
127
128 it('registers the action', function () {
129 expect(registry.actions.TestAction).to.equal('testing');
130 });
131
132 it('allows access via the getter', function () {
133 expect(registry.getAction('TestAction')).to.equal('testing');
134 });
135
136 it('publishes an action registered action', function (done) {
137 const unsubscribe = Actions.actionRegistered.listen((name) => {
138 expect(name).to.equal('TestAction');
139 unsubscribe();
140 done();
141 });
142 });
143
144 context('when the action already exists', function () {
145 beforeEach(function () {
146 registry.registerAction('TestAction', 'override');
147 });
148
149 it('publishes an action overridden action', function (done) {
150 const unsubscribe = Actions.actionOverridden.listen((name) => {
151 expect(name).to.equal('TestAction');
152 unsubscribe();
153 done();
154 });
155 });
156 });
157 });
158
159 describe('#registerComponent', function () {
160 let registry: AppRegistry;
161
162 beforeEach(function () {
163 registry = new AppRegistry().registerComponent('IndexView', 'testing');
164 });
165
166 it('registers the component', function () {
167 expect(registry.components.IndexView).to.equal('testing');
168 });
169
170 it('allows access via the getter', function () {
171 expect(registry.getComponent('IndexView')).to.equal('testing');
172 });
173
174 it('publishes a component registered action', function (done) {
175 const unsubscribe = Actions.componentRegistered.listen((name) => {
176 expect(name).to.equal('IndexView');
177 unsubscribe();
178 done();
179 });
180 });
181
182 context('when the component already exists', function () {
183 beforeEach(function () {
184 registry.registerComponent('IndexView', 'override');
185 });
186
187 it('publishes a component overridden action', function (done) {
188 const unsubscribe = Actions.componentOverridden.listen((name) => {
189 expect(name).to.equal('IndexView');
190 unsubscribe();
191 done();
192 });
193 });
194 });
195 });
196
197 describe('#registerRole', function () {
198 let registry: AppRegistry;
199
200 const role = {
201 component: 'collection-tab',
202 name: 'another tab',
203 order: 2,
204 minimumServerVersion: '3.2.0-rc0',
205 };
206
207 const roleTwo = {
208 component: 'collection-tab-two',
209 name: 'another tab two',
210 order: 1,
211 };
212
213 const roleThree = {
214 component: 'collection-tab-three',
215 name: 'another tab three',
216 };
217
218 beforeEach(function () {
219 registry = new AppRegistry().registerRole('Role.Collection.Tab', role);
220 });
221
222 it('registers the component', function () {
223 expect(registry.roles['Role.Collection.Tab']).to.deep.equal([role]);
224 });
225
226 it('allows access via the getter', function () {
227 expect(registry.getRole('Role.Collection.Tab')).to.deep.equal([role]);
228 });
229
230 it('publishes a role registered action', function (done) {
231 const unsubscribe = Actions.roleRegistered.listen((name) => {
232 expect(name).to.equal('Role.Collection.Tab');
233 unsubscribe();
234 done();
235 });
236 });
237
238 context('when the component already exists', function () {
239 beforeEach(function () {
240 registry.registerRole('Role.Collection.Tab', role);
241 });
242
243 it('does not register the duplicate', function () {
244 expect(registry.roles['Role.Collection.Tab']).to.deep.equal([role]);
245 });
246 });
247
248 context('when the component does not already exists', function () {
249 context('when the role defines an order', function () {
250 beforeEach(function () {
251 registry.registerRole('Role.Collection.Tab', roleTwo);
252 });
253
254 it('registers the additional component in order', function () {
255 expect(registry.roles['Role.Collection.Tab']).to.deep.equal([
256 roleTwo,
257 role,
258 ]);
259 });
260 });
261
262 context('when the role does not define an order', function () {
263 beforeEach(function () {
264 registry
265 .registerRole('Role.Collection.Tab', roleTwo)
266 .registerRole('Role.Collection.Tab', roleThree);
267 });
268
269 it('registers the additional component in order', function () {
270 expect(registry.roles['Role.Collection.Tab']).to.deep.equal([
271 roleTwo,
272 role,
273 roleThree,
274 ]);
275 });
276 });
277 });
278 });
279
280 describe('#registerStore', function () {
281 let registry: AppRegistry;
282
283 beforeEach(function () {
284 registry = new AppRegistry().registerStore('IndexStore', 'testing');
285 });
286
287 it('registers the store', function () {
288 expect(registry.stores.IndexStore).to.equal('testing');
289 });
290
291 it('allows access via the getter', function () {
292 expect(registry.getStore('IndexStore')).to.equal('testing');
293 });
294
295 it('publishes a store registered action', function (done) {
296 const unsubscribe = Actions.storeRegistered.listen((name) => {
297 expect(name).to.equal('IndexStore');
298 unsubscribe();
299 done();
300 });
301 });
302
303 context('when the store already exists', function () {
304 beforeEach(function () {
305 registry.registerStore('IndexStore', 'override');
306 });
307
308 it('publishes a store overridden action', function (done) {
309 const unsubscribe = Actions.storeOverridden.listen((name) => {
310 expect(name).to.equal('IndexStore');
311 unsubscribe();
312 done();
313 });
314 });
315 });
316 });
317
318 describe('#deregisterAction', function () {
319 context('when the action exists', function () {
320 let registry: AppRegistry;
321
322 beforeEach(function () {
323 registry = new AppRegistry()
324 .registerAction('TestAction', 'testing')
325 .deregisterAction('TestAction');
326 });
327
328 it('deregisters the action', function () {
329 expect(registry.actions.TestAction).to.equal(undefined);
330 });
331
332 it('publishes an action deregisted action', function (done) {
333 const unsubscribe = Actions.actionDeregistered.listen((name) => {
334 expect(name).to.equal('TestAction');
335 unsubscribe();
336 done();
337 });
338 });
339 });
340 });
341
342 describe('#deregisterComponent', function () {
343 context('when the component exists', function () {
344 let registry: AppRegistry;
345
346 beforeEach(function () {
347 registry = new AppRegistry()
348 .registerComponent('TestComponent', 'testing')
349 .deregisterComponent('TestComponent');
350 });
351
352 it('deregisters the component', function () {
353 expect(registry.components.TestComponent).to.equal(undefined);
354 });
355
356 it('publishes a component deregisted action', function (done) {
357 const unsubscribe = Actions.componentDeregistered.listen((name) => {
358 expect(name).to.equal('TestComponent');
359 unsubscribe();
360 done();
361 });
362 });
363 });
364 });
365
366 describe('#deregisterRole', function () {
367 const role = {
368 component: 'collection-tab',
369 name: 'another tab',
370 order: 2,
371 minimumServerVersion: '3.2.0-rc0',
372 };
373
374 const roleTwo = {
375 component: 'collection-tab-two',
376 name: 'another tab two',
377 order: 1,
378 };
379
380 context('when the role exists', function () {
381 let registry: AppRegistry;
382
383 beforeEach(function () {
384 registry = new AppRegistry()
385 .registerRole('TestRole', role)
386 .registerRole('TestRole', roleTwo)
387 .deregisterRole('TestRole', roleTwo);
388 });
389
390 it('deregisters the role', function () {
391 expect(registry.roles.TestRole).to.deep.equal([role]);
392 });
393
394 it('publishes a role deregisted action', function (done) {
395 const unsubscribe = Actions.roleDeregistered.listen((name) => {
396 expect(name).to.equal('TestRole');
397 unsubscribe();
398 done();
399 });
400 });
401 });
402 });
403
404 describe('#deregisterStore', function () {
405 context('when the store exists', function () {
406 let registry: AppRegistry;
407
408 beforeEach(function () {
409 registry = new AppRegistry()
410 .registerStore('TestStore', 'testing')
411 .deregisterStore('TestStore');
412 });
413
414 it('deregisters the store', function () {
415 expect(registry.stores.TestStore).to.equal(undefined);
416 });
417
418 it('publishes a store deregisted action', function (done) {
419 const unsubscribe = Actions.storeDeregistered.listen((name) => {
420 expect(name).to.equal('TestStore');
421 unsubscribe();
422 done();
423 });
424 });
425 });
426 });
427
428 describe('#emit', function () {
429 let registry: AppRegistry;
430
431 beforeEach(function () {
432 registry = new AppRegistry();
433 });
434
435 it('emits the event', function (done) {
436 registry.once('test-event', (value) => {
437 expect(value).to.equal('test');
438 done();
439 });
440 registry.emit('test-event', 'test');
441 });
442 });
443
444 describe('#on', function () {
445 let registry: AppRegistry;
446
447 beforeEach(function () {
448 registry = new AppRegistry();
449 });
450
451 it('subscribes to the event', function (done) {
452 registry.on('test-event', (value) => {
453 expect(value).to.equal('test');
454 done();
455 });
456 registry.emit('test-event', 'test');
457 });
458 });
459
460 describe('#once', function () {
461 let registry: AppRegistry;
462
463 beforeEach(function () {
464 registry = new AppRegistry();
465 });
466
467 it('subscribes to the event once', function (done) {
468 registry.once('test-event', (value) => {
469 expect(value).to.equal('test');
470 done();
471 });
472 registry.emit('test-event', 'test');
473 });
474 });
475
476 describe('#addListener', function () {
477 let registry: AppRegistry;
478
479 beforeEach(function () {
480 registry = new AppRegistry();
481 });
482
483 it('adds the listener to the event', function (done) {
484 registry.addListener('test-event', (value) => {
485 expect(value).to.equal('test');
486 done();
487 });
488 registry.emit('test-event', 'test');
489 });
490 });
491
492 describe('#removeListener', function () {
493 let registry: AppRegistry;
494 const listener = () => {
495 return true;
496 };
497
498 beforeEach(function () {
499 registry = new AppRegistry();
500 registry.addListener('test-event', listener);
501 });
502
503 it('removes the listener', function () {
504 registry.removeListener('test-event', listener);
505 expect(registry.listenerCount('test-event')).to.equal(0);
506 });
507 });
508
509 describe('#removeAllListeners', function () {
510 let registry: AppRegistry;
511 const listenerOne = () => {
512 return true;
513 };
514 const listenerTwo = () => {
515 return true;
516 };
517
518 beforeEach(function () {
519 registry = new AppRegistry();
520 registry.addListener('test-event', listenerOne);
521 registry.addListener('test-event', listenerTwo);
522 });
523
524 it('removes all the listeners', function () {
525 registry.removeAllListeners('test-event');
526 expect(registry.listenerCount('test-event')).to.equal(0);
527 });
528 });
529
530 describe('#eventNames', function () {
531 let registry: AppRegistry;
532
533 beforeEach(function () {
534 registry = new AppRegistry();
535 registry.on('test-event', () => {
536 return true;
537 });
538 });
539
540 it('returns the names of all events', function () {
541 expect(registry.eventNames()).to.deep.equal(['test-event']);
542 });
543 });
544
545 describe('#listeners', function () {
546 let registry: AppRegistry;
547 const listenerOne = () => {
548 return true;
549 };
550 const listenerTwo = () => {
551 return true;
552 };
553
554 beforeEach(function () {
555 registry = new AppRegistry();
556 registry.addListener('test-event', listenerOne);
557 registry.addListener('test-event', listenerTwo);
558 });
559
560 it('returns the listeners for the event', function () {
561 expect(registry.listeners('test-event')).to.deep.equal([
562 listenerOne,
563 listenerTwo,
564 ]);
565 });
566 });
567
568 context('when freezing the app registry', function () {
569 let registry: AppRegistry;
570
571 beforeEach(function () {
572 registry = Object.freeze(new AppRegistry());
573 });
574
575 context('when adding a listener', function () {
576 const addListener = () => {
577 registry.on('test', () => {
578 return true;
579 });
580 };
581
582 it('allows the edition', function () {
583 expect(addListener).to.not.throw();
584 });
585 });
586
587 context('when registering an object', function () {
588 const registerStore = () => {
589 registry.registerStore('test', 'testing');
590 };
591
592 it('allows the registration', function () {
593 expect(registerStore).to.not.throw();
594 });
595 });
596
597 context('when deregistering an object', function () {
598 beforeEach(function () {
599 registry.registerStore('test', 'testing');
600 });
601
602 const deregisterStore = () => {
603 registry.registerStore('test');
604 };
605
606 it('allows the registration', function () {
607 expect(deregisterStore).to.not.throw();
608 });
609 });
610
611 context('when modifying the object', function () {
612 const modify = () => {
613 registry.emit = () => {
614 /* ignore */
615 };
616 };
617
618 it('raises an error', function () {
619 expect(modify).to.throw();
620 });
621 });
622 });
623});