forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeverDropCollectionViewSpec.coffee
More file actions
185 lines (154 loc) · 6.41 KB
/
Copy pathNeverDropCollectionViewSpec.coffee
File metadata and controls
185 lines (154 loc) · 6.41 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
define [
'jquery'
'underscore'
'Backbone'
'compiled/collections/NeverDropCollection'
'compiled/views/assignments/NeverDropCollectionView'
'helpers/util'
], ($, _, Backbone, NeverDropCollection, NeverDropCollectionView, util) ->
class AssignmentStub extends Backbone.Model
name: -> @get('name')
toView: =>
name: @get('name')
id: @id
class Assignments extends Backbone.Collection
model: AssignmentStub
QUnit.module "NeverDropCollectionView",
setup: ->
@clock = sinon.useFakeTimers()
util.useOldDebounce()
@assignments = new Assignments((id: "#{i}", name: "Assignment #{i}") for i in [1..3])
@never_drops = new NeverDropCollection [],
assignments: @assignments
ag_id: 'new'
@view = new NeverDropCollectionView
collection: @never_drops
canChangeDropRules: true
$('#fixtures').empty().append @view.render().el
teardown: ->
@clock.restore()
util.useNormalDebounce()
addNeverDrop= ->
@never_drops.add
id: @never_drops.size()
label_id: 'new'
test "possibleValues is set to the range of assignment ids", ->
deepEqual @never_drops.possibleValues, @assignments.map (a) -> a.id
test "adding a NeverDrop to the collection reduces availableValues by one", ->
start_length = @never_drops.availableValues.length
addNeverDrop.call @
equal start_length - 1, @never_drops.availableValues.length
test "adding a NeverDrop renders a <select> with the value from the front of the availableValues collection", ->
expected_val = @never_drops.availableValues.slice(0)[0].id
addNeverDrop.call @
@clock.tick(101)
view = $('#fixtures').find('select')
ok view.length, 'a select was rendered'
equal expected_val, view.val(), 'the selects value is the same as the last available value'
test "the number of <option>s with the value the same as availableValue should equal the number of selects", ->
addNeverDrop.call @
addNeverDrop.call @
@clock.tick(101)
available_val = @never_drops.availableValues.at(0).id
equal $('#fixtures').find('option[value='+ available_val+']').length, 2
test "removing a NeverDrop from the collection increases availableValues by one", ->
addNeverDrop.call @
@clock.tick(101)
current_size = @never_drops.availableValues.length
model = @never_drops.at(0)
@never_drops.remove model
equal current_size + 1, @never_drops.availableValues.length
test "removing a NeverDrop from the collection removes the view", ->
addNeverDrop.call @
model = @never_drops.at(0)
@never_drops.remove model
@clock.tick(101)
view = $('#fixtures').find('select')
equal view.length, 0
test "changing a <select> will remove all <option>s with that value from other selects", ->
addNeverDrop.call @
addNeverDrop.call @
target_id = "1"
@clock.tick(101)
ok $('#fixtures').find('option[value='+target_id+']').length, 2
#change one of the selects
$('#fixtures').find('select:first').val(target_id).trigger('change')
@clock.tick(101)
#should only be one now
ok $('#fixtures').find('option[value='+target_id+']').length, 1
# target_id is now taken
ok @never_drops.takenValues.find (nd) -> nd.id == target_id
test "changing a <select> will add all <option>s with the previous value to other selects", ->
addNeverDrop.call @
addNeverDrop.call @
change_id = "1"
target_id = "3"
@clock.tick(101)
#should just have the selected one
ok $('#fixtures').find('option[value='+target_id+']').length, 1
#change one of the selects
$('#fixtures').find('select:first').val(change_id).trigger('change')
@clock.tick(101)
#should now be more than one
ok $('#fixtures').find('option[value='+target_id+']').length, 2
# target_id is now available
ok @never_drops.availableValues.find (nd) -> nd.id == target_id
test "resetting NeverDrops with a chosen assignment renders a <span>", ->
target_id = "1"
@never_drops.reset [
id: @never_drops.length
label_id: 'new'
chosen: 'Assignment 1'
chosen_id: target_id
]
@clock.tick(101)
ok $('#fixtures').find('span').length, 1
ok @never_drops.takenValues.find (nd) -> nd.id == target_id
test "when there are no availableValues, the add assignment link is not rendered", ->
addNeverDrop.call @
addNeverDrop.call @
addNeverDrop.call @
@clock.tick(101)
equal $('#fixtures').find('.add_never_drop').length, 0
test "when there are no takenValues, the add assignment says 'add an assignment'", ->
text = $('#fixtures').find('.add_never_drop').text()
equal $.trim(text), 'Add an assignment'
test "when there is at least one takenValue, the add assignment says 'add another assignment'", ->
addNeverDrop.call @
@clock.tick(101)
text = $('#fixtures').find('.add_never_drop').text()
equal $.trim(text), 'Add another assignment'
test 'allows adding never_drop items when canChangeDropRules is true', ->
notOk $('#fixtures').find('.add_never_drop').hasClass('disabled')
$('#fixtures').find('.add_never_drop').trigger('click')
@clock.tick(101)
equal @never_drops.length, 1
test 'allows removing never_drop items when canChangeDropRules is true', ->
addNeverDrop.call @
@clock.tick(101)
$('#fixtures').find('.remove_never_drop').trigger('click')
@clock.tick(101)
equal @never_drops.length, 0
test 'disables adding never_drop items when canChangeDropRules is false', ->
@view.canChangeDropRules = false
@view.render() # force re-render
ok $('#fixtures').find('.add_never_drop').hasClass('disabled')
$('#fixtures').find('.add_never_drop').trigger('click')
@clock.tick(101)
equal @never_drops.length, 0
test 'disables removing never_drop items when canChangeDropRules is false', ->
addNeverDrop.call @
@view.canChangeDropRules = false
@clock.tick(101)
ok $('#fixtures').find('.remove_never_drop').hasClass('disabled')
$('#fixtures').find('.remove_never_drop').trigger('click')
@clock.tick(101)
equal @never_drops.length, 1
test 'disables changing assignment options when canChangeDropRules is false', ->
addNeverDrop.call @
@view.canChangeDropRules = false
@clock.tick(101)
ok $('#fixtures').find('select:first').attr('readonly')
$('#fixtures').find('select:first').val('2').trigger('change')
@clock.tick(101)
notOk @never_drops.takenValues.find (nd) -> nd.id == '2'