forked from xiyuyizhi/vod-fp.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
208 lines (192 loc) · 4.9 KB
/
store.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
import {
combineActions,
combineStates,
createStore
} from '../../src/oop/store';
import { Just } from '../../src';
import { F } from '../../src';
import { create } from 'domain';
const { compose, map, prop } = F;
const chai = require('chai');
const spies = require('chai-spies');
chai.use(spies);
chai.should();
describe('OOp: test store', function() {
let spy;
beforeEach(() => {
spy = chai.spy();
});
it('combineActions', () => {
const actions1 = {
TO_DO1: 'toDo1',
TO_DO2: 'toDo2'
};
const actions2 = {
TO_DO3: 'toDo3',
TO_DO4: 'toDo4'
};
const combined = combineActions(actions1, actions2);
Object.keys(combined).length.should.be.equal(4);
});
it('combineActions with module', () => {
const global = {
ERROR: 'error'
};
const module1 = {
module: 'PLAYLIST',
ACTION: {
M3U8_URL: 'm3u8Url',
PLAYLIST: 'playlist',
CURRENT_LEVEL: 'currentLevel'
}
};
const module2 = {
module: 'MEDIA',
ACTION: {
MEDIA_SOURCE: 'mediaSource'
}
};
const result = combineActions(global, module1, module2);
result.should.have.property('ERROR');
result.should.have.property('PLAYLIST');
result.should.have.property('MEDIA');
result['ERROR'].should.be.equal('error');
result['PLAYLIST'].should.have.property('PLAYLIST');
result['PLAYLIST'].should.have.property('CURRENT_LEVEL');
result['PLAYLIST']['CURRENT_LEVEL'].should.be.equal(
'playlist.currentLevel'
);
});
it('combineStates', () => {
const state1 = {
getState() {
return { levels: [] };
}
};
const state2 = {
getState() {
return {
mediaEle: null,
mediaSource: null
};
}
};
const result = combineStates(state1, state2);
result.should.have.property('derive');
Object.keys(result).length.should.be.equal(4);
});
it('combineStates with module', () => {
const module1 = {
module: 'PLAYLIST',
getState() {
return {
m3u8Url: '',
levels: [],
currentLevel: -1,
derive: {
currentLevel: () => {}
}
};
}
};
const module2 = {
module: 'MEDIA',
getState() {
return {
mediaEle: null,
mediaSource: null
};
}
};
const result = combineStates(module1, module2);
result.should.have.property('derive');
result.should.have.property('playlist');
result.playlist.should.have.property('m3u8Url');
result.should.have.property('media');
});
it('createStore', () => {
const states = {
m3u8Url: '',
levels: [],
currentLevel: -1
};
const actions = {
M3U8_URL: 'm3u8Url',
LEVELS: 'levels',
CURRENT_LEVEL: 'currentLevel'
};
const store = createStore(states, actions);
const initState = store.getState();
initState.constructor.should.be.equal(Just);
initState.value().should.have.property('m3u8Url');
initState.value().should.have.property('levels');
store.subscribe(actions.M3U8_URL, spy);
store.dispatch(actions.M3U8_URL, 'https://xxxx.com');
spy.should.be.called();
store
.getState()
.value()
.m3u8Url.should.be.equal('https://xxxx.com');
store.dispatch(actions.LEVELS, [123]);
store
.getState()
.value()
.levels[0].should.be.equal(123);
store.dispatch(actions.CURRENT_LEVEL, 2);
});
it('createStore with module', () => {
const module1 = {
module: 'PLAYLIST',
ACTION: {
CURRENT_LEVEL: 'currentLevel',
LEVELS: 'levels'
},
getState() {
return {
levels: [],
currentLevel: -1,
derive: {
levels(state, payload) {
if (!payload) {
return map(prop('levels'))(state);
}
return state.map(x => {
x.levels = payload;
return x;
});
},
currentLevel(state, payload) {
if (!payload) {
return map(prop('currentLevel'))(state);
}
return state.map(x => {
x.currentLevel = payload;
return x;
});
}
}
};
}
};
const a = combineActions(module1);
const s = combineStates(module1);
const store = createStore(s, a);
store
.getState(a.PLAYLIST.LEVELS)
.value()
.length.should.be.equal(0);
store.subscribe(a.PLAYLIST.LEVELS, v => {
v.value()[0].should.be.equal(123);
});
store.dispatch(a.PLAYLIST.LEVELS, [123]);
store
.getState(a.PLAYLIST.CURRENT_LEVEL)
.value()
.should.be.equal(-1);
store.dispatch(a.PLAYLIST.CURRENT_LEVEL, 1);
store
.getState(a.PLAYLIST.CURRENT_LEVEL)
.value()
.should.be.equal(1);
});
});