connect.test.js
6.92 KB
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
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import React from 'react';
import { mount } from 'enzyme';
import { create, Provider, connect } from '../index';
var StatelessApp;
var Connected;
var store;
var wrapper;
var StatefulApp = /** @class */ (function (_super) {
__extends(StatefulApp, _super);
function StatefulApp() {
return _super !== null && _super.apply(this, arguments) || this;
}
StatefulApp.prototype.render = function () {
return (React.createElement("div", null, this.props.msg));
};
return StatefulApp;
}(React.Component));
describe('stateless', function () {
beforeEach(function () {
StatelessApp = function (_a) {
var msg = _a.msg;
return React.createElement("div", null, msg);
};
Connected = connect(function (state) { return state; })(StatelessApp);
store = create({ msg: 'hello', count: 0 });
wrapper = mount(React.createElement(Provider, { store: store },
React.createElement(Connected, null)));
});
test('map state to props', function () {
expect(wrapper.text()).toBe('hello');
});
test('renrender as subscribed state changes', function () {
store.setState({ msg: 'halo' });
expect(wrapper.text()).toBe('halo');
});
test('on rerender when unsubscribed state changes', function () {
store.setState({ count: 1 });
expect(wrapper.text()).toBe('hello');
});
test('do not subscribe', function () {
Connected = connect()(StatelessApp);
wrapper = mount(React.createElement(Provider, { store: store },
React.createElement(Connected, { msg: "hello" })));
expect(wrapper.instance().unsubscribe).toBeUndefined();
});
test('pass own props to mapStateToProps', function () {
Connected = connect(function (state, props) { return ({
msg: state.msg + " " + props.name
}); })(StatelessApp);
wrapper = mount(React.createElement(Provider, { store: store },
React.createElement(Connected, { name: "world" })));
expect(wrapper.text()).toBe('hello world');
});
test('mapStateToProps is invoked when own props changes', function () {
Connected = connect(function (state, props) { return ({
msg: state.msg + " " + props.name
}); })(StatelessApp);
var App = /** @class */ (function (_super) {
__extends(App, _super);
function App() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
name: 'world'
};
return _this;
}
App.prototype.render = function () {
var _this = this;
return (React.createElement("div", null,
React.createElement("button", { onClick: function () { return _this.setState({ name: 'there' }); } }, "Click"),
React.createElement(Connected, { name: this.state.name })));
};
return App;
}(React.Component));
wrapper = mount(React.createElement(Provider, { store: store },
React.createElement(App, null)));
wrapper.find('button').simulate('click');
expect(wrapper.find(Connected).text()).toBe('hello there');
});
test('mapStateToProps is not invoked when own props is not used', function () {
var mapStateToProps = jest.fn(function (state) { return ({ msg: state.msg }); });
Connected = connect(mapStateToProps)(StatelessApp);
var App = /** @class */ (function (_super) {
__extends(App, _super);
function App() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
name: 'world'
};
return _this;
}
App.prototype.render = function () {
var _this = this;
return (React.createElement("div", null,
React.createElement("button", { onClick: function () { return _this.setState({ name: 'there' }); } }, "Click"),
React.createElement(Connected, { name: this.state.name })));
};
return App;
}(React.Component));
wrapper = mount(React.createElement(Provider, { store: store },
React.createElement(App, null)));
wrapper.find('button').simulate('click');
expect(mapStateToProps).toHaveBeenCalledTimes(2);
});
// https://github.com/ant-design/ant-design/issues/11723
test('rerender component when props changes', function () {
var Dummy = function (_a) {
var visible = _a.visible;
return React.createElement("div", null, visible && 'hello');
};
Connected = connect(function (state, props) { return ({
visible: state.visible === false ? props.ownVisible : state.visible
}); })(Dummy);
var App = /** @class */ (function (_super) {
__extends(App, _super);
function App() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
visible: true,
};
return _this;
}
App.prototype.render = function () {
var _this = this;
return (React.createElement("div", null,
React.createElement("button", { onClick: function () { return _this.setState({ visible: false }); } }, "Click"),
React.createElement(Connected, { ownVisible: this.state.visible })));
};
return App;
}(React.Component));
store = create({ visible: false });
wrapper = mount(React.createElement(Provider, { store: store },
React.createElement(App, null)));
wrapper.find('button').simulate('click');
expect(wrapper.find(Dummy).text()).toBe('');
store.setState({ visible: true });
wrapper.update();
expect(wrapper.find(Dummy).text()).toBe('hello');
});
});
describe('stateful', function () {
beforeEach(function () {
Connected = connect(function (state) { return state; })(StatefulApp);
store = create({ msg: 'hello', count: 0 });
wrapper = mount(React.createElement(Provider, { store: store },
React.createElement(Connected, null)));
});
});