/
createImmutableStore.js
132 lines (110 loc) · 4.69 KB
/
createImmutableStore.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
'use strict';
var expect = require('chai').expect;
var dispatcher = require('dispatchr');
var BasicStore = require('../fixtures/BasicStore');
var RegularStore = require('../fixtures/RegularStore');
var PrivatesStore = require('../fixtures/PrivatesStore');
var stores = [
BasicStore,
RegularStore,
PrivatesStore
];
beforeEach(function () {
this.dispatcher = dispatcher.createDispatcher({ stores: stores });
this.dispatcherContext = this.dispatcher.createContext({});
});
describe('A store defining its own accessor functions', function () {
it('should not have any accessors defined on its prototype', function () {
expect(BasicStore.prototype.accessors).to.equal(undefined);
});
it('should mutate its internal state in response to a dispatch', function () {
var store = this.dispatcherContext.getStore(BasicStore);
expect(store.number).to.equal(50);
this.dispatcherContext.dispatch('TRIGGER', 100);
expect(store.number).to.equal(100);
});
it('should not allow set access to that internal state', function () {
var store = this.dispatcherContext.getStore(BasicStore);
var num = store.number;
expect(
function () {
store.number = 250;
}).to.throw(TypeError);
expect(store.number).to.equal(num);
});
it('should not conflict with other instances of itself', function () {
var dispatcherA = dispatcher.createDispatcher({ stores: stores });
var dispatcherContextA = dispatcherA.createContext({});
var dispatcherB = dispatcher.createDispatcher({ stores: stores });
var dispatcherContextB = dispatcherB.createContext({});
var storeA = dispatcherContextA.getStore(BasicStore);
var storeB = dispatcherContextB.getStore(BasicStore);
expect(storeA.number).to.equal(50);
expect(storeB.number).to.equal(50);
dispatcherContextA.dispatch('TRIGGER', 100);
expect(storeA.number).to.equal(100);
expect(storeB.number).to.equal(50);
});
});
describe('A store not using accessors', function () {
it('should not have any accessors defined on its prototype', function () {
expect(RegularStore.prototype.accessors).to.equal(undefined);
});
it('should mutate its internal state in response to a dispatch', function () {
var store = this.dispatcherContext.getStore(RegularStore);
expect(store.number).to.equal(50);
this.dispatcherContext.dispatch('TRIGGER', 100);
expect(store.number).to.equal(100);
});
it('should allow set access to that internal state', function () {
var store = this.dispatcherContext.getStore(RegularStore);
store.number = 250;
expect(store.number).to.equal(250);
});
it('should not conflict with other instances of itself', function () {
var dispatcherA = dispatcher.createDispatcher({ stores: stores });
var dispatcherContextA = dispatcherA.createContext({});
var dispatcherB = dispatcher.createDispatcher({ stores: stores });
var dispatcherContextB = dispatcherB.createContext({});
var storeA = dispatcherContextA.getStore(RegularStore);
var storeB = dispatcherContextB.getStore(RegularStore);
expect(storeA.number).to.equal(50);
expect(storeB.number).to.equal(50);
dispatcherContextA.dispatch('TRIGGER', 100);
expect(storeA.number).to.equal(100);
expect(storeB.number).to.equal(50);
});
});
describe('A store using the `privates` feature', function () {
it('should not have any accessors defined on its prototype', function () {
expect(PrivatesStore.prototype.accessors).to.equal(undefined);
});
it('should mutate its internal state in response to a dispatch', function () {
var store = this.dispatcherContext.getStore(PrivatesStore);
expect(store.number).to.equal(50);
this.dispatcherContext.dispatch('TRIGGER', 100);
expect(store.number).to.equal(100);
});
it('should not allow set access to that internal state', function () {
var store = this.dispatcherContext.getStore(PrivatesStore);
var num = store.number;
expect(
function () {
store.number = 250;
}).to.throw(TypeError);
expect(store.number).to.equal(num);
});
it('should not conflict with other instances of itself', function () {
var dispatcherA = dispatcher.createDispatcher({ stores: stores });
var dispatcherContextA = dispatcherA.createContext({});
var dispatcherB = dispatcher.createDispatcher({ stores: stores });
var dispatcherContextB = dispatcherB.createContext({});
var storeA = dispatcherContextA.getStore(PrivatesStore);
var storeB = dispatcherContextB.getStore(PrivatesStore);
expect(storeA.number).to.equal(50);
expect(storeB.number).to.equal(50);
dispatcherContextA.dispatch('TRIGGER', 100);
expect(storeA.number).to.equal(100);
expect(storeB.number).to.equal(50);
});
});