/
invoke.jsx
224 lines (189 loc) · 5.73 KB
/
invoke.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import { expect } from 'chai';
import wrap from 'mocha-wrap';
import sinon from 'sinon-sandbox';
import { Portal } from 'react-is';
import { render } from 'enzyme';
import getAdapter from 'enzyme/build/getAdapter';
import {
ITERATOR_SYMBOL,
sym,
} from 'enzyme/build/Utils';
import {
describeIf,
itIf,
} from '../../_helpers';
import realArrowFunction from '../../_helpers/realArrowFunction';
import { getElementPropSelector, getWrapperPropSelector } from '../../_helpers/selectors';
import {
is,
REACT16,
} from '../../_helpers/version';
import {
createClass,
createPortal,
createRef,
Fragment,
} from '../../_helpers/react-compat';
export default function describeInvoke({
Wrap,
WrapRendered,
Wrapper,
WrapperName,
isShallow,
isMount,
makeDOMElement,
}) {
describe('.invoke(eventName, ..args)', () => {
it('should return the handlers return value', () => {
const spy = sinon.stub().returns(123);
class Foo extends React.Component {
render() {
return (<a onClick={spy}>foo</a>);
}
}
const wrapper = shallow(<Foo />);
const value = wrapper.invoke('click');
expect(value).to.equal(123);
expect(spy).to.have.property('callCount', 1);
});
it('should invoke event handlers without propagation', () => {
class Foo extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
this.incrementCount = this.incrementCount.bind(this);
}
incrementCount() {
this.setState({ count: this.state.count + 1 });
}
render() {
const { count } = this.state;
return (
<div onClick={this.incrementCount}>
<a
className={`clicks-${count}`}
onClick={this.incrementCount}
>
foo
</a>
</div>
);
}
}
const wrapper = shallow(<Foo />);
expect(wrapper.find('.clicks-0').length).to.equal(1);
wrapper.find('a').invoke('click');
expect(wrapper.find('.clicks-1').length).to.equal(1);
});
it('should pass in arguments', () => {
const spy = sinon.spy();
class Foo extends React.Component {
render() {
return (
<a onClick={spy}>foo</a>
);
}
}
const wrapper = shallow(<Foo />);
const a = {};
const b = {};
wrapper.invoke('click', a, b);
expect(spy.args[0][0]).to.equal(a);
expect(spy.args[0][1]).to.equal(b);
});
describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => {
it('should invoke event handlers', () => {
const spy = sinon.spy();
const Foo = ({ onClick }) => (
<div onClick={onClick}>
<a onClick={onClick}>foo</a>
</div>
);
const wrapper = shallow(<Foo onClick={spy} />);
expect(spy).to.have.property('callCount', 0);
wrapper.find('a').invoke('click');
expect(spy).to.have.property('callCount', 1);
});
it('should pass in arguments', () => {
const spy = sinon.spy();
const Foo = () => (
<a onClick={spy}>foo</a>
);
const wrapper = shallow(<Foo />);
const a = {};
const b = {};
wrapper.invoke('click', a, b);
const [[arg1, arg2]] = spy.args;
expect(arg1).to.equal(a);
expect(arg2).to.equal(b);
});
});
describe('Normalizing JS event names', () => {
it('should convert lowercase events to React camelcase', () => {
const spy = sinon.spy();
const clickSpy = sinon.spy();
class SpiesOnClicks extends React.Component {
render() {
return (<a onClick={clickSpy} onDoubleClick={spy}>foo</a>);
}
}
const wrapper = shallow(<SpiesOnClicks />);
wrapper.invoke('dblclick');
expect(spy).to.have.property('callCount', 1);
wrapper.invoke('click');
expect(clickSpy).to.have.property('callCount', 1);
});
describeIf(is('> 0.13'), 'normalizing mouseenter', () => {
it('should convert lowercase events to React camelcase', () => {
const spy = sinon.spy();
class Foo extends React.Component {
render() {
return (<a onMouseEnter={spy}>foo</a>);
}
}
const wrapper = shallow(<Foo />);
wrapper.invoke('mouseenter');
expect(spy).to.have.property('callCount', 1);
});
it('should convert lowercase events to React camelcase in stateless components', () => {
const spy = sinon.spy();
const Foo = () => (
<a onMouseEnter={spy}>foo</a>
);
const wrapper = shallow(<Foo />);
wrapper.invoke('mouseenter');
expect(spy).to.have.property('callCount', 1);
});
});
});
it('should batch updates', () => {
let renderCount = 0;
class Foo extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0,
};
this.onClick = this.onClick.bind(this);
}
onClick() {
this.setState({ count: this.state.count + 1 });
this.setState({ count: this.state.count + 1 });
}
render() {
renderCount += 1;
const { count } = this.state;
return (
<a onClick={this.onClick}>{count}</a>
);
}
}
const wrapper = shallow(<Foo />);
wrapper.invoke('click');
expect(wrapper.text()).to.equal('1');
expect(renderCount).to.equal(2);
});
});
}