forked from alibaba-fusion/next
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-spec.js
More file actions
160 lines (151 loc) · 5.22 KB
/
index-spec.js
File metadata and controls
160 lines (151 loc) · 5.22 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
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
import React from 'react';
import Enzyme, { mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import sinon from 'sinon';
import assert from 'power-assert';
import Switch from '../../src/switch/index';
Enzyme.configure({ adapter: new Adapter() });
describe('Switch', () => {
it('on', () => {
const wrapper = mount(<Switch checked />);
assert(wrapper.find('.next-switch-on').length === 1);
});
it('off', () => {
const wrapper = mount(<Switch />);
assert(wrapper.find('.next-switch-off').length === 1);
});
it('size', () => {
const wrapper = mount(<Switch size="small" />);
assert(wrapper.find('.next-switch-small').length === 1);
});
it('defaultChecked and disabled', () => {
const wrapper = mount(<Switch defaultChecked disabled />);
assert(wrapper.find('.next-switch-on').length === 1);
wrapper.find('.next-switch').simulate('click');
assert(wrapper.find('.next-switch-on').length === 1);
});
it('children setting and onChange', () => {
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
checked: false,
};
this.onChange = this.onChange.bind(this);
}
render() {
return (
<div>
<Switch
checkedChildren="开"
unCheckedChildren="关"
checked={this.state.checked}
onChange={this.onChange}
/>
</div>
);
}
onChange(checked) {
this.setState({ checked });
}
}
const wrapper = mount(<App />);
assert(wrapper.find('.next-switch-children').text() === '关');
wrapper.find('.next-switch').simulate('click');
assert(wrapper.find('.next-switch-children').text() === '开');
//非受控的onChange
const onChange = sinon.spy();
const wrapper1 = mount(
<Switch
checkedChildren="开"
unCheckedChildren="关"
onChange={onChange}
/>
);
assert(wrapper1.find('.next-switch-children').text() === '关');
wrapper1.find('.next-switch').simulate('click');
assert(wrapper1.find('.next-switch-children').text() === '开');
assert(onChange.called);
});
it('onClick', () => {
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
checked: false,
};
this.onChange = this.onChange.bind(this);
}
render() {
return (
<div>
<Switch
checkedChildren="开"
unCheckedChildren="关"
checked={this.state.checked}
onChange={this.onChange}
onClick={this.props.clickCallback}
/>
</div>
);
}
onChange(checked) {
this.setState({ checked });
}
}
const onClick = sinon.spy();
const wrapper = mount(<App clickCallback={onClick} />);
wrapper.find('.next-switch').simulate('click');
assert(onClick.called);
});
it('onKeydown', () => {
const wrapper = mount(<Switch defaultChecked={false} />);
wrapper.simulate('keydown', {
keyCode: 13,
});
assert(wrapper.find('.next-switch-on').length === 1);
});
it('checked === undefined', () => {
const wrapper = mount(<Switch checked />);
wrapper.setProps({
checked: undefined,
});
assert(wrapper.find('.next-switch-off').length === 1);
});
it('should render readonly switch', () => {
const wrapper = mount(<Switch readOnly />);
assert(wrapper.find('.next-switch-off').length === 1);
wrapper.find('.next-switch').simulate('click');
assert(wrapper.find('.next-switch-off').length === 1);
});
it('should renderPreview', () => {
const wrapper = mount(
<Switch
id="render-preview"
isPreview
renderPreview={() => 'preview switch'}
/>
);
assert(wrapper.getDOMNode().innerText === 'preview switch');
});
it('should use checkedChildren if exist when on & preview', () => {
const wrapper = mount(
<Switch
isPreview
checked
checkedChildren="✓"
/>
);
assert(wrapper.getDOMNode().innerText === '✓');
});
it('should use unCheckedChildren if exist when off & preview', () => {
const wrapper = mount(
<Switch
isPreview
checked={false}
unCheckedChildren="✕"
/>
);
assert(wrapper.getDOMNode().innerText === '✕');
});
});