-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
236 lines (188 loc) · 6.51 KB
/
test.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* global describe, it */
/**
* This test script should run with the 'dev' version of Vlow, including React.
* When the production version is compiled, react is not included.
*/
import React from 'react';
import assert from 'assert';
import Vlow from './src/index';
import {withVlow} from './src/index';
const TestActions = Vlow.createActions(['add', 'pop']);
class TestStore extends Vlow.Store {
constructor() {
super(TestActions);
this.state = {
items: []
};
}
onAdd(item) {
this.setState({
items: [...this.state.items, item]
});
}
onPop() {
if (this.state.items.length) {
this.setState({
items: this.state.items.slice(0, -1)
});
}
}
}
class TestComponent extends Vlow.Component {
constructor(props) {
super(props);
this.mapStore(TestStore);
}
render() {
return null;
}
}
class SomeClass extends React.Component {
isSomeClass() {
return true;
}
render() {
return null;
}
}
const TestWithComponent = withVlow(TestStore)(SomeClass);
class TestExtendedComponent extends Vlow.Component.extend(SomeClass) {
constructor(props) {
super(props);
this.mapStores([TestStore]);
}
}
const item0 = {id: 0, name: 'foo'};
const item1 = {id: 2, name: 'oof'};
describe('Test Vlow.createActions', () => {
it('Create duplicate action', () => {
assert.throws(() => { Vlow.createActions(['duplicate', 'duplicate']); });
});
it('Create empty action', () => {
assert.throws(() => { Vlow.createActions(['']); });
});
it('Create non-string action', () => {
assert.throws(() => { Vlow.createActions([null]); });
});
it('Create action starting with an underscore', () => {
assert.throws(() => { Vlow.createActions(['_doThis']); });
});
it('Create valid actions', () => {
let result;
assert.doesNotThrow(() => { result = Vlow.createActions(['fetch', 'update']); });
assert.strictEqual(typeof result.fetch, 'function');
assert.strictEqual(typeof result.update, 'function');
});
});
describe('Test Vlow.Component', () => {
const component = new TestComponent();
it('Initial items should return empty', () => {
assert.deepStrictEqual(component.state.items, []);
});
it('Add action should add items to the store', () => {
TestActions.add(item0);
TestActions.add(item1);
assert.strictEqual(component.state.items.length, 2);
});
it('Pop action should remove the last item from the store', () => {
assert.strictEqual(component.state.items.length, 2);
TestActions.pop();
TestActions.pop();
assert.strictEqual(component.state.items.length, 0);
});
it('Component should be able to mount', () => {
assert.doesNotThrow(() => { component.componentDidMount(); });
});
it('Component should unmount', () => {
assert.doesNotThrow(() => { component.componentWillUnmount(); });
});
});
describe('Test withVlow', () => {
const component = new TestWithComponent();
it('Initial items should return empty', () => {
assert.deepStrictEqual(component.state.items, []);
});
it('Add action should add items to the store', () => {
TestActions.add(item0);
TestActions.add(item1);
assert.strictEqual(component.state.items.length, 2);
});
it('Pop action should remove the last item from the store', () => {
assert.strictEqual(component.state.items.length, 2);
TestActions.pop();
TestActions.pop();
assert.strictEqual(component.state.items.length, 0);
});
it('Component should be able to mount', () => {
assert.doesNotThrow(() => { component.componentDidMount(); });
});
it('Component should unmount', () => {
assert.doesNotThrow(() => { component.componentWillUnmount(); });
});
});
describe('Test Vlow.Component.extend', () => {
const extendedComponent = new TestExtendedComponent();
it('Initial items should return empty', () => {
assert.deepStrictEqual(extendedComponent.state.items, []);
});
it('Add action should add items to the store', () => {
TestActions.add(item0);
TestActions.add(item1);
assert.strictEqual(extendedComponent.state.items.length, 2);
});
it('Pop action should remove the last item from the store', () => {
assert.strictEqual(extendedComponent.state.items.length, 2);
TestActions.pop();
TestActions.pop();
assert.strictEqual(extendedComponent.state.items.length, 0);
});
it('Component should be able to mount', () => {
assert.doesNotThrow(() => { extendedComponent.componentDidMount(); });
});
it('Component should unmount', () => {
assert.doesNotThrow(() => { extendedComponent.componentWillUnmount(); });
});
});
describe('Test Exception when overwriting state', () => {
class Component extends Vlow.Component {
constructor(props) {
super(props);
this.mapStore({store: TestStore, keys: ['items']});
this.state = {};
}
}
const component = new Component();
it('Raise when will mount is called', () => {
assert.throws(() => component.componentDidMount());
});
it('Component should still unmount', () => {
assert.doesNotThrow(() => component.componentWillUnmount());
});
});
describe('Test alter state', () => {
class FakeReactComponent {
constructor() {
}
}
class Component extends Vlow.Component.extend(FakeReactComponent) {
constructor(props) {
super(props);
this.mapStore({store: TestStore, altState: (storeState, state, props) => {
assert(state === this.state, '`state` must be equal to the components state.');
assert(props === this.props, '`props` must be equal to the components props.');
return !storeState ? null : {
items: storeState.items,
len: storeState.items.length
};
}});
}
}
const component = new Component();
it('Component len state property should be set', () => {
TestActions.add(item0);
assert.strictEqual(component.state.len, 1);
});
it('Component should unmount and persistent stores should be checked', () => {
assert.doesNotThrow(() => component.componentWillUnmount());
});
});