Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81171 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @emails react-core
10
*/
11
12
"use strict";
13
14
/*jshint evil:true */
15
16
var emptyFunction = require('emptyFunction');
17
var mocks = require('mocks');
18
19
describe('ReactDOMInput', function() {
20
var React;
21
var ReactLink;
22
var ReactTestUtils;
23
24
beforeEach(function() {
25
React = require('React');
26
ReactLink = require('ReactLink');
27
ReactTestUtils = require('ReactTestUtils');
28
});
29
30
it('should display `defaultValue` of number 0', function() {
31
var stub = <input type="text" defaultValue={0} />;
32
stub = ReactTestUtils.renderIntoDocument(stub);
33
var node = stub.getDOMNode();
34
35
expect(node.value).toBe('0');
36
});
37
38
it('should display "true" for `defaultValue` of `true`', function() {
39
var stub = <input type="text" defaultValue={true} />;
40
stub = ReactTestUtils.renderIntoDocument(stub);
41
var node = stub.getDOMNode();
42
43
expect(node.value).toBe('true');
44
});
45
46
it('should display "false" for `defaultValue` of `false`', function() {
47
var stub = <input type="text" defaultValue={false} />;
48
stub = ReactTestUtils.renderIntoDocument(stub);
49
var node = stub.getDOMNode();
50
51
expect(node.value).toBe('false');
52
});
53
54
it('should display "foobar" for `defaultValue` of `objToString`', function() {
55
var objToString = {
56
toString: function() {
57
return "foobar";
58
}
59
};
60
61
var stub = <input type="text" defaultValue={objToString} />;
62
stub = ReactTestUtils.renderIntoDocument(stub);
63
var node = stub.getDOMNode();
64
65
expect(node.value).toBe('foobar');
66
});
67
68
it('should display `value` of number 0', function() {
69
var stub = <input type="text" value={0} />;
70
stub = ReactTestUtils.renderIntoDocument(stub);
71
var node = stub.getDOMNode();
72
73
expect(node.value).toBe('0');
74
});
75
76
it('should allow setting `value` to `true`', function() {
77
var stub = <input type="text" value="yolo" onChange={emptyFunction} />;
78
stub = ReactTestUtils.renderIntoDocument(stub);
79
var node = stub.getDOMNode();
80
81
expect(node.value).toBe('yolo');
82
83
stub.replaceProps({value: true, onChange: emptyFunction});
84
expect(node.value).toEqual('true');
85
});
86
87
it("should allow setting `value` to `false`", function() {
88
var stub = <input type="text" value="yolo" onChange={emptyFunction} />;
89
stub = ReactTestUtils.renderIntoDocument(stub);
90
var node = stub.getDOMNode();
91
92
expect(node.value).toBe('yolo');
93
94
stub.replaceProps({value: false});
95
expect(node.value).toEqual('false');
96
});
97
98
it('should allow setting `value` to `objToString`', function() {
99
var stub = <input type="text" value="foo" onChange={emptyFunction} />;
100
stub = ReactTestUtils.renderIntoDocument(stub);
101
var node = stub.getDOMNode();
102
103
expect(node.value).toBe('foo');
104
105
var objToString = {
106
toString: function() {
107
return "foobar";
108
}
109
};
110
111
stub.replaceProps({value: objToString, onChange: emptyFunction});
112
expect(node.value).toEqual('foobar');
113
});
114
115
it('should properly control a value of number `0`', function() {
116
var stub = <input type="text" value={0} onChange={emptyFunction} />;
117
stub = ReactTestUtils.renderIntoDocument(stub);
118
var node = stub.getDOMNode();
119
120
node.value = 'giraffe';
121
ReactTestUtils.Simulate.change(node);
122
expect(node.value).toBe('0');
123
});
124
125
it('should not set a value for submit buttons unnecessarily', function() {
126
var stub = <input type="submit" />;
127
stub = ReactTestUtils.renderIntoDocument(stub);
128
var node = stub.getDOMNode();
129
130
// The value shouldn't be '', or else the button will have no text; it
131
// should have the default "Submit" or "Submit Query" label. Most browsers
132
// report this as not having a `value` attribute at all; IE reports it as
133
// the actual label that the user sees.
134
expect(
135
!node.hasAttribute('value') || node.getAttribute('value').length > 0
136
).toBe(true);
137
});
138
139
it('should control radio buttons', function() {
140
var RadioGroup = React.createClass({
141
render: function() {
142
return (
143
<div>
144
<input
145
ref="a"
146
type="radio"
147
name="fruit"
148
checked={true}
149
onChange={emptyFunction}
150
/>A
151
<input
152
ref="b"
153
type="radio"
154
name="fruit"
155
onChange={emptyFunction}
156
/>B
157
158
<form>
159
<input
160
ref="c"
161
type="radio"
162
name="fruit"
163
defaultChecked={true}
164
onChange={emptyFunction}
165
/>
166
</form>
167
</div>
168
);
169
}
170
});
171
172
var stub = ReactTestUtils.renderIntoDocument(<RadioGroup />);
173
var aNode = stub.refs.a.getDOMNode();
174
var bNode = stub.refs.b.getDOMNode();
175
var cNode = stub.refs.c.getDOMNode();
176
177
expect(aNode.checked).toBe(true);
178
expect(bNode.checked).toBe(false);
179
// c is in a separate form and shouldn't be affected at all here
180
expect(cNode.checked).toBe(true);
181
182
bNode.checked = true;
183
// This next line isn't necessary in a proper browser environment, but
184
// jsdom doesn't uncheck the others in a group (which makes this whole test
185
// a little less effective)
186
aNode.checked = false;
187
expect(cNode.checked).toBe(true);
188
189
// Now let's run the actual ReactDOMInput change event handler
190
ReactTestUtils.Simulate.change(bNode);
191
192
// The original state should have been restored
193
expect(aNode.checked).toBe(true);
194
expect(cNode.checked).toBe(true);
195
});
196
197
it('should support ReactLink', function() {
198
var container = document.createElement('div');
199
var link = new ReactLink('yolo', mocks.getMockFunction());
200
var instance = <input type="text" valueLink={link} />;
201
202
instance = React.render(instance, container);
203
204
expect(instance.getDOMNode().value).toBe('yolo');
205
expect(link.value).toBe('yolo');
206
expect(link.requestChange.mock.calls.length).toBe(0);
207
208
instance.getDOMNode().value = 'test';
209
ReactTestUtils.Simulate.change(instance.getDOMNode());
210
211
expect(link.requestChange.mock.calls.length).toBe(1);
212
expect(link.requestChange.mock.calls[0][0]).toEqual('test');
213
});
214
215
it('should warn with value and no onChange handler', function() {
216
var oldWarn = console.warn;
217
try {
218
console.warn = mocks.getMockFunction();
219
220
var node = document.createElement('div');
221
var link = new ReactLink('yolo', mocks.getMockFunction());
222
React.render(<input type="text" valueLink={link} />, node);
223
expect(console.warn.mock.calls.length).toBe(0);
224
225
React.render(
226
<input type="text" value="zoink" onChange={mocks.getMockFunction()} />,
227
node
228
);
229
expect(console.warn.mock.calls.length).toBe(0);
230
231
React.render(
232
<input type="text" value="zoink" readOnly={true} />,
233
node
234
);
235
expect(console.warn.mock.calls.length).toBe(0);
236
237
React.render(<input type="text" value="zoink" />, node);
238
expect(console.warn.mock.calls.length).toBe(1);
239
240
React.render(
241
<input type="text" value="zoink" readOnly={false} />,
242
node
243
);
244
expect(console.warn.mock.calls.length).toBe(2);
245
} finally {
246
console.warn = oldWarn;
247
}
248
});
249
250
it('should throw if both value and valueLink are provided', function() {
251
// Silences console.error messages
252
// ReactErrorUtils.guard is applied to all methods of a React component
253
// and calls console.error in __DEV__ (true for test environment)
254
spyOn(console, 'error');
255
256
var node = document.createElement('div');
257
var link = new ReactLink('yolo', mocks.getMockFunction());
258
var instance = <input type="text" valueLink={link} />;
259
260
expect(React.render.bind(React, instance, node)).not.toThrow();
261
262
instance =
263
<input
264
type="text"
265
valueLink={link}
266
value="test"
267
onChange={emptyFunction}
268
/>;
269
expect(React.render.bind(React, instance, node)).toThrow();
270
271
instance = <input type="text" valueLink={link} onChange={emptyFunction} />;
272
expect(React.render.bind(React, instance, node)).toThrow();
273
274
});
275
276
it('should support checkedLink', function() {
277
var container = document.createElement('div');
278
var link = new ReactLink(true, mocks.getMockFunction());
279
var instance = <input type="checkbox" checkedLink={link} />;
280
281
instance = React.render(instance, container);
282
283
expect(instance.getDOMNode().checked).toBe(true);
284
expect(link.value).toBe(true);
285
expect(link.requestChange.mock.calls.length).toBe(0);
286
287
instance.getDOMNode().checked = false;
288
ReactTestUtils.Simulate.change(instance.getDOMNode());
289
290
expect(link.requestChange.mock.calls.length).toBe(1);
291
expect(link.requestChange.mock.calls[0][0]).toEqual(false);
292
});
293
294
it('should warn with checked and no onChange handler', function() {
295
var oldWarn = console.warn;
296
try {
297
console.warn = mocks.getMockFunction();
298
299
var node = document.createElement('div');
300
var link = new ReactLink(true, mocks.getMockFunction());
301
React.render(<input type="checkbox" checkedLink={link} />, node);
302
expect(console.warn.mock.calls.length).toBe(0);
303
304
React.render(
305
<input
306
type="checkbox"
307
checked="false"
308
onChange={mocks.getMockFunction()}
309
/>,
310
node
311
);
312
expect(console.warn.mock.calls.length).toBe(0);
313
314
React.render(
315
<input type="checkbox" checked="false" readOnly={true} />,
316
node
317
);
318
expect(console.warn.mock.calls.length).toBe(0);
319
320
React.render(<input type="checkbox" checked="false" />, node);
321
expect(console.warn.mock.calls.length).toBe(1);
322
323
React.render(
324
<input type="checkbox" checked="false" readOnly={false} />,
325
node
326
);
327
expect(console.warn.mock.calls.length).toBe(2);
328
} finally {
329
console.warn = oldWarn;
330
}
331
});
332
333
it('should throw if both checked and checkedLink are provided', function() {
334
// Silences console.error messages
335
// ReactErrorUtils.guard is applied to all methods of a React component
336
// and calls console.error in __DEV__ (true for test environment)
337
spyOn(console, 'error');
338
339
var node = document.createElement('div');
340
var link = new ReactLink(true, mocks.getMockFunction());
341
var instance = <input type="checkbox" checkedLink={link} />;
342
343
expect(React.render.bind(React, instance, node)).not.toThrow();
344
345
instance =
346
<input
347
type="checkbox"
348
checkedLink={link}
349
checked="false"
350
onChange={emptyFunction}
351
/>;
352
expect(React.render.bind(React, instance, node)).toThrow();
353
354
instance =
355
<input type="checkbox" checkedLink={link} onChange={emptyFunction} />;
356
expect(React.render.bind(React, instance, node)).toThrow();
357
358
});
359
360
it('should throw if both checkedLink and valueLink are provided', function() {
361
// Silences console.error messages
362
// ReactErrorUtils.guard is applied to all methods of a React component
363
// and calls console.error in __DEV__ (true for test environment)
364
spyOn(console, 'error');
365
366
var node = document.createElement('div');
367
var link = new ReactLink(true, mocks.getMockFunction());
368
var instance = <input type="checkbox" checkedLink={link} />;
369
370
expect(React.render.bind(React, instance, node)).not.toThrow();
371
372
instance = <input type="checkbox" valueLink={link} />;
373
expect(React.render.bind(React, instance, node)).not.toThrow();
374
375
instance =
376
<input type="checkbox" checkedLink={link} valueLink={emptyFunction} />;
377
expect(React.render.bind(React, instance, node)).toThrow();
378
});
379
});
380
381