Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80522 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 _connectToStores = require('../connectToStores');
10
11
var connectToStores = _interopRequire(_connectToStores);
12
13
var _addContext = require('./addContext');
14
15
var addContext = _interopRequire(_addContext);
16
17
var _Actions$Store$Flummox = require('../../Flux');
18
19
var _React = require('react/addons');
20
21
var React = _interopRequire(_React);
22
23
var PropTypes = React.PropTypes;
24
var TestUtils = React.addons.TestUtils;
25
26
var TestActions = (function (_Actions) {
27
function TestActions() {
28
_classCallCheck(this, TestActions);
29
30
if (_Actions != null) {
31
_Actions.apply(this, arguments);
32
}
33
}
34
35
_inherits(TestActions, _Actions);
36
37
TestActions.prototype.getSomething = function getSomething(something) {
38
return something;
39
};
40
41
return TestActions;
42
})(_Actions$Store$Flummox.Actions);
43
44
var TestStore = (function (_Store) {
45
function TestStore(flux) {
46
_classCallCheck(this, TestStore);
47
48
_Store.call(this);
49
50
var testActions = flux.getActions('test');
51
this.register(testActions.getSomething, this.handleGetSomething);
52
53
this.state = {
54
something: null
55
};
56
}
57
58
_inherits(TestStore, _Store);
59
60
TestStore.prototype.handleGetSomething = function handleGetSomething(something) {
61
this.setState({ something: something });
62
};
63
64
return TestStore;
65
})(_Actions$Store$Flummox.Store);
66
67
var Flux = (function (_Flummox) {
68
function Flux() {
69
_classCallCheck(this, Flux);
70
71
_Flummox.call(this);
72
73
this.createActions('test', TestActions);
74
this.createStore('test', TestStore, this);
75
}
76
77
_inherits(Flux, _Flummox);
78
79
return Flux;
80
})(_Actions$Store$Flummox.Flummox);
81
82
describe('connectToStores (HoC)', function () {
83
it('gets Flux from either props or context', function () {
84
var flux = new Flux();
85
var contextComponent = undefined,
86
propsComponent = undefined;
87
88
var BaseComponent = (function (_React$Component) {
89
function BaseComponent() {
90
_classCallCheck(this, BaseComponent);
91
92
if (_React$Component != null) {
93
_React$Component.apply(this, arguments);
94
}
95
}
96
97
_inherits(BaseComponent, _React$Component);
98
99
BaseComponent.prototype.render = function render() {
100
return React.createElement('div', null);
101
};
102
103
return BaseComponent;
104
})(React.Component);
105
106
var ConnectedComponent = connectToStores(BaseComponent, 'test');
107
108
var ContextComponent = addContext(ConnectedComponent, { flux: flux }, { flux: React.PropTypes.instanceOf(_Actions$Store$Flummox.Flummox) });
109
110
var tree = TestUtils.renderIntoDocument(React.createElement(ContextComponent, null));
111
112
contextComponent = TestUtils.findRenderedComponentWithType(tree, ConnectedComponent);
113
114
propsComponent = TestUtils.renderIntoDocument(React.createElement(ConnectedComponent, { flux: flux }));
115
116
expect(contextComponent.flux).to.be.an['instanceof'](_Actions$Store$Flummox.Flummox);
117
expect(propsComponent.flux).to.be.an['instanceof'](_Actions$Store$Flummox.Flummox);
118
});
119
120
it('transfers props', function () {
121
var flux = new Flux();
122
123
var BaseComponent = (function (_React$Component2) {
124
function BaseComponent() {
125
_classCallCheck(this, BaseComponent);
126
127
if (_React$Component2 != null) {
128
_React$Component2.apply(this, arguments);
129
}
130
}
131
132
_inherits(BaseComponent, _React$Component2);
133
134
BaseComponent.prototype.render = function render() {
135
return React.createElement('div', null);
136
};
137
138
return BaseComponent;
139
})(React.Component);
140
141
var ConnectedComponent = connectToStores(BaseComponent, 'test');
142
143
var tree = TestUtils.renderIntoDocument(React.createElement(ConnectedComponent, { flux: flux, foo: 'bar', bar: 'baz' }));
144
145
var component = TestUtils.findRenderedComponentWithType(tree, BaseComponent);
146
147
expect(component.props.foo).to.equal('bar');
148
expect(component.props.bar).to.equal('baz');
149
});
150
151
it('syncs with store after state change', function () {
152
var flux = new Flux();
153
154
var BaseComponent = (function (_React$Component3) {
155
function BaseComponent() {
156
_classCallCheck(this, BaseComponent);
157
158
if (_React$Component3 != null) {
159
_React$Component3.apply(this, arguments);
160
}
161
}
162
163
_inherits(BaseComponent, _React$Component3);
164
165
BaseComponent.prototype.render = function render() {
166
return React.createElement('div', null);
167
};
168
169
return BaseComponent;
170
})(React.Component);
171
172
var ConnectedComponent = connectToStores(BaseComponent, 'test');
173
174
var tree = TestUtils.renderIntoDocument(React.createElement(ConnectedComponent, { flux: flux }));
175
176
var component = TestUtils.findRenderedComponentWithType(tree, BaseComponent);
177
178
var getSomething = flux.getActions('test').getSomething;
179
180
expect(component.props.something).to.be['null'];
181
182
getSomething('do');
183
184
expect(component.props.something).to.equal('do');
185
186
getSomething('re');
187
188
expect(component.props.something).to.equal('re');
189
});
190
});
191