Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80517 views
1
'use strict';
2
3
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj['default'] : obj; };
4
5
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } };
6
7
var _inherits = function (subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; };
8
9
var _Store$Flux$Actions = require('../Flux');
10
11
var _sinon = require('sinon');
12
13
var sinon = _interopRequire(_sinon);
14
15
describe('Store', function () {
16
var ExampleStore = (function (_Store) {
17
function ExampleStore() {
18
_classCallCheck(this, ExampleStore);
19
20
_Store.call(this);
21
this.state = { foo: 'bar' };
22
}
23
24
_inherits(ExampleStore, _Store);
25
26
return ExampleStore;
27
})(_Store$Flux$Actions.Store);
28
29
var actionId = 'actionId';
30
31
describe('#register()', function () {
32
it('adds handler to internal collection of handlers', function () {
33
var _store$_handlers;
34
35
var store = new ExampleStore();
36
var handler = sinon.spy();
37
store.register(actionId, handler);
38
39
var mockArgs = ['foo', 'bar'];
40
(_store$_handlers = store._handlers)[actionId].apply(_store$_handlers, mockArgs);
41
42
expect(handler.calledWith.apply(handler, mockArgs)).to.be['true'];
43
});
44
45
it('binds handler to store', function () {
46
var store = new ExampleStore();
47
store.foo = 'bar';
48
49
function handler() {
50
return this.foo;
51
}
52
53
store.register(actionId, handler);
54
55
expect(store._handlers[actionId]()).to.equal('bar');
56
});
57
58
it('accepts actions instead of action ids', function () {
59
var _store$_handlers2;
60
61
var ExampleActions = (function (_Actions) {
62
function ExampleActions() {
63
_classCallCheck(this, ExampleActions);
64
65
if (_Actions != null) {
66
_Actions.apply(this, arguments);
67
}
68
}
69
70
_inherits(ExampleActions, _Actions);
71
72
ExampleActions.prototype.getFoo = function getFoo() {
73
return 'foo';
74
};
75
76
return ExampleActions;
77
})(_Store$Flux$Actions.Actions);
78
79
var actions = new ExampleActions();
80
var store = new ExampleStore();
81
var handler = sinon.spy();
82
store.register(actions.getFoo, handler);
83
84
var mockArgs = ['foo', 'bar'];
85
(_store$_handlers2 = store._handlers)[actions.getFoo._id].apply(_store$_handlers2, mockArgs);
86
87
expect(handler.calledWith.apply(handler, mockArgs)).to.be['true'];
88
});
89
90
it('ignores non-function handlers', function () {
91
var store = new ExampleStore();
92
expect(store.register.bind(store, null)).not.to['throw']();
93
});
94
});
95
96
it('default state is null', function () {
97
var store = new _Store$Flux$Actions.Store();
98
expect(store.state).to.be['null'];
99
});
100
101
describe('#registerAsync()', function () {
102
it('registers handlers for begin, success, and failure of async action', function callee$2$0() {
103
var error, ExampleActions, ExampleFlux, flux, actions, store, handler, begin, success, failure;
104
return regeneratorRuntime.async(function callee$2$0$(context$3$0) {
105
while (1) switch (context$3$0.prev = context$3$0.next) {
106
case 0:
107
error = new Error();
108
109
ExampleActions = (function (_Actions2) {
110
function ExampleActions() {
111
_classCallCheck(this, ExampleActions);
112
113
if (_Actions2 != null) {
114
_Actions2.apply(this, arguments);
115
}
116
}
117
118
_inherits(ExampleActions, _Actions2);
119
120
ExampleActions.prototype.getFoo = function getFoo(message) {
121
var _success = arguments[1] === undefined ? true : arguments[1];
122
123
return regeneratorRuntime.async(function getFoo$(context$5$0) {
124
while (1) switch (context$5$0.prev = context$5$0.next) {
125
case 0:
126
if (_success) {
127
context$5$0.next = 2;
128
break;
129
}
130
131
throw error;
132
133
case 2:
134
return context$5$0.abrupt('return', message + ' success');
135
136
case 3:
137
case 'end':
138
return context$5$0.stop();
139
}
140
}, null, this);
141
};
142
143
ExampleActions.prototype.getBar = function getBar(message) {
144
return regeneratorRuntime.async(function getBar$(context$5$0) {
145
while (1) switch (context$5$0.prev = context$5$0.next) {
146
case 0:
147
return context$5$0.abrupt('return', message);
148
149
case 1:
150
case 'end':
151
return context$5$0.stop();
152
}
153
}, null, this);
154
};
155
156
return ExampleActions;
157
})(_Store$Flux$Actions.Actions);
158
159
ExampleFlux = (function (_Flux) {
160
function ExampleFlux() {
161
_classCallCheck(this, ExampleFlux);
162
163
_Flux.call(this);
164
this.createActions('example', ExampleActions);
165
this.createStore('example', ExampleStore);
166
}
167
168
_inherits(ExampleFlux, _Flux);
169
170
return ExampleFlux;
171
})(_Store$Flux$Actions.Flux);
172
173
flux = new ExampleFlux();
174
actions = flux.getActions('example');
175
store = flux.getStore('example');
176
handler = sinon.spy();
177
178
store.register(actions.getBar, handler);
179
180
context$3$0.next = 10;
181
return actions.getBar('bar');
182
183
case 10:
184
expect(handler.calledOnce).to.be['true'];
185
expect(handler.firstCall.args).to.deep.equal(['bar']);
186
187
begin = sinon.spy();
188
success = sinon.spy();
189
failure = sinon.spy();
190
191
store.registerAsync(actions.getFoo, begin, success, failure);
192
193
context$3$0.next = 18;
194
return actions.getFoo('foo', true);
195
196
case 18:
197
expect(begin.calledOnce).to.be['true'];
198
expect(begin.firstCall.args).to.deep.equal(['foo', true]);
199
expect(success.calledOnce).to.be['true'];
200
expect(success.firstCall.args[0]).to.equal('foo success');
201
expect(failure.called).to.be['false'];
202
203
context$3$0.next = 25;
204
return expect(actions.getFoo('bar', false)).to.be.rejected;
205
206
case 25:
207
208
expect(begin.calledTwice).to.be['true'];
209
expect(success.calledOnce).to.be['true'];
210
expect(failure.calledOnce).to.be['true'];
211
expect(failure.firstCall.args[0]).to.equal(error);
212
213
case 29:
214
case 'end':
215
return context$3$0.stop();
216
}
217
}, null, this);
218
});
219
220
it('ignores non-function handlers', function () {
221
var store = new ExampleStore();
222
expect(store.registerAsync.bind(store, null)).not.to['throw']();
223
});
224
});
225
226
describe('#registerAll()', function () {
227
it('adds handler to internal collection of "catch all" handlers', function () {
228
var _store$_catchAllHandlers;
229
230
var store = new ExampleStore();
231
var handler = sinon.spy();
232
store.registerAll(handler);
233
234
var mockArgs = ['foo', 'bar'];
235
(_store$_catchAllHandlers = store._catchAllHandlers)[0].apply(_store$_catchAllHandlers, mockArgs);
236
237
expect(handler.calledWith.apply(handler, mockArgs)).to.be['true'];
238
});
239
240
it('adds multiple handlers to internal collection of "catch all" handlers', function () {
241
var _store$_catchAllHandlers2, _store$_catchAllHandlers3;
242
243
var store = new ExampleStore();
244
var handler1 = sinon.spy();
245
var handler2 = sinon.spy();
246
store.registerAll(handler1);
247
store.registerAll(handler2);
248
249
var mockArgs = ['foo', 'bar'];
250
(_store$_catchAllHandlers2 = store._catchAllHandlers)[0].apply(_store$_catchAllHandlers2, mockArgs);
251
(_store$_catchAllHandlers3 = store._catchAllHandlers)[1].apply(_store$_catchAllHandlers3, mockArgs);
252
253
expect(handler1.calledWith.apply(handler1, mockArgs)).to.be['true'];
254
expect(handler2.calledWith.apply(handler2, mockArgs)).to.be['true'];
255
});
256
257
it('binds handler to store', function () {
258
var store = new ExampleStore();
259
store.foo = 'bar';
260
261
function handler() {
262
return this.foo;
263
}
264
265
store.registerAll(handler);
266
267
expect(store._catchAllHandlers[0]()).to.equal('bar');
268
});
269
270
it('accepts actions instead of action ids', function () {
271
var _store$_catchAllHandlers4;
272
273
var ExampleActions = (function (_Actions3) {
274
function ExampleActions() {
275
_classCallCheck(this, ExampleActions);
276
277
if (_Actions3 != null) {
278
_Actions3.apply(this, arguments);
279
}
280
}
281
282
_inherits(ExampleActions, _Actions3);
283
284
ExampleActions.prototype.getFoo = function getFoo() {
285
return 'foo';
286
};
287
288
return ExampleActions;
289
})(_Store$Flux$Actions.Actions);
290
291
var actions = new ExampleActions();
292
var store = new ExampleStore();
293
var handler = sinon.spy();
294
store.registerAll(handler);
295
296
var mockArgs = ['foo', 'bar'];
297
(_store$_catchAllHandlers4 = store._catchAllHandlers)[0].apply(_store$_catchAllHandlers4, mockArgs);
298
299
expect(handler.calledWith.apply(handler, mockArgs)).to.be['true'];
300
});
301
302
it('ignores non-function handlers', function () {
303
var store = new ExampleStore();
304
expect(store.registerAll.bind(store, null)).not.to['throw']();
305
});
306
307
it('registers for all async actions success', function callee$2$0() {
308
var error, ExampleActions, ExampleFlux, flux, actions, store, handler;
309
return regeneratorRuntime.async(function callee$2$0$(context$3$0) {
310
while (1) switch (context$3$0.prev = context$3$0.next) {
311
case 0:
312
error = new Error();
313
314
ExampleActions = (function (_Actions4) {
315
function ExampleActions() {
316
_classCallCheck(this, ExampleActions);
317
318
if (_Actions4 != null) {
319
_Actions4.apply(this, arguments);
320
}
321
}
322
323
_inherits(ExampleActions, _Actions4);
324
325
ExampleActions.prototype.getFoo = function getFoo(message) {
326
var _success = arguments[1] === undefined ? true : arguments[1];
327
328
return regeneratorRuntime.async(function getFoo$(context$5$0) {
329
while (1) switch (context$5$0.prev = context$5$0.next) {
330
case 0:
331
if (_success) {
332
context$5$0.next = 2;
333
break;
334
}
335
336
throw error;
337
338
case 2:
339
return context$5$0.abrupt('return', message + ' success');
340
341
case 3:
342
case 'end':
343
return context$5$0.stop();
344
}
345
}, null, this);
346
};
347
348
ExampleActions.prototype.getBar = function getBar(message) {
349
var _success = arguments[1] === undefined ? true : arguments[1];
350
351
return regeneratorRuntime.async(function getBar$(context$5$0) {
352
while (1) switch (context$5$0.prev = context$5$0.next) {
353
case 0:
354
if (_success) {
355
context$5$0.next = 2;
356
break;
357
}
358
359
throw error;
360
361
case 2:
362
return context$5$0.abrupt('return', message + ' success');
363
364
case 3:
365
case 'end':
366
return context$5$0.stop();
367
}
368
}, null, this);
369
};
370
371
return ExampleActions;
372
})(_Store$Flux$Actions.Actions);
373
374
ExampleFlux = (function (_Flux2) {
375
function ExampleFlux() {
376
_classCallCheck(this, ExampleFlux);
377
378
_Flux2.call(this);
379
this.createActions('example', ExampleActions);
380
this.createStore('example', ExampleStore);
381
}
382
383
_inherits(ExampleFlux, _Flux2);
384
385
return ExampleFlux;
386
})(_Store$Flux$Actions.Flux);
387
388
flux = new ExampleFlux();
389
actions = flux.getActions('example');
390
store = flux.getStore('example');
391
handler = sinon.spy();
392
393
store.registerAll(handler);
394
395
context$3$0.next = 10;
396
return actions.getBar('bar');
397
398
case 10:
399
expect(handler.calledOnce).to.be['true'];
400
expect(handler.firstCall.args).to.deep.equal(['bar success']);
401
402
case 12:
403
case 'end':
404
return context$3$0.stop();
405
}
406
}, null, this);
407
});
408
});
409
410
describe('#registerAllAsync()', function () {
411
it('registers "catch all" handlers for begin, success, and failure of async action', function callee$2$0() {
412
var error, ExampleActions, ExampleFlux, flux, actions, store, begin, success, failure;
413
return regeneratorRuntime.async(function callee$2$0$(context$3$0) {
414
while (1) switch (context$3$0.prev = context$3$0.next) {
415
case 0:
416
error = new Error();
417
418
ExampleActions = (function (_Actions5) {
419
function ExampleActions() {
420
_classCallCheck(this, ExampleActions);
421
422
if (_Actions5 != null) {
423
_Actions5.apply(this, arguments);
424
}
425
}
426
427
_inherits(ExampleActions, _Actions5);
428
429
ExampleActions.prototype.getFoo = function getFoo(message) {
430
var _success = arguments[1] === undefined ? true : arguments[1];
431
432
return regeneratorRuntime.async(function getFoo$(context$5$0) {
433
while (1) switch (context$5$0.prev = context$5$0.next) {
434
case 0:
435
if (_success) {
436
context$5$0.next = 2;
437
break;
438
}
439
440
throw error;
441
442
case 2:
443
return context$5$0.abrupt('return', message + ' success');
444
445
case 3:
446
case 'end':
447
return context$5$0.stop();
448
}
449
}, null, this);
450
};
451
452
ExampleActions.prototype.getBar = function getBar(message) {
453
var _success = arguments[1] === undefined ? true : arguments[1];
454
455
return regeneratorRuntime.async(function getBar$(context$5$0) {
456
while (1) switch (context$5$0.prev = context$5$0.next) {
457
case 0:
458
if (_success) {
459
context$5$0.next = 2;
460
break;
461
}
462
463
throw error;
464
465
case 2:
466
return context$5$0.abrupt('return', message + ' success');
467
468
case 3:
469
case 'end':
470
return context$5$0.stop();
471
}
472
}, null, this);
473
};
474
475
return ExampleActions;
476
})(_Store$Flux$Actions.Actions);
477
478
ExampleFlux = (function (_Flux3) {
479
function ExampleFlux() {
480
_classCallCheck(this, ExampleFlux);
481
482
_Flux3.call(this);
483
this.createActions('example', ExampleActions);
484
this.createStore('example', ExampleStore);
485
}
486
487
_inherits(ExampleFlux, _Flux3);
488
489
return ExampleFlux;
490
})(_Store$Flux$Actions.Flux);
491
492
flux = new ExampleFlux();
493
actions = flux.getActions('example');
494
store = flux.getStore('example');
495
begin = sinon.spy();
496
success = sinon.spy();
497
failure = sinon.spy();
498
499
store.registerAllAsync(begin, success, failure);
500
501
context$3$0.next = 12;
502
return actions.getFoo('foo', true);
503
504
case 12:
505
expect(begin.calledOnce).to.be['true'];
506
expect(begin.firstCall.args).to.deep.equal(['foo', true]);
507
expect(success.calledOnce).to.be['true'];
508
expect(success.firstCall.args[0]).to.equal('foo success');
509
expect(failure.called).to.be['false'];
510
511
context$3$0.next = 19;
512
return expect(actions.getFoo('bar', false)).to.be.rejected;
513
514
case 19:
515
expect(begin.calledTwice).to.be['true'];
516
expect(success.calledOnce).to.be['true'];
517
expect(failure.calledOnce).to.be['true'];
518
expect(failure.firstCall.args[0]).to.equal(error);
519
520
context$3$0.next = 25;
521
return actions.getBar('foo', true);
522
523
case 25:
524
expect(begin.calledThrice).to.be['true'];
525
expect(begin.thirdCall.args).to.deep.equal(['foo', true]);
526
expect(success.calledTwice).to.be['true'];
527
expect(success.secondCall.args[0]).to.equal('foo success');
528
expect(failure.calledTwice).to.be['false'];
529
530
context$3$0.next = 32;
531
return expect(actions.getBar('bar', false)).to.be.rejected;
532
533
case 32:
534
expect(begin.callCount).to.equal(4);
535
expect(success.calledTwice).to.be['true'];
536
expect(failure.calledTwice).to.be['true'];
537
expect(failure.secondCall.args[0]).to.equal(error);
538
539
case 36:
540
case 'end':
541
return context$3$0.stop();
542
}
543
}, null, this);
544
});
545
546
it('ignores non-function handlers', function () {
547
var store = new ExampleStore();
548
expect(store.registerAsync.bind(store, null)).not.to['throw']();
549
});
550
});
551
552
describe('#handler()', function () {
553
it('delegates dispatches to registered handlers', function () {
554
var store = new ExampleStore();
555
var handler = sinon.spy();
556
store.register(actionId, handler);
557
558
// Simulate dispatch
559
var body = { foo: 'bar' };
560
store.handler({ body: body, actionId: actionId });
561
562
expect(handler.calledWith(body)).to.be['true'];
563
});
564
565
it('delegates dispatches to registered "catch all" handlers', function () {
566
var store = new ExampleStore();
567
var handler = sinon.spy();
568
var actionIds = ['actionId1', 'actionId2'];
569
store.registerAll(handler);
570
571
// Simulate dispatch
572
var body = { foo: 'bar' };
573
store.handler({ body: body, actionId: actionIds[0] });
574
store.handler({ body: body, actionId: actionIds[1] });
575
576
expect(handler.calledWith(body)).to.be['true'];
577
expect(handler.calledTwice).to.be['true'];
578
});
579
});
580
581
describe('#waitFor()', function () {
582
it('waits for other stores', function () {
583
var flux = new _Store$Flux$Actions.Flux();
584
var result = [];
585
586
var store2 = undefined;
587
588
var Store1 = (function (_Store2) {
589
function Store1() {
590
_classCallCheck(this, Store1);
591
592
_Store2.call(this);
593
594
this.register(actionId, function () {
595
this.waitFor(store2);
596
result.push(1);
597
});
598
}
599
600
_inherits(Store1, _Store2);
601
602
return Store1;
603
})(_Store$Flux$Actions.Store);
604
605
var Store2 = (function (_Store3) {
606
function Store2() {
607
_classCallCheck(this, Store2);
608
609
_Store3.call(this);
610
611
this.register(actionId, function () {
612
result.push(2);
613
});
614
}
615
616
_inherits(Store2, _Store3);
617
618
return Store2;
619
})(_Store$Flux$Actions.Store);
620
621
flux.createStore('store1', Store1);
622
flux.createStore('store2', Store2);
623
624
store2 = flux.getStore('store2');
625
626
flux.dispatch(actionId, 'foobar');
627
628
expect(result).to.deep.equal([2, 1]);
629
});
630
});
631
632
describe('#forceUpdate()', function () {
633
it('emits change event', function () {
634
var store = new ExampleStore();
635
var listener = sinon.spy();
636
store.addListener('change', listener);
637
638
store.forceUpdate();
639
640
expect(listener.calledOnce).to.be['true'];
641
});
642
643
it('doesn\'t modify existing state', function () {
644
var store = new ExampleStore();
645
var listener = sinon.spy();
646
store.addListener('change', listener);
647
648
store.register(actionId, function () {
649
this.replaceState({ bar: 'baz' });
650
this.forceUpdate();
651
652
expect(this.state).to.deep.equal({ foo: 'bar' });
653
expect(listener.called).to.be['false'];
654
655
this.setState({ foo: 'bar' });
656
this.forceUpdate();
657
this.replaceState({ baz: 'foo' });
658
});
659
660
// Simulate dispatch
661
store.handler({ actionId: actionId, body: 'foobar' });
662
663
expect(listener.calledOnce).to.be['true'];
664
expect(store.state).to.deep.equal({ baz: 'foo' });
665
});
666
});
667
668
describe('#setState()', function () {
669
it('shallow merges old state with new state', function () {
670
var store = new ExampleStore();
671
672
store.setState({ bar: 'baz' });
673
674
expect(store.state).to.deep.equal({
675
foo: 'bar',
676
bar: 'baz' });
677
});
678
679
it('supports transactional updates', function () {
680
var store = new _Store$Flux$Actions.Store();
681
store.state = { a: 1 };
682
store.setState(function (state) {
683
return { a: state.a + 1 };
684
});
685
expect(store.state.a).to.equal(2);
686
store.setState(function (state) {
687
return { a: state.a + 1 };
688
});
689
expect(store.state.a).to.equal(3);
690
store.setState(function (state) {
691
return { a: state.a + 1 };
692
});
693
expect(store.state.a).to.equal(4);
694
});
695
696
it('emits change event', function () {
697
var store = new ExampleStore();
698
var listener = sinon.spy();
699
store.addListener('change', listener);
700
701
store.setState({ foo: 'bar' });
702
703
expect(listener.calledOnce).to.be['true'];
704
});
705
706
it('batches multiple state updates within action handler', function () {
707
var store = new ExampleStore();
708
var listener = sinon.spy();
709
store.addListener('change', listener);
710
711
store.register(actionId, function () {
712
this.setState({ bar: 'baz' });
713
714
expect(this.state).to.deep.equal({ foo: 'bar' });
715
expect(listener.called).to.be['false'];
716
717
this.setState({ baz: 'foo' });
718
});
719
720
// Simulate dispatch
721
store.handler({ actionId: actionId, body: 'foobar' });
722
723
expect(listener.calledOnce).to.be['true'];
724
expect(store.state).to.deep.equal({ foo: 'bar', bar: 'baz', baz: 'foo' });
725
});
726
});
727
728
describe('#replaceState()', function () {
729
it('replaces old state with new state', function () {
730
var store = new ExampleStore();
731
732
store.replaceState({ bar: 'baz' });
733
734
expect(store.state).to.deep.equal({
735
bar: 'baz' });
736
});
737
738
it('batches multiple state updates within action handler', function () {
739
var store = new ExampleStore();
740
var listener = sinon.spy();
741
store.addListener('change', listener);
742
743
store.register(actionId, function () {
744
this.replaceState({ bar: 'baz' });
745
746
expect(this.state).to.deep.equal({ foo: 'bar' });
747
expect(listener.called).to.be['false'];
748
749
this.setState({ foo: 'bar' });
750
this.replaceState({ baz: 'foo' });
751
});
752
753
// Simulate dispatch
754
store.handler({ actionId: actionId, body: 'foobar' });
755
756
expect(listener.calledOnce).to.be['true'];
757
expect(store.state).to.deep.equal({ baz: 'foo' });
758
});
759
760
it('emits change event', function () {
761
var store = new ExampleStore();
762
var listener = sinon.spy();
763
store.addListener('change', listener);
764
765
store.replaceState({ foo: 'bar' });
766
767
expect(listener.calledOnce).to.be['true'];
768
});
769
});
770
771
describe('.assignState', function () {
772
it('can be overridden to enable custom state types', function () {
773
var StringStore = (function (_Store4) {
774
function StringStore() {
775
_classCallCheck(this, StringStore);
776
777
if (_Store4 != null) {
778
_Store4.apply(this, arguments);
779
}
780
}
781
782
_inherits(StringStore, _Store4);
783
784
StringStore.assignState = function assignState(prevState, nextState) {
785
return [prevState, nextState].filter(function (state) {
786
return typeof state === 'string';
787
}).join('');
788
};
789
790
return StringStore;
791
})(_Store$Flux$Actions.Store);
792
793
var store = new StringStore();
794
795
expect(store.state).to.be['null'];
796
store.setState('a');
797
expect(store.state).to.equal('a');
798
store.setState('b');
799
expect(store.state).to.equal('ab');
800
store.replaceState('xyz');
801
expect(store.state).to.equal('xyz');
802
store.setState('zyx');
803
expect(store.state).to.equal('xyzzyx');
804
});
805
});
806
807
describe('#getStateAsObject()', function () {
808
it('returns the current state as an object', function () {
809
var store = new _Store$Flux$Actions.Store();
810
store.setState({ foo: 'bar', bar: 'baz' });
811
expect(store.getStateAsObject()).to.deep.equal({ foo: 'bar', bar: 'baz' });
812
});
813
});
814
815
describe('#forceUpdate()', function () {
816
it('emits change event', function () {
817
var store = new ExampleStore();
818
var listener = sinon.spy();
819
store.addListener('change', listener);
820
821
store.forceUpdate();
822
823
expect(listener.calledOnce).to.be['true'];
824
});
825
826
it('doesn\'t modify existing state', function () {
827
var store = new ExampleStore();
828
var listener = sinon.spy();
829
store.addListener('change', listener);
830
831
store.register(actionId, function () {
832
this.replaceState({ bar: 'baz' });
833
this.forceUpdate();
834
835
expect(this.state).to.deep.equal({ foo: 'bar' });
836
expect(listener.called).to.be['false'];
837
838
this.setState({ foo: 'bar' });
839
this.forceUpdate();
840
this.replaceState({ baz: 'foo' });
841
});
842
843
// Simulate dispatch
844
store.handler({ actionId: actionId, body: 'foobar' });
845
846
expect(listener.calledOnce).to.be['true'];
847
expect(store.state).to.deep.equal({ baz: 'foo' });
848
});
849
});
850
});
851