forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-checkbox.js
More file actions
126 lines (92 loc) · 3.08 KB
/
test-checkbox.js
File metadata and controls
126 lines (92 loc) · 3.08 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
/**
* MUI test react checkbox library
* @module test/react-tests/test-checkbox
*/
import assert from 'assert';
import createClass from 'create-react-class';
import React from 'react';
import ReactDOM from 'react-dom';
import ReactUtils from 'react-dom/test-utils';
import Checkbox from '../../src/react/checkbox';
import { getShallowRendererOutput } from '../lib/react-helpers';
describe('react/checkbox', function() {
let elem;
beforeEach(function() {
elem = <Checkbox label="My Label"></Checkbox>;
});
it('renders wrapper properly', function() {
let result = getShallowRendererOutput(elem);
assert.equal(result.type, 'div');
assert.equal(result.props.className, 'mui-checkbox ');
});
it('renders properly with additional classNames', function() {
let result = getShallowRendererOutput(
<Checkbox className="additional" label="test" />
);
assert.equal(result.props.className, 'mui-checkbox additional');
});
it('renders properly with additional styles', function() {
let result = getShallowRendererOutput(
<Checkbox style={{additonal: 'style'}} label="test" />
);
assert.equal(result.props.style.additonal, 'style');
});
it('renders content properly', function() {
let instance = ReactUtils.renderIntoDocument(elem);
let wrapperEl = ReactDOM.findDOMNode(instance);
assert.equal(wrapperEl.children.length, 1);
let labelEl = wrapperEl.children[0];
assert.equal(labelEl.tagName, 'LABEL');
let inputEl = labelEl.children[0];
assert.equal(inputEl.tagName, 'INPUT');
});
it('can be used as a controlled component', function() {
var TestApp = createClass({
getInitialState: function() {
return {checked: this.props.checked};
},
onChange: function(ev) {
this.setState({checked: ev.target.checked});
},
render: function() {
return (
<Checkbox
ref="refEl"
checked={this.state.checked}
onChange={this.onChange}
/>
);
}
});
let elem = <TestApp checked={false} />;
let instance = ReactUtils.renderIntoDocument(elem);
let inputEl = instance.refs.refEl.refs.inputEl
// check default value
assert.equal(inputEl.checked, false);
// update TestApp and check inputEl value
instance.setState({checked: true});
assert.equal(inputEl.checked, true);
// update inputEl and check state
inputEl.checked = false;
ReactUtils.Simulate.change(inputEl);
assert.equal(instance.state.checked, false);
});
it('supports onChange method', function(done) {
let counter = 0;
let onChangeFn = function() {
counter += 1;
};
let node = ReactUtils.renderIntoDocument(
<Checkbox onChange={onChangeFn} />
);
// change checkbox
let inputEl = ReactUtils.findRenderedDOMComponentWithTag(node, 'input');
ReactUtils.Simulate.change(inputEl);
// test conditions
setTimeout(function() {
// one onChange event (https://github.com/muicss/mui/issues/94)
assert.equal(counter, 1);
done();
}, 50);
});
});