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
var emptyFunction = require('emptyFunction');
15
var mocks = require('mocks');
16
17
describe('ReactDOMTextarea', function() {
18
var React;
19
var ReactLink;
20
var ReactTestUtils;
21
22
var renderTextarea;
23
24
beforeEach(function() {
25
React = require('React');
26
ReactLink = require('ReactLink');
27
ReactTestUtils = require('ReactTestUtils');
28
29
renderTextarea = function(component) {
30
var stub = ReactTestUtils.renderIntoDocument(component);
31
var node = stub.getDOMNode();
32
// Polyfilling the browser's quirky behavior.
33
node.value = node.innerHTML;
34
return stub;
35
};
36
});
37
38
it('should allow setting `defaultValue`', function() {
39
var stub = <textarea defaultValue="giraffe" />;
40
stub = renderTextarea(stub);
41
var node = stub.getDOMNode();
42
43
expect(node.value).toBe('giraffe');
44
45
// Changing `defaultValue` should do nothing.
46
stub.replaceProps({defaultValue: 'gorilla'});
47
expect(node.value).toEqual('giraffe');
48
});
49
50
it('should display `defaultValue` of number 0', function() {
51
var stub = <textarea defaultValue={0} />;
52
stub = renderTextarea(stub);
53
var node = stub.getDOMNode();
54
55
expect(node.value).toBe('0');
56
});
57
58
it('should display "false" for `defaultValue` of `false`', function() {
59
var stub = <textarea type="text" defaultValue={false} />;
60
stub = renderTextarea(stub);
61
var node = stub.getDOMNode();
62
63
expect(node.value).toBe('false');
64
});
65
66
it('should display "foobar" for `defaultValue` of `objToString`', function() {
67
var objToString = {
68
toString: function() {
69
return "foobar";
70
}
71
};
72
73
var stub = <textarea type="text" defaultValue={objToString} />;
74
stub = renderTextarea(stub);
75
var node = stub.getDOMNode();
76
77
expect(node.value).toBe('foobar');
78
});
79
80
it('should not render value as an attribute', function() {
81
var stub = <textarea value="giraffe" onChange={emptyFunction} />;
82
stub = renderTextarea(stub);
83
var node = stub.getDOMNode();
84
85
expect(node.getAttribute('value')).toBe(null);
86
});
87
88
it('should display `value` of number 0', function() {
89
var stub = <textarea value={0} />;
90
stub = renderTextarea(stub);
91
var node = stub.getDOMNode();
92
93
expect(node.value).toBe('0');
94
});
95
96
it('should allow setting `value` to `giraffe`', function() {
97
var stub = <textarea value="giraffe" onChange={emptyFunction} />;
98
stub = renderTextarea(stub);
99
var node = stub.getDOMNode();
100
101
expect(node.value).toBe('giraffe');
102
103
stub.replaceProps({value: 'gorilla', onChange: emptyFunction});
104
expect(node.value).toEqual('gorilla');
105
});
106
107
it('should allow setting `value` to `true`', function() {
108
var stub = <textarea value="giraffe" onChange={emptyFunction} />;
109
stub = renderTextarea(stub);
110
var node = stub.getDOMNode();
111
112
expect(node.value).toBe('giraffe');
113
114
stub.replaceProps({value: true, onChange: emptyFunction});
115
expect(node.value).toEqual('true');
116
});
117
118
it('should allow setting `value` to `false`', function() {
119
var stub = <textarea value="giraffe" onChange={emptyFunction} />;
120
stub = renderTextarea(stub);
121
var node = stub.getDOMNode();
122
123
expect(node.value).toBe('giraffe');
124
125
stub.replaceProps({value: false});
126
expect(node.value).toEqual('false');
127
});
128
129
it('should allow setting `value` to `objToString`', function() {
130
var stub = <textarea value="giraffe" onChange={emptyFunction} />;
131
stub = renderTextarea(stub);
132
var node = stub.getDOMNode();
133
134
expect(node.value).toBe('giraffe');
135
136
var objToString = {
137
toString: function() {
138
return "foo";
139
}
140
};
141
stub.replaceProps({value: objToString, onChange: emptyFunction});
142
expect(node.value).toEqual('foo');
143
});
144
145
it('should properly control a value of number `0`', function() {
146
var stub = <textarea value={0} onChange={emptyFunction} />;
147
stub = renderTextarea(stub);
148
var node = stub.getDOMNode();
149
150
node.value = 'giraffe';
151
ReactTestUtils.Simulate.change(node);
152
expect(node.value).toBe('0');
153
});
154
155
it('should treat children like `defaultValue`', function() {
156
spyOn(console, 'warn');
157
158
var stub = <textarea>giraffe</textarea>;
159
stub = renderTextarea(stub);
160
var node = stub.getDOMNode();
161
162
expect(console.warn.argsForCall.length).toBe(1);
163
expect(node.value).toBe('giraffe');
164
165
// Changing children should do nothing, it functions like `defaultValue`.
166
stub.replaceProps({children: 'gorilla'});
167
expect(node.value).toEqual('giraffe');
168
});
169
170
it('should allow numbers as children', function() {
171
spyOn(console, 'warn');
172
var node = renderTextarea(<textarea>{17}</textarea>).getDOMNode();
173
expect(console.warn.argsForCall.length).toBe(1);
174
expect(node.value).toBe('17');
175
});
176
177
it('should allow booleans as children', function() {
178
spyOn(console, 'warn');
179
var node = renderTextarea(<textarea>{false}</textarea>).getDOMNode();
180
expect(console.warn.argsForCall.length).toBe(1);
181
expect(node.value).toBe('false');
182
});
183
184
it('should allow objects as children', function() {
185
spyOn(console, 'warn');
186
var obj = {
187
toString: function() {
188
return "sharkswithlasers";
189
}
190
};
191
var node = renderTextarea(<textarea>{obj}</textarea>).getDOMNode();
192
expect(console.warn.argsForCall.length).toBe(1);
193
expect(node.value).toBe('sharkswithlasers');
194
});
195
196
it('should throw with multiple or invalid children', function() {
197
spyOn(console, 'warn');
198
199
expect(function() {
200
ReactTestUtils.renderIntoDocument(
201
<textarea>{'hello'}{'there'}</textarea>
202
);
203
}).toThrow();
204
205
expect(console.warn.argsForCall.length).toBe(1);
206
207
var node;
208
expect(function() {
209
node = renderTextarea(<textarea><strong /></textarea>).getDOMNode();
210
}).not.toThrow();
211
212
expect(node.value).toBe('[object Object]');
213
214
expect(console.warn.argsForCall.length).toBe(2);
215
});
216
217
it('should support ReactLink', function() {
218
var container = document.createElement('div');
219
var link = new ReactLink('yolo', mocks.getMockFunction());
220
var instance = <textarea valueLink={link} />;
221
222
instance = React.render(instance, container);
223
224
expect(instance.getDOMNode().value).toBe('yolo');
225
expect(link.value).toBe('yolo');
226
expect(link.requestChange.mock.calls.length).toBe(0);
227
228
instance.getDOMNode().value = 'test';
229
ReactTestUtils.Simulate.change(instance.getDOMNode());
230
231
expect(link.requestChange.mock.calls.length).toBe(1);
232
expect(link.requestChange.mock.calls[0][0]).toEqual('test');
233
});
234
});
235
236