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 _Flummox$Store$Actions = require('../../Flux');
10
11
var _addContext = require('./addContext');
12
13
var addContext = _interopRequire(_addContext);
14
15
var _React = require('react/addons');
16
17
var React = _interopRequire(_React);
18
19
var _FluxComponent = require('../FluxComponent');
20
21
var FluxComponent = _interopRequire(_FluxComponent);
22
23
var _sinon = require('sinon');
24
25
var sinon = _interopRequire(_sinon);
26
27
var TestUtils = React.addons.TestUtils;
28
29
describe('FluxComponent', function () {
30
var TestActions = (function (_Actions) {
31
function TestActions() {
32
_classCallCheck(this, TestActions);
33
34
if (_Actions != null) {
35
_Actions.apply(this, arguments);
36
}
37
}
38
39
_inherits(TestActions, _Actions);
40
41
TestActions.prototype.getSomething = function getSomething(something) {
42
return something;
43
};
44
45
return TestActions;
46
})(_Flummox$Store$Actions.Actions);
47
48
var TestStore = (function (_Store) {
49
function TestStore(flux) {
50
_classCallCheck(this, TestStore);
51
52
_Store.call(this);
53
54
var testActions = flux.getActions('test');
55
this.register(testActions.getSomething, this.handleGetSomething);
56
57
this.state = {
58
something: null
59
};
60
}
61
62
_inherits(TestStore, _Store);
63
64
TestStore.prototype.handleGetSomething = function handleGetSomething(something) {
65
this.setState({ something: something });
66
};
67
68
return TestStore;
69
})(_Flummox$Store$Actions.Store);
70
71
var Flux = (function (_Flummox) {
72
function Flux() {
73
_classCallCheck(this, Flux);
74
75
_Flummox.call(this);
76
77
this.createActions('test', TestActions);
78
this.createStore('test', TestStore, this);
79
}
80
81
_inherits(Flux, _Flummox);
82
83
return Flux;
84
})(_Flummox$Store$Actions.Flummox);
85
86
it('gets Flux property from either props or context', function () {
87
var flux = new Flux();
88
var contextComponent = undefined,
89
propsComponent = undefined;
90
91
var ContextComponent = addContext(FluxComponent, { flux: flux }, { flux: React.PropTypes.instanceOf(_Flummox$Store$Actions.Flummox) });
92
93
var tree = TestUtils.renderIntoDocument(React.createElement(ContextComponent, null));
94
95
contextComponent = TestUtils.findRenderedComponentWithType(tree, FluxComponent);
96
97
propsComponent = TestUtils.renderIntoDocument(React.createElement(FluxComponent, { flux: flux }));
98
99
expect(contextComponent.flux).to.be.an['instanceof'](_Flummox$Store$Actions.Flummox);
100
expect(propsComponent.flux).to.be.an['instanceof'](_Flummox$Store$Actions.Flummox);
101
});
102
103
it('allows for FluxComponents through the tree via context', function () {
104
var flux = new Flux();
105
var actions = flux.getActions('test');
106
107
var TopView = (function (_React$Component) {
108
function TopView() {
109
_classCallCheck(this, TopView);
110
111
if (_React$Component != null) {
112
_React$Component.apply(this, arguments);
113
}
114
}
115
116
_inherits(TopView, _React$Component);
117
118
TopView.prototype.render = function render() {
119
return React.createElement(
120
FluxComponent,
121
{ flux: flux },
122
React.createElement(SubView, null)
123
);
124
};
125
126
return TopView;
127
})(React.Component);
128
129
var SubView = (function (_React$Component2) {
130
function SubView() {
131
_classCallCheck(this, SubView);
132
133
if (_React$Component2 != null) {
134
_React$Component2.apply(this, arguments);
135
}
136
}
137
138
_inherits(SubView, _React$Component2);
139
140
SubView.prototype.render = function render() {
141
return React.createElement(SubSubView, null);
142
};
143
144
return SubView;
145
})(React.Component);
146
147
var SubSubView = (function (_React$Component3) {
148
function SubSubView() {
149
_classCallCheck(this, SubSubView);
150
151
if (_React$Component3 != null) {
152
_React$Component3.apply(this, arguments);
153
}
154
}
155
156
_inherits(SubSubView, _React$Component3);
157
158
SubSubView.prototype.render = function render() {
159
return React.createElement(
160
FluxComponent,
161
{ connectToStores: 'test' },
162
React.createElement('div', null)
163
);
164
};
165
166
return SubSubView;
167
})(React.Component);
168
169
var tree = TestUtils.renderIntoDocument(React.createElement(TopView, null));
170
171
var div = TestUtils.findRenderedDOMComponentWithTag(tree, 'div');
172
173
actions.getSomething('something good');
174
expect(div.props.something).to.equal('something good');
175
});
176
177
it('passes connectToStore prop to reactComponentMethod connectToStores()', function () {
178
var flux = new Flux();
179
var actions = flux.getActions('test');
180
181
var component = TestUtils.renderIntoDocument(React.createElement(FluxComponent, { flux: flux, connectToStores: 'test' }));
182
183
actions.getSomething('something good');
184
expect(component.state.something).to.deep.equal('something good');
185
actions.getSomething('something else');
186
expect(component.state.something).to.deep.equal('something else');
187
});
188
189
it('passes stateGetter prop to reactComponentMethod connectToStores()', function () {
190
var flux = new Flux();
191
var actions = flux.getActions('test');
192
var stateGetter = sinon.stub().returns({ fiz: 'bin' });
193
194
var component = TestUtils.renderIntoDocument(React.createElement(FluxComponent, { flux: flux, connectToStores: 'test', stateGetter: stateGetter }));
195
196
expect(component.state.fiz).to.equal('bin');
197
});
198
199
it('injects children with flux prop', function () {
200
var flux = new Flux();
201
var actions = flux.getActions('test');
202
203
var tree = TestUtils.renderIntoDocument(React.createElement(
204
FluxComponent,
205
{ flux: flux },
206
React.createElement('div', null)
207
));
208
209
var div = TestUtils.findRenderedDOMComponentWithTag(tree, 'div');
210
211
expect(div.props.flux).to.equal(flux);
212
});
213
214
it('injects children with props corresponding to component state', function () {
215
var flux = new Flux();
216
var actions = flux.getActions('test');
217
218
var tree = TestUtils.renderIntoDocument(React.createElement(
219
FluxComponent,
220
{ flux: flux, connectToStores: 'test' },
221
React.createElement('div', null)
222
));
223
224
var div = TestUtils.findRenderedDOMComponentWithTag(tree, 'div');
225
226
actions.getSomething('something good');
227
expect(div.props.something).to.equal('something good');
228
actions.getSomething('something else');
229
expect(div.props.something).to.equal('something else');
230
});
231
232
it('injects children with any extra props', function () {
233
var flux = new Flux();
234
var stateGetter = function stateGetter() {};
235
236
// Pass all possible PropTypes to ensure only extra props
237
// are injected.
238
var tree = TestUtils.renderIntoDocument(React.createElement(FluxComponent, {
239
flux: flux,
240
connectToStores: 'test',
241
stateGetter: stateGetter,
242
extraProp: 'hello',
243
render: function (props) {
244
return React.createElement('div', props);
245
}
246
}));
247
248
var div = TestUtils.findRenderedDOMComponentWithTag(tree, 'div');
249
250
expect(div.props.extraProp).to.equal('hello');
251
expect(Object.keys(div.props)).to.deep.equal(['flux', 'extraProp']);
252
});
253
254
it('wraps multiple children in span tag', function () {
255
var flux = new Flux();
256
257
var tree = TestUtils.renderIntoDocument(React.createElement(
258
FluxComponent,
259
{ flux: flux },
260
React.createElement('div', null),
261
React.createElement('div', null)
262
));
263
264
var wrapper = TestUtils.findRenderedDOMComponentWithTag(tree, 'span');
265
var divs = TestUtils.scryRenderedDOMComponentsWithTag(tree, 'div');
266
267
expect(divs.length).to.equal(2);
268
});
269
270
it('does not wrap single child in span tag', function () {
271
var flux = new Flux();
272
273
var tree = TestUtils.renderIntoDocument(React.createElement(
274
FluxComponent,
275
{ flux: flux },
276
React.createElement('div', null)
277
));
278
279
expect(TestUtils.findRenderedDOMComponentWithTag.bind(TestUtils, tree, 'span')).to['throw']('Did not find exactly one match for tag:span');
280
});
281
282
it('allows for nested FluxComponents', function () {
283
var flux = new Flux();
284
var actions = flux.getActions('test');
285
286
var tree = TestUtils.renderIntoDocument(React.createElement(
287
FluxComponent,
288
{ flux: flux, connectToStores: 'test' },
289
React.createElement(
290
FluxComponent,
291
null,
292
React.createElement('div', null)
293
)
294
));
295
296
var div = TestUtils.findRenderedDOMComponentWithTag(tree, 'div');
297
298
actions.getSomething('something good');
299
expect(div.props.something).to.equal('something good');
300
actions.getSomething('something else');
301
expect(div.props.something).to.equal('something else');
302
});
303
304
it('uses `render` prop for custom rendering, if it exists', function () {
305
var flux = new Flux();
306
var actions = flux.getActions('test');
307
308
var tree = TestUtils.renderIntoDocument(React.createElement(FluxComponent, {
309
flux: flux,
310
connectToStores: 'test',
311
render: function (props) {
312
return React.createElement('div', { something: props.something });
313
}
314
}));
315
316
var div = TestUtils.findRenderedDOMComponentWithTag(tree, 'div');
317
318
actions.getSomething('something good');
319
expect(div.props.something).to.equal('something good');
320
actions.getSomething('something else');
321
expect(div.props.something).to.equal('something else');
322
});
323
324
it('updates with render-time computed values in state getters on componentWillReceiveProps()', function () {
325
var flux = new Flux();
326
327
var Owner = (function (_React$Component4) {
328
function Owner(props) {
329
_classCallCheck(this, Owner);
330
331
_React$Component4.call(this, props);
332
333
this.state = {
334
foo: 'bar'
335
};
336
}
337
338
_inherits(Owner, _React$Component4);
339
340
Owner.prototype.render = function render() {
341
var _this = this;
342
343
return React.createElement(FluxComponent, {
344
flux: flux,
345
connectToStores: {
346
test: function test(store) {
347
return {
348
yay: _this.state.foo
349
};
350
}
351
},
352
render: function (storeState) {
353
return React.createElement('div', storeState);
354
}
355
});
356
};
357
358
return Owner;
359
})(React.Component);
360
361
var owner = TestUtils.renderIntoDocument(React.createElement(Owner, null));
362
var div = TestUtils.findRenderedDOMComponentWithTag(owner, 'div');
363
364
expect(div.props.yay).to.equal('bar');
365
owner.setState({ foo: 'baz' });
366
expect(div.props.yay).to.equal('baz');
367
});
368
});
369