-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathselect-box.spec.js
More file actions
219 lines (187 loc) · 7.39 KB
/
select-box.spec.js
File metadata and controls
219 lines (187 loc) · 7.39 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var React = require('react/addons')
var TestUtils = React.addons.TestUtils
var SelectBox = React.createFactory(require('../lib/select-box'))
describe('SelectBox component', function () {
var selectBox
var options
var testOptions = [
{ value: 'red', label: 'Red' },
{ value: 'green', label: 'Green' },
{ value: 'blue', label: 'Blue' },
]
function makeSelectBox(multi) {
var args = testOptions.map(function (option) {
return React.DOM.option({value: option.value}, option.label)
})
args.unshift({
label: 'foo',
value: null,
multiple: multi !== true ? false : multi,
onChange: function() {}
})
return TestUtils.renderIntoDocument(SelectBox.apply(null, args))
}
function scryRenderedDOMComponentsWithClass(name) {
return TestUtils.scryRenderedDOMComponentsWithClass(
selectBox,
name
)
}
describe('Single select mode', function () {
beforeEach(function () {
selectBox = makeSelectBox()
})
it('should render the label when no value is selected', function () {
var label = scryRenderedDOMComponentsWithClass('react-select-box-label')
label.should.have.length(1)
label[0].getDOMNode().textContent.should.equal(selectBox.props.label)
})
it('should render the label when no value is selected', function () {
var label = scryRenderedDOMComponentsWithClass('react-select-box-label')
label.should.have.length(1)
label[0].getDOMNode().textContent.should.equal(selectBox.props.label)
})
it('should render the label for the selected value', function (done) {
selectBox.setProps({ value: testOptions[0].value }, function () {
var label = scryRenderedDOMComponentsWithClass('react-select-box-label')
label.should.have.length(1)
label[0].getDOMNode().textContent.should.equal(testOptions[0].label)
done()
})
})
it('should not render the clear button with no value selected', function (done) {
selectBox.setProps({ value: null }, function () {
var clear = scryRenderedDOMComponentsWithClass('react-select-box-clear')
clear.should.have.length(0)
done()
})
})
it('should render the clear button with a selected value', function (done) {
selectBox.setProps({ value: testOptions[0].value }, function () {
var clear = scryRenderedDOMComponentsWithClass('react-select-box-clear')
clear.should.have.length(1)
done()
})
})
it('should add hidden class to options when state.open is false', function (done) {
selectBox.setState({ open: false }, function () {
var options = scryRenderedDOMComponentsWithClass('react-select-box-options')
options.should.have.length(1)
options[0].getDOMNode().className.should.match(/hidden/)
done()
})
})
it('should render options', function (done) {
selectBox.setState({ open: true }, function () {
var options = scryRenderedDOMComponentsWithClass('react-select-box-options')
options.should.have.length(1)
done()
})
})
it('should show an option for each option in props.options', function (done) {
selectBox.setState({ open: true }, function () {
var options = scryRenderedDOMComponentsWithClass('react-select-box-option')
options.should.have.length(options.length)
options.forEach(function (option, i) {
option.getDOMNode().textContent.should.equal(testOptions[i].label)
})
done()
})
})
describe("Toggle options list open/closed", function() {
var selectBoxElement
beforeEach(function() {
selectBoxElement = TestUtils.findRenderedDOMComponentWithClass(
selectBox,
'react-select-box'
)
})
it('should close an open options list when select box is clicked', function(done) {
// Start with open options list
selectBox.setState({ open: true }, function () {
// Simulate a click on the select box (element with class tag `react-select-box`)
TestUtils.Simulate.click(selectBoxElement)
// Re-render component (to ensure state change occured)
selectBox.forceUpdate(function() {
// Check if it is closed
selectBox.state.open.should.equal(false)
// End test
done()
})
})
})
it('should open a closed options list when select box is clicked', function(done) {
// Start with closed options list
selectBox.setState({ open: false }, function () {
// Simulate a click on the select box (element with class tag `react-select-box`)
TestUtils.Simulate.click(selectBoxElement)
// Re-render component (to ensure state change occured)
selectBox.forceUpdate(function() {
// Check if it is open
selectBox.state.open.should.equal(true)
// End test
done()
})
})
})
it('should close an open select box when focused and ESC key is pressed', function(done) {
selectBox.setState({ open: true }, function () {
// Simulate focus event on selectBox
TestUtils.Simulate.focus(selectBoxElement)
// Simulate pressing escape key
TestUtils.Simulate.keyDown(selectBoxElement, {keyCode: 27, which: 27})
// Re-render component (to ensure state change occured)
selectBox.forceUpdate(function() {
// Check that it is closed
selectBox.state.open.should.equal(false)
// End test
done()
})
})
})
it('should open a closed select box when focused and ENTER key is pressed', function(done) {
selectBox.setState({ open: false }, function () {
// Simulate focus event on selectBox
TestUtils.Simulate.focus(selectBoxElement)
// Simulate pressing down arrow key
TestUtils.Simulate.keyDown(selectBoxElement, {keyCode: 40, which: 40})
// Re-render component (to ensure state change occured)
selectBox.forceUpdate(function() {
// Check that it is open
selectBox.state.open.should.equal(true)
// End test
done()
})
})
})
})
})
describe('Multi-select mode', function () {
var testValues
beforeEach(function () {
testValues = [testOptions[0].value, testOptions[1].value];
selectBox = makeSelectBox(true)
})
it('should render the label for muliple selected values', function (done) {
var expectedLabel = testOptions[0].label + ', ' + testOptions[1].label;
selectBox.setProps({ value: testValues }, function () {
var label = scryRenderedDOMComponentsWithClass('react-select-box-label')
label.should.have.length(1)
label[0].getDOMNode().textContent.should.equal(expectedLabel)
done()
})
})
it('should show all selected options', function (done) {
selectBox.setProps({ value: testValues }, function () {
selectBox.setState({ open: true }, function () {
selectBox.forceUpdate(function() {
var label = scryRenderedDOMComponentsWithClass('react-select-box-option-selected')
label.should.have.length(testValues.length)
selectBox.state.open.should.equal(true)
done()
})
})
})
})
})
})