forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-input.js
More file actions
127 lines (96 loc) · 3.4 KB
/
test-input.js
File metadata and controls
127 lines (96 loc) · 3.4 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
/**
* MUI test react input component
* @module test/react-tests/test-input
*/
import assert from 'assert';
import React from 'react';
import ReactDOM from 'react-dom';
import ReactUtils from 'react-addons-test-utils';
import Input from '../../src/react/input';
describe('react/input', function() {
let errFn;
before(function() {
errFn = console.error;
console.error = function(msg) {throw Error(msg);};
});
after(function() {
console.error = errFn;
});
it('renders wrapper properly', function() {
let instance = ReactUtils.renderIntoDocument(<Input></Input>);
let wrapperEl = ReactDOM.findDOMNode(instance);
assert.equal(wrapperEl.tagName, 'DIV');
assert.equal(wrapperEl.className.trim(), 'mui-textfield');
});
it('renders native input element', function() {
let elem = <Input defaultValue="my input"></Input>;
let instance = ReactUtils.renderIntoDocument(elem);
let inputEl = ReactUtils
.findRenderedDOMComponentWithTag(instance, 'input');
assert.equal(inputEl.value, 'my input');
});
it('adds dirty class on focus', function() {
let instance = ReactUtils.renderIntoDocument(<Input></Input>);
let inputEl = ReactUtils
.findRenderedDOMComponentWithTag(instance, 'input');
// starts with empty class
assert.equal(inputEl.className, 'mui--is-empty');
// adds dirty class on focus
ReactUtils.Simulate.focus(inputEl);
assert.equal(/mui--is-dirty/.test(inputEl.className), true);
assert.equal(/mui--is-empty/.test(inputEl.className), true);
assert.equal(/mui--is-not-empty/.test(inputEl.className), false);
// modify input
ReactUtils.Simulate.change(inputEl);
});
it('does controlled component validation', function() {
// raises error when `value` defined and `onChange missing
assert.throws(
function() {
let elem = <Input value="my value"></Input>;
let instance = ReactUtils.renderIntoDocument(elem);
},
/MUI Warning/
);
});
it('can be used as controlled component', function() {
var TestApp = React.createClass({
getInitialState: function() {
return {value: this.props.value};
},
onChange: function(ev) {
this.setState({value: ev.target.value});
},
render: function() {
return (
<Input
value={this.state.value}
onChange={this.onChange}
/>
);
}
});
let elem = <TestApp value="test" />;
let instance = ReactUtils.renderIntoDocument(elem);
let findComponent = ReactUtils.findRenderedDOMComponentWithTag;
let inputEl = findComponent(instance, 'input');
// check default value
assert.equal(inputEl.value, 'test');
// update TestApp and check inputEl value
instance.setState({value: 'test2'});
assert.equal(inputEl.value, 'test2');
// update inputEl and check state
inputEl.value = 'test3';
ReactUtils.Simulate.change(inputEl);
assert.equal(instance.state.value, 'test3');
});
it('handles label unmount gracefully', function() {
let elem = <Input label="label" defaultValue="defaultValue"></Input>;
let instance = ReactUtils.renderIntoDocument(elem);
let wrapperEl = ReactDOM.findDOMNode(instance);
ReactDOM.unmountComponentAtNode(wrapperEl.parentNode);
// TODO: How can we access the timer id to check if it was removed
// successfully?
assert.equal(true, true);
});
});