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 _Flux$Store$Actions = require('../Flux');
10
11
var _sinon = require('sinon');
12
13
var sinon = _interopRequire(_sinon);
14
15
function createSerializableStore(serializedState) {
16
return (function (_Store) {
17
function SerializableStore() {
18
_classCallCheck(this, SerializableStore);
19
20
if (_Store != null) {
21
_Store.apply(this, arguments);
22
}
23
}
24
25
_inherits(SerializableStore, _Store);
26
27
SerializableStore.serialize = function serialize() {
28
return serializedState;
29
};
30
31
SerializableStore.deserialize = function deserialize(stateString) {
32
return {
33
stateString: stateString,
34
deserialized: true
35
};
36
};
37
38
return SerializableStore;
39
})(_Flux$Store$Actions.Store);
40
}
41
42
describe('Flux', function () {
43
44
describe('#createStore()', function () {
45
it('throws if key already exists', function () {
46
var flux = new _Flux$Store$Actions.Flux();
47
48
var TestStore = (function (_Store2) {
49
function TestStore() {
50
_classCallCheck(this, TestStore);
51
52
if (_Store2 != null) {
53
_Store2.apply(this, arguments);
54
}
55
}
56
57
_inherits(TestStore, _Store2);
58
59
return TestStore;
60
})(_Flux$Store$Actions.Store);
61
62
flux.createStore('ExampleStore', TestStore);
63
expect(flux.createStore.bind(flux, 'ExampleStore', TestStore)).to['throw']('You\'ve attempted to create multiple stores with key ExampleStore. ' + 'Keys must be unique.');
64
});
65
66
it('throws if Store is not a prototype of class', function () {
67
var flux = new _Flux$Store$Actions.Flux();
68
69
var ForgotToExtendStore = function ForgotToExtendStore() {
70
_classCallCheck(this, ForgotToExtendStore);
71
};
72
73
expect(flux.createStore.bind(flux, 'Flux', ForgotToExtendStore)).to['throw']('You\'ve attempted to create a store from the class ' + 'ForgotToExtendStore, which does not have the base Store class in its ' + 'prototype chain. Make sure you\'re using the `extends` keyword: ' + '`class ForgotToExtendStore extends Store { ... }`');
74
});
75
76
it('registers store\'s handler with central dispatcher', function () {
77
var ExampleStore = (function (_Store3) {
78
function ExampleStore() {
79
_classCallCheck(this, ExampleStore);
80
81
if (_Store3 != null) {
82
_Store3.apply(this, arguments);
83
}
84
}
85
86
_inherits(ExampleStore, _Store3);
87
88
return ExampleStore;
89
})(_Flux$Store$Actions.Store);
90
91
var spy1 = sinon.spy();
92
var spy2 = sinon.spy();
93
94
ExampleStore.prototype.foo = 'bar';
95
ExampleStore.prototype.handler = function (_payload) {
96
spy1(_payload);
97
spy2(this.foo);
98
};
99
100
var flux = new _Flux$Store$Actions.Flux();
101
flux.createStore('ExampleStore', ExampleStore);
102
103
var payload = 'foobar';
104
flux.dispatch('actionId', payload);
105
expect(spy1.getCall(0).args[0].body).to.equal('foobar');
106
expect(spy2.calledWith('bar')).to.be['true'];
107
});
108
109
it('returns the created store instance', function () {
110
var ExampleStore = (function (_Store4) {
111
function ExampleStore() {
112
_classCallCheck(this, ExampleStore);
113
114
if (_Store4 != null) {
115
_Store4.apply(this, arguments);
116
}
117
}
118
119
_inherits(ExampleStore, _Store4);
120
121
return ExampleStore;
122
})(_Flux$Store$Actions.Store);
123
124
var flux = new _Flux$Store$Actions.Flux();
125
var store = flux.createStore('ExampleStore', ExampleStore);
126
expect(store).to.be.an.instanceOf(ExampleStore);
127
});
128
});
129
130
describe('#getStore()', function () {
131
it('retrieves store for key', function () {
132
var flux = new _Flux$Store$Actions.Flux();
133
134
var TestStore = (function (_Store5) {
135
function TestStore() {
136
_classCallCheck(this, TestStore);
137
138
if (_Store5 != null) {
139
_Store5.apply(this, arguments);
140
}
141
}
142
143
_inherits(TestStore, _Store5);
144
145
return TestStore;
146
})(_Flux$Store$Actions.Store);
147
148
flux.createStore('ExampleStore', TestStore);
149
expect(flux.getStore('ExampleStore')).to.be.an.instanceOf(_Flux$Store$Actions.Store);
150
expect(flux.getStore('NonexistentStore')).to.be.undefined;
151
});
152
});
153
154
describe('#removeStore()', function () {
155
it('throws if key does not exist', function () {
156
var flux = new _Flux$Store$Actions.Flux();
157
158
var TestStore = (function (_Store6) {
159
function TestStore() {
160
_classCallCheck(this, TestStore);
161
162
if (_Store6 != null) {
163
_Store6.apply(this, arguments);
164
}
165
}
166
167
_inherits(TestStore, _Store6);
168
169
return TestStore;
170
})(_Flux$Store$Actions.Store);
171
172
flux.createStore('ExampleStore', TestStore);
173
expect(flux.removeStore.bind(flux, 'NonexistentStore')).to['throw']('You\'ve attempted to remove store with key NonexistentStore which does not exist.');
174
});
175
176
it('deletes store instance', function () {
177
var flux = new _Flux$Store$Actions.Flux();
178
179
var TestStore = (function (_Store7) {
180
function TestStore() {
181
_classCallCheck(this, TestStore);
182
183
if (_Store7 != null) {
184
_Store7.apply(this, arguments);
185
}
186
}
187
188
_inherits(TestStore, _Store7);
189
190
return TestStore;
191
})(_Flux$Store$Actions.Store);
192
193
var store = flux.createStore('ExampleStore', TestStore);
194
expect(flux.dispatcher.$Dispatcher_callbacks[store._token]).to.be['function'];
195
flux.removeStore('ExampleStore');
196
expect(flux._stores.ExampleStore).to.be.undefined;
197
expect(flux.dispatcher.$Dispatcher_callbacks[store._token]).to.be.undefined;
198
});
199
});
200
201
describe('#createActions()', function () {
202
it('throws if key already exists', function () {
203
var TestActions = (function (_Actions) {
204
function TestActions() {
205
_classCallCheck(this, TestActions);
206
207
if (_Actions != null) {
208
_Actions.apply(this, arguments);
209
}
210
}
211
212
_inherits(TestActions, _Actions);
213
214
return TestActions;
215
})(_Flux$Store$Actions.Actions);
216
217
var flux = new _Flux$Store$Actions.Flux();
218
flux.createActions('ExampleActions', TestActions);
219
220
expect(flux.createActions.bind(flux, 'ExampleActions', _Flux$Store$Actions.Actions)).to['throw']('You\'ve attempted to create multiple actions with key ExampleActions. ' + 'Keys must be unique.');
221
});
222
223
it('throws if Actions is a class without base Actions class in its prototype chain', function () {
224
var flux = new _Flux$Store$Actions.Flux();
225
226
var ForgotToExtendActions = function ForgotToExtendActions() {
227
_classCallCheck(this, ForgotToExtendActions);
228
};
229
230
expect(flux.createActions.bind(flux, 'Flux', ForgotToExtendActions)).to['throw']('You\'ve attempted to create actions from the class ' + 'ForgotToExtendActions, which does not have the base Actions class ' + 'in its prototype chain. Make sure you\'re using the `extends` ' + 'keyword: `class ForgotToExtendActions extends Actions { ... }`');
231
});
232
233
it('accepts plain old JavaScript object', function () {
234
var flux = new _Flux$Store$Actions.Flux();
235
236
flux.createActions('foobar', {
237
foo: function foo() {
238
return 'bar';
239
},
240
241
bar: function bar() {
242
return 'baz';
243
}
244
});
245
246
expect(flux.getActions('foobar')).to.be.an['instanceof'](_Flux$Store$Actions.Actions);
247
expect(flux.getActions('foobar').foo()).to.equal('bar');
248
expect(flux.getActions('foobar').bar()).to.equal('baz');
249
});
250
251
it('returns the created action\'s instance', function () {
252
var TestActions = (function (_Actions2) {
253
function TestActions() {
254
_classCallCheck(this, TestActions);
255
256
if (_Actions2 != null) {
257
_Actions2.apply(this, arguments);
258
}
259
}
260
261
_inherits(TestActions, _Actions2);
262
263
return TestActions;
264
})(_Flux$Store$Actions.Actions);
265
266
var flux = new _Flux$Store$Actions.Flux();
267
var actions = flux.createActions('TestActions', TestActions);
268
expect(actions).to.be.an.instanceOf(TestActions);
269
});
270
});
271
272
describe('#getActions()', function () {
273
var TestActions = (function (_Actions3) {
274
function TestActions() {
275
_classCallCheck(this, TestActions);
276
277
if (_Actions3 != null) {
278
_Actions3.apply(this, arguments);
279
}
280
}
281
282
_inherits(TestActions, _Actions3);
283
284
return TestActions;
285
})(_Flux$Store$Actions.Actions);
286
287
it('retrieves actions for key', function () {
288
var flux = new _Flux$Store$Actions.Flux();
289
flux.createActions('TestActions', TestActions);
290
291
expect(flux.getActions('TestActions')).to.be.an.instanceOf(_Flux$Store$Actions.Actions);
292
expect(flux.getActions('NonexistentActions')).to.be.undefined;
293
});
294
});
295
296
describe('#getActionIds() / #getConstants()', function () {
297
var TestActions = (function (_Actions4) {
298
function TestActions() {
299
_classCallCheck(this, TestActions);
300
301
if (_Actions4 != null) {
302
_Actions4.apply(this, arguments);
303
}
304
}
305
306
_inherits(TestActions, _Actions4);
307
308
TestActions.prototype.getFoo = function getFoo() {};
309
310
return TestActions;
311
})(_Flux$Store$Actions.Actions);
312
313
it('retrives ids of actions for key', function () {
314
var flux = new _Flux$Store$Actions.Flux();
315
flux.createActions('TestActions', TestActions);
316
317
expect(flux.getActionIds('TestActions').getFoo).to.be.a('string');
318
expect(flux.getActionIds('NonexistentActions')).to.be.undefined;
319
320
expect(flux.getConstants('TestActions').getFoo).to.be.a('string');
321
expect(flux.getConstants('NonexistentActions')).to.be.undefined;
322
});
323
});
324
325
describe('#removeActions()', function () {
326
it('throws if key does not exist', function () {
327
var flux = new _Flux$Store$Actions.Flux();
328
329
var TestActions = (function (_Actions5) {
330
function TestActions() {
331
_classCallCheck(this, TestActions);
332
333
if (_Actions5 != null) {
334
_Actions5.apply(this, arguments);
335
}
336
}
337
338
_inherits(TestActions, _Actions5);
339
340
return TestActions;
341
})(_Flux$Store$Actions.Actions);
342
343
flux.createActions('TestActions', TestActions);
344
expect(flux.removeActions.bind(flux, 'NonexistentActions')).to['throw']('You\'ve attempted to remove actions with key NonexistentActions which does not exist.');
345
});
346
347
it('deletes actions instance', function () {
348
var flux = new _Flux$Store$Actions.Flux();
349
350
var TestActions = (function (_Store8) {
351
function TestActions() {
352
_classCallCheck(this, TestActions);
353
354
if (_Store8 != null) {
355
_Store8.apply(this, arguments);
356
}
357
}
358
359
_inherits(TestActions, _Store8);
360
361
return TestActions;
362
})(_Flux$Store$Actions.Store);
363
364
flux.createStore('TestActions', TestActions);
365
flux.removeStore('TestActions');
366
expect(flux._actions.TestActions).to.be.undefined;
367
});
368
});
369
370
describe('#getAllActionIds() / #getAllConstants()', function () {
371
var TestFooActions = (function (_Actions6) {
372
function TestFooActions() {
373
_classCallCheck(this, TestFooActions);
374
375
if (_Actions6 != null) {
376
_Actions6.apply(this, arguments);
377
}
378
}
379
380
_inherits(TestFooActions, _Actions6);
381
382
TestFooActions.prototype.getFoo = function getFoo() {};
383
384
TestFooActions.prototype.getBar = function getBar() {};
385
386
return TestFooActions;
387
})(_Flux$Store$Actions.Actions);
388
389
var TestBarActions = (function (_Actions7) {
390
function TestBarActions() {
391
_classCallCheck(this, TestBarActions);
392
393
if (_Actions7 != null) {
394
_Actions7.apply(this, arguments);
395
}
396
}
397
398
_inherits(TestBarActions, _Actions7);
399
400
TestBarActions.prototype.getFoo = function getFoo() {};
401
402
TestBarActions.prototype.getBar = function getBar() {};
403
404
return TestBarActions;
405
})(_Flux$Store$Actions.Actions);
406
407
it('retrives ids of all actions', function () {
408
var flux = new _Flux$Store$Actions.Flux();
409
flux.createActions('TestFooActions', TestFooActions);
410
flux.createActions('TestBarActions', TestBarActions);
411
412
expect(flux.getAllActionIds()).to.be.an('array');
413
expect(flux.getAllActionIds()[0]).to.be.a('string');
414
expect(flux.getAllActionIds()).to.have.length(4);
415
416
expect(flux.getAllConstants()).to.be.an('array');
417
expect(flux.getAllConstants()[0]).to.be.a('string');
418
expect(flux.getAllConstants()).to.have.length(4);
419
});
420
});
421
422
describe('#dispatch()', function () {
423
424
it('delegates to dispatcher', function () {
425
var flux = new _Flux$Store$Actions.Flux();
426
var dispatch = sinon.spy();
427
flux.dispatcher = { dispatch: dispatch };
428
var actionId = 'actionId';
429
430
flux.dispatch(actionId, 'foobar');
431
432
expect(dispatch.firstCall.args[0]).to.deep.equal({
433
actionId: actionId,
434
body: 'foobar'
435
});
436
});
437
438
it('emits dispatch event', function () {
439
var flux = new _Flux$Store$Actions.Flux();
440
var listener = sinon.spy();
441
442
flux.addListener('dispatch', listener);
443
444
var actionId = 'actionId';
445
446
flux.dispatch(actionId, 'foobar');
447
448
expect(listener.calledOnce).to.be['true'];
449
expect(listener.firstCall.args[0]).to.deep.equal({
450
actionId: actionId,
451
body: 'foobar'
452
});
453
});
454
});
455
456
describe('#dispatchAsync()', function () {
457
458
it('delegates to dispatcher', function callee$2$0() {
459
var flux, dispatch, actionId;
460
return regeneratorRuntime.async(function callee$2$0$(context$3$0) {
461
while (1) switch (context$3$0.prev = context$3$0.next) {
462
case 0:
463
flux = new _Flux$Store$Actions.Flux();
464
dispatch = sinon.spy();
465
466
flux.dispatcher = { dispatch: dispatch };
467
actionId = 'actionId';
468
context$3$0.next = 6;
469
return flux.dispatchAsync(actionId, Promise.resolve('foobar'));
470
471
case 6:
472
473
expect(dispatch.callCount).to.equal(2);
474
expect(dispatch.firstCall.args[0]).to.deep.equal({
475
actionId: actionId,
476
async: 'begin'
477
});
478
expect(dispatch.secondCall.args[0]).to.deep.equal({
479
actionId: actionId,
480
body: 'foobar',
481
async: 'success'
482
});
483
484
case 9:
485
case 'end':
486
return context$3$0.stop();
487
}
488
}, null, this);
489
});
490
491
it('emits dispatch event', function callee$2$1() {
492
var flux, listener, actionId;
493
return regeneratorRuntime.async(function callee$2$1$(context$3$0) {
494
while (1) switch (context$3$0.prev = context$3$0.next) {
495
case 0:
496
flux = new _Flux$Store$Actions.Flux();
497
listener = sinon.spy();
498
499
flux.addListener('dispatch', listener);
500
501
actionId = 'actionId';
502
context$3$0.next = 6;
503
return flux.dispatchAsync(actionId, Promise.resolve('foobar'));
504
505
case 6:
506
507
expect(listener.calledTwice).to.be['true'];
508
expect(listener.firstCall.args[0]).to.deep.equal({
509
actionId: actionId,
510
async: 'begin'
511
});
512
expect(listener.secondCall.args[0]).to.deep.equal({
513
actionId: actionId,
514
async: 'success',
515
body: 'foobar'
516
});
517
518
case 9:
519
case 'end':
520
return context$3$0.stop();
521
}
522
}, null, this);
523
});
524
525
it('resolves to value of given promise', function (done) {
526
var flux = new _Flux$Store$Actions.Flux();
527
var dispatch = sinon.spy();
528
flux.dispatcher = { dispatch: dispatch };
529
var actionId = 'actionId';
530
531
expect(flux.dispatchAsync(actionId, Promise.resolve('foobar'))).to.eventually.equal('foobar').notify(done);
532
});
533
534
it('dispatches with error if promise rejects', function callee$2$2() {
535
var flux, dispatch, actionId, error;
536
return regeneratorRuntime.async(function callee$2$2$(context$3$0) {
537
while (1) switch (context$3$0.prev = context$3$0.next) {
538
case 0:
539
flux = new _Flux$Store$Actions.Flux();
540
dispatch = sinon.spy();
541
542
flux.dispatcher = { dispatch: dispatch };
543
actionId = 'actionId';
544
error = new Error('error');
545
context$3$0.next = 7;
546
return flux.dispatchAsync(actionId, Promise.reject(error));
547
548
case 7:
549
550
expect(dispatch.callCount).to.equal(2);
551
expect(dispatch.firstCall.args[0]).to.deep.equal({
552
actionId: actionId,
553
async: 'begin'
554
});
555
expect(dispatch.secondCall.args[0]).to.deep.equal({
556
actionId: actionId,
557
error: error,
558
async: 'failure'
559
});
560
561
case 10:
562
case 'end':
563
return context$3$0.stop();
564
}
565
}, null, this);
566
});
567
568
it('emit errors that occur as result of dispatch', function callee$2$3() {
569
var ExampleStore, flux, listener, actionId, store;
570
return regeneratorRuntime.async(function callee$2$3$(context$3$0) {
571
while (1) switch (context$3$0.prev = context$3$0.next) {
572
case 0:
573
ExampleStore = (function (_Store9) {
574
function ExampleStore() {
575
_classCallCheck(this, ExampleStore);
576
577
if (_Store9 != null) {
578
_Store9.apply(this, arguments);
579
}
580
}
581
582
_inherits(ExampleStore, _Store9);
583
584
return ExampleStore;
585
})(_Flux$Store$Actions.Store);
586
587
flux = new _Flux$Store$Actions.Flux();
588
listener = sinon.spy();
589
590
flux.addListener('error', listener);
591
592
actionId = 'actionId';
593
store = flux.createStore('example', ExampleStore);
594
595
store.registerAsync(actionId, null, function () {
596
throw new Error('success error');
597
}, function () {
598
throw new Error('failure error');
599
});
600
601
context$3$0.next = 9;
602
return expect(flux.dispatchAsync(actionId, Promise.resolve('foobar'))).to.be.rejectedWith('success error');
603
604
case 9:
605
expect(listener.calledOnce).to.be['true'];
606
expect(listener.firstCall.args[0].message).to.equal('success error');
607
608
context$3$0.next = 13;
609
return expect(flux.dispatchAsync(actionId, Promise.reject(new Error('foobar')))).to.be.rejectedWith('failure error');
610
611
case 13:
612
expect(listener.calledTwice).to.be['true'];
613
expect(listener.secondCall.args[0].message).to.equal('failure error');
614
615
case 15:
616
case 'end':
617
return context$3$0.stop();
618
}
619
}, null, this);
620
});
621
});
622
623
describe('#removeAllStoreListeners', function () {
624
it('removes all listeners from stores', function () {
625
var TestStore = (function (_Store10) {
626
function TestStore() {
627
_classCallCheck(this, TestStore);
628
629
if (_Store10 != null) {
630
_Store10.apply(this, arguments);
631
}
632
}
633
634
_inherits(TestStore, _Store10);
635
636
return TestStore;
637
})(_Flux$Store$Actions.Store);
638
639
var flux = new _Flux$Store$Actions.Flux();
640
var storeA = flux.createStore('storeA', TestStore);
641
var storeB = flux.createStore('storeB', TestStore);
642
643
var listener = function listener() {};
644
645
storeA.addListener('change', listener);
646
storeA.addListener('change', listener);
647
storeB.addListener('change', listener);
648
storeB.addListener('change', listener);
649
650
expect(storeA.listeners('change').length).to.equal(2);
651
expect(storeB.listeners('change').length).to.equal(2);
652
653
flux.removeAllStoreListeners();
654
655
expect(storeA.listeners('change').length).to.equal(0);
656
expect(storeB.listeners('change').length).to.equal(0);
657
});
658
});
659
660
describe('#serialize()', function () {
661
662
it('returns state of all the stores as a JSON string', function () {
663
var flux = new _Flux$Store$Actions.Flux();
664
665
flux.createStore('foo', createSerializableStore('foo state'));
666
flux.createStore('bar', createSerializableStore('bar state'));
667
flux.createStore('baz', createSerializableStore('baz state'));
668
669
expect(JSON.parse(flux.serialize())).to.deep.equal({
670
foo: 'foo state',
671
bar: 'bar state',
672
baz: 'baz state'
673
});
674
});
675
676
it('ignores stores whose classes do not implement .serialize()', function () {
677
var flux = new _Flux$Store$Actions.Flux();
678
679
var TestStore = (function (_Store11) {
680
function TestStore() {
681
_classCallCheck(this, TestStore);
682
683
if (_Store11 != null) {
684
_Store11.apply(this, arguments);
685
}
686
}
687
688
_inherits(TestStore, _Store11);
689
690
return TestStore;
691
})(_Flux$Store$Actions.Store);
692
693
flux.createStore('foo', createSerializableStore('foo state'));
694
flux.createStore('bar', createSerializableStore('bar state'));
695
flux.createStore('baz', TestStore);
696
697
expect(JSON.parse(flux.serialize())).to.deep.equal({
698
foo: 'foo state',
699
bar: 'bar state'
700
});
701
});
702
703
it('warns if any store classes .serialize() returns a non-string', function () {
704
var flux = new _Flux$Store$Actions.Flux();
705
var warn = sinon.spy(console, 'warn');
706
707
flux.createStore('foo', createSerializableStore({}));
708
flux.serialize();
709
710
expect(warn.firstCall.args[0]).to.equal('The store with key \'foo\' was not serialized because the static ' + 'method `SerializableStore.serialize()` returned a non-string with ' + 'type \'object\'.');
711
712
console.warn.restore();
713
});
714
715
it('warns and skips stores whose classes do not implement .deserialize()', function () {
716
var flux = new _Flux$Store$Actions.Flux();
717
var warn = sinon.spy(console, 'warn');
718
719
var TestStore = (function (_Store12) {
720
function TestStore() {
721
_classCallCheck(this, TestStore);
722
723
if (_Store12 != null) {
724
_Store12.apply(this, arguments);
725
}
726
}
727
728
_inherits(TestStore, _Store12);
729
730
TestStore.serialize = function serialize() {
731
return 'state string';
732
};
733
734
return TestStore;
735
})(_Flux$Store$Actions.Store);
736
737
flux.createStore('test', TestStore);
738
flux.serialize();
739
740
expect(warn.firstCall.args[0]).to.equal('The class `TestStore` has a `serialize()` method, but no ' + 'corresponding `deserialize()` method.');
741
742
console.warn.restore();
743
});
744
});
745
746
describe('#deserialize()', function () {
747
748
it('converts a serialized string into state and uses it to replace state of stores', function () {
749
var flux = new _Flux$Store$Actions.Flux();
750
751
flux.createStore('foo', createSerializableStore());
752
flux.createStore('bar', createSerializableStore());
753
flux.createStore('baz', createSerializableStore());
754
755
flux.deserialize('{\n "foo": "foo state",\n "bar": "bar state",\n "baz": "baz state"\n }');
756
757
var fooStore = flux.getStore('foo');
758
var barStore = flux.getStore('bar');
759
var bazStore = flux.getStore('baz');
760
761
expect(fooStore.state.stateString).to.equal('foo state');
762
expect(fooStore.state.deserialized).to.be['true'];
763
expect(barStore.state.stateString).to.equal('bar state');
764
expect(barStore.state.deserialized).to.be['true'];
765
expect(bazStore.state.stateString).to.equal('baz state');
766
expect(bazStore.state.deserialized).to.be['true'];
767
});
768
769
it('warns and skips if passed string is invalid JSON', function () {
770
var flux = new _Flux$Store$Actions.Flux();
771
772
var TestStore = (function (_Store13) {
773
function TestStore() {
774
_classCallCheck(this, TestStore);
775
776
if (_Store13 != null) {
777
_Store13.apply(this, arguments);
778
}
779
}
780
781
_inherits(TestStore, _Store13);
782
783
return TestStore;
784
})(_Flux$Store$Actions.Store);
785
786
flux.createStore('foo', TestStore);
787
788
expect(flux.deserialize.bind(flux, 'not JSON')).to['throw']('Invalid value passed to `Flux#deserialize()`: not JSON');
789
});
790
791
it('warns and skips stores whose classes do not implement .serialize()', function () {
792
var flux = new _Flux$Store$Actions.Flux();
793
var warn = sinon.spy(console, 'warn');
794
795
var TestStore = (function (_Store14) {
796
function TestStore() {
797
_classCallCheck(this, TestStore);
798
799
if (_Store14 != null) {
800
_Store14.apply(this, arguments);
801
}
802
}
803
804
_inherits(TestStore, _Store14);
805
806
TestStore.deserialize = function deserialize() {
807
return {};
808
};
809
810
return TestStore;
811
})(_Flux$Store$Actions.Store);
812
813
flux.createStore('test', TestStore);
814
flux.deserialize('{"test": "test state"}');
815
816
expect(warn.firstCall.args[0]).to.equal('The class `TestStore` has a `deserialize()` method, but no ' + 'corresponding `serialize()` method.');
817
818
console.warn.restore();
819
});
820
821
it('ignores stores whose classes do not implement .deserialize()', function () {
822
var flux = new _Flux$Store$Actions.Flux();
823
824
var TestStore = (function (_Store15) {
825
function TestStore() {
826
_classCallCheck(this, TestStore);
827
828
if (_Store15 != null) {
829
_Store15.apply(this, arguments);
830
}
831
}
832
833
_inherits(TestStore, _Store15);
834
835
return TestStore;
836
})(_Flux$Store$Actions.Store);
837
838
flux.createStore('foo', createSerializableStore());
839
flux.createStore('bar', createSerializableStore());
840
flux.createStore('baz', TestStore);
841
842
flux.deserialize('{\n "foo": "foo state",\n "bar": "bar state",\n "baz": "baz state"\n }');
843
844
var fooStore = flux.getStore('foo');
845
var barStore = flux.getStore('bar');
846
var bazStore = flux.getStore('baz');
847
848
expect(fooStore.state.stateString).to.equal('foo state');
849
expect(fooStore.state.deserialized).to.be['true'];
850
expect(barStore.state.stateString).to.equal('bar state');
851
expect(barStore.state.deserialized).to.be['true'];
852
expect(bazStore.state).to.be['null'];
853
});
854
});
855
});
856