forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComboBoxSpec.coffee
More file actions
200 lines (171 loc) · 5.88 KB
/
Copy pathComboBoxSpec.coffee
File metadata and controls
200 lines (171 loc) · 5.88 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
#
# Copyright (C) 2012 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
define [
'jquery'
'compiled/widget/ComboBox'
'helpers/simulateClick'
], ($, ComboBox, simulateClick)->
QUnit.module 'ComboBox',
setup: ->
teardown: ->
# remove a combobox if one was created
@combobox.$el.remove() if @combobox?
confirmSelected = (combobox, item) ->
equal combobox.$menu.val(), combobox._value item
test 'constructor: dom setup', ->
items = [
{label: 'label1', value: 'value1'}
{label: 'label2', value: 'value2'}
{label: 'label3', value: 'value3'}
]
@combobox = new ComboBox items
# should have the infrastructure in place
ok @combobox.$el.hasClass 'ui-combobox'
ok @combobox.$prev.hasClass 'ui-combobox-prev'
ok @combobox.$next.hasClass 'ui-combobox-next'
ok @combobox.$menu[0].tagName, 'SELECT'
# should have the options (both flavors) set up according to items
options = $('option', @combobox.$menu)
equal options.length, 3
for item, i in items
equal options.eq(i).prop('value'), item.value
equal options.eq(i).text(), item.label
# should have the first item selected
confirmSelected @combobox, items[0]
test 'constructor: value', ->
items = [
{label: 'label1', id: 'id1'}
{label: 'label2', id: 'id2'}
{label: 'label3', id: 'id3'}
]
valueFunc = (item) -> item.id
@combobox = new ComboBox items, value: valueFunc
options = $('option', @combobox.$menu)
for item, i in items
equal options.eq(i).prop('value'), valueFunc item
test 'constructor: label', ->
items = [
{name: 'name1', value: 'value1'}
{name: 'name2', value: 'value2'}
{name: 'name3', value: 'value3'}
]
labelFunc = (item) -> item.name
@combobox = new ComboBox items, label: labelFunc
options = $('option', @combobox.$menu)
for item, i in items
equal options.eq(i).text(), labelFunc item
test 'constructor: selected', ->
items = [
{label: 'label1', value: 'value1'}
{label: 'label2', value: 'value2'}
{label: 'label3', value: 'value3'}
]
selectedItem = items[2]
@combobox = new ComboBox items, selected: selectedItem.value
# should have the specified item selected
confirmSelected @combobox, selectedItem
test 'constructor: value and selected', ->
items = [
{label: 'label1', id: 'id1'}
{label: 'label2', id: 'id2'}
{label: 'label3', id: 'id3'}
]
selectedItem = items[2]
valueFunc = (item) -> item.id
@combobox = new ComboBox items,
value: valueFunc
selected: valueFunc selectedItem
# should have the specified item selected
confirmSelected @combobox, selectedItem
test 'select', ->
items = [
{label: 'label1', value: 'value1'}
{label: 'label2', value: 'value2'}
{label: 'label3', value: 'value3'}
]
@combobox = new ComboBox items
spy = @spy()
@combobox.on 'change', spy
# calling select should change selection and trigger callback with new
# selected item
@combobox.select items[2].value
confirmSelected @combobox, items[2]
# for some reason spy.withArgs(items[2]).calledOnce doesn't work
ok spy.calledOnce
equal spy.getCall(0).args[0], items[2]
# calling with the current selection should not trigger callback
spy.reset()
@combobox.select items[2].value
ok not spy.called
test 'prev button', ->
items = [
{label: 'label1', value: 'value1'}
{label: 'label2', value: 'value2'}
{label: 'label3', value: 'value3'}
]
@combobox = new ComboBox items, selected: items[1].value
spy = @spy()
@combobox.on 'change', spy
# clicking prev button selects previous element
simulateClick @combobox.$prev[0]
confirmSelected @combobox, items[0]
ok spy.calledOnce
equal spy.getCall(0).args[0], items[0]
# clicking from the front wraps around
spy.reset()
simulateClick @combobox.$prev[0]
confirmSelected @combobox, items[2]
ok spy.calledOnce
equal spy.getCall(0).args[0], items[2]
test 'prev button: one item', ->
items = [{label: 'label1', value: 'value1'}]
@combobox = new ComboBox items
spy = @spy()
@combobox.on 'change', spy
# clicking prev button does nothing
simulateClick @combobox.$prev[0]
confirmSelected @combobox, items[0]
ok not spy.called
test 'next button', ->
items = [
{label: 'label1', value: 'value1'}
{label: 'label2', value: 'value2'}
{label: 'label3', value: 'value3'}
]
@combobox = new ComboBox items, selected: items[1].value
spy = @spy()
@combobox.on 'change', spy
# clicking prev button selects previous element
simulateClick @combobox.$next[0]
confirmSelected @combobox, items[2]
ok spy.calledOnce
equal spy.getCall(0).args[0], items[2]
# clicking from the front wraps around
spy.reset()
simulateClick @combobox.$next[0]
confirmSelected @combobox, items[0]
ok spy.calledOnce
equal spy.getCall(0).args[0], items[0]
test 'next button: one item', ->
items = [{label: 'label1', value: 'value1'}]
@combobox = new ComboBox items
spy = @spy()
@combobox.on 'change', spy
# clicking prev button does nothing
simulateClick @combobox.$next[0]
confirmSelected @combobox, items[0]
ok not spy.called