forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignmentSettingsViewSpec.coffee
More file actions
186 lines (159 loc) · 7.55 KB
/
Copy pathAssignmentSettingsViewSpec.coffee
File metadata and controls
186 lines (159 loc) · 7.55 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
define [
'Backbone'
'compiled/collections/AssignmentGroupCollection'
'compiled/models/Course'
'compiled/models/AssignmentGroup'
'compiled/views/assignments/AssignmentSettingsView'
'compiled/views/assignments/AssignmentGroupWeightsView'
'jquery'
'helpers/fakeENV'
'helpers/jquery.simulate'
], (Backbone, AssignmentGroupCollection, Course, AssignmentGroup, AssignmentSettingsView, AssignmentGroupWeightsView, $, fakeENV) ->
group = (opts = {}) ->
new AssignmentGroup $.extend({group_weight: 50}, opts)
assignmentGroups = ->
@groups = new AssignmentGroupCollection([group(), group()])
createView = (opts = {}) ->
@course = new Course
apply_assignment_group_weights: opts.weighted
@course.urlRoot = "/courses/1" #without this it keeps throwing an error
view = new AssignmentSettingsView
model: @course
assignmentGroups: opts.assignmentGroups or assignmentGroups()
weightsView: AssignmentGroupWeightsView
userIsAdmin: opts.userIsAdmin
view.open()
view
QUnit.module 'AssignmentSettingsView',
setup: -> fakeENV.setup()
teardown: -> fakeENV.teardown()
test 'sets the checkbox to the right value on open', ->
view = createView(weighted: true)
ok view.$('#apply_assignment_group_weights').prop('checked')
view.remove()
view = createView(weighted: false)
ok !view.$('#apply_assignment_group_weights').prop('checked')
view.remove()
test 'shows the weights table when checked', ->
view = createView(weighted: true)
ok view.$('#ag_weights_wrapper').is(":visible")
view.remove()
test 'hides the weights table when clicked', ->
view = createView(weighted: true)
ok view.$('#ag_weights_wrapper').is(":visible")
view.$('#apply_assignment_group_weights').click()
ok view.$('#ag_weights_wrapper').not(":visible")
view.remove()
test 'calculates the total weight', ->
view = createView(weighted: true)
equal view.$('#percent_total').text(), '100%'
test 'changes the apply_assignment_group_weights flag', ->
view = createView(weighted: true)
view.$('#apply_assignment_group_weights').click()
attributes = view.getFormData()
equal(attributes.apply_assignment_group_weights, "0")
view.remove()
test 'onSaveSuccess triggers weightedToggle event with expected argument', ->
sandbox = sinon.sandbox.create()
stub1 = sandbox.stub()
view = createView(weighted: true)
view.on('weightedToggle', stub1)
view.onSaveSuccess()
equal stub1.callCount, 1
deepEqual stub1.getCall(0).args, [true]
view.remove()
stub2 = sandbox.stub()
view = createView(weighted: false)
view.on('weightedToggle', stub2)
view.onSaveSuccess()
equal stub2.callCount, 1
deepEqual stub2.getCall(0).args, [false]
view.remove()
sandbox.restore()
test 'saves group weights', ->
view = createView(weighted: true)
view.$(".ag-weights-tr:eq(0) .group_weight_value").val("20")
view.$(".ag-weights-tr:eq(1) .group_weight_value").val("80")
view.$("#update-assignment-settings").click()
equal view.assignmentGroups.first().get('group_weight'), 20
equal view.assignmentGroups.last().get('group_weight'), 80
view.remove()
QUnit.module 'AssignmentSettingsView with an assignment in a closed grading period',
setup: -> fakeENV.setup()
teardown: -> fakeENV.teardown()
test 'disables the checkbox', ->
closed_group = group(any_assignment_in_closed_grading_period: true)
groups = new AssignmentGroupCollection([group(), closed_group])
view = createView(weighted: true, assignmentGroups: groups)
ok view.$('#apply_assignment_group_weights').hasClass("disabled")
ok view.$('#ag_weights_wrapper').is(":visible")
ok view.$('#apply_assignment_group_weights').prop('checked')
view.$('#apply_assignment_group_weights').click()
ok view.$('#ag_weights_wrapper').is(":visible")
ok view.$('#apply_assignment_group_weights').prop('checked')
view.remove()
test 'does not disable the checkbox when the user is an admin', ->
closed_group = group(any_assignment_in_closed_grading_period: true)
groups = new AssignmentGroupCollection([group(), closed_group])
view = createView(weighted: true, assignmentGroups: groups, userIsAdmin: true)
notOk view.$('#apply_assignment_group_weights').hasClass("disabled")
ok view.$('#ag_weights_wrapper').is(":visible")
ok view.$('#apply_assignment_group_weights').prop('checked')
view.$('#apply_assignment_group_weights').click()
ok view.$('#ag_weights_wrapper').not(":visible")
notOk view.$('#apply_assignment_group_weights').prop('checked')
view.remove()
test 'does not change the apply_assignment_group_weights flag', ->
closed_group = group(any_assignment_in_closed_grading_period: true)
groups = new AssignmentGroupCollection([group(), closed_group])
view = createView(weighted: true, assignmentGroups: groups)
view.$('#apply_assignment_group_weights').click()
attributes = view.getFormData()
equal(attributes.apply_assignment_group_weights, "1")
view.remove()
test 'changes the apply_assignment_group_weights flag when the user is an admin', ->
closed_group = group(any_assignment_in_closed_grading_period: true)
groups = new AssignmentGroupCollection([group(), closed_group])
view = createView(weighted: true, assignmentGroups: groups, userIsAdmin: true)
view.$('#apply_assignment_group_weights').click()
attributes = view.getFormData()
equal(attributes.apply_assignment_group_weights, "0")
view.remove()
test 'disables the weight input fields in the table', ->
closed_group = group(any_assignment_in_closed_grading_period: true, group_weight: 35)
groups = new AssignmentGroupCollection([group(group_weight: 25), closed_group])
view = createView(weighted: true, assignmentGroups: groups)
ok view.$('.ag-weights-tr:eq(0) .group_weight_value').attr('readonly')
ok view.$('.ag-weights-tr:eq(1) .group_weight_value').attr('readonly')
test 'disables the Save and Cancel buttons', ->
closed_group = group(any_assignment_in_closed_grading_period: true)
groups = new AssignmentGroupCollection([group(), closed_group])
view = createView(weighted: true, assignmentGroups: groups)
ok view.$('#cancel-assignment-settings').hasClass('disabled')
ok view.$('#update-assignment-settings').hasClass('disabled')
view.remove()
test 'disables the Save and Cancel button handlers', ->
closed_group = group(any_assignment_in_closed_grading_period: true)
groups = new AssignmentGroupCollection([group(), closed_group])
view = createView(weighted: true, assignmentGroups: groups)
@spy view, 'saveFormData'
@spy view, 'cancel'
view.$('#cancel-assignment-settings').click()
view.$('#update-assignment-settings').click()
notOk view.saveFormData.called
notOk view.cancel.called
view.remove()
test 'does not allow NaN values to be saved', ->
closed_group = group(any_assignment_in_closed_grading_period: true)
groups = new AssignmentGroupCollection([group(), closed_group])
view = createView(weighted: true, assignmentGroups: groups)
weight_input = view.$el.find('.group_weight_value')[0]
$(weight_input).val('weight for it')
errors = view.validateFormData()
ok errors
equal _.keys(errors).length, 1
test 'calculates the total weight', ->
closed_group = group(any_assignment_in_closed_grading_period: true, group_weight: 35)
groups = new AssignmentGroupCollection([group(group_weight: 25), closed_group])
view = createView(weighted: true, assignmentGroups: groups)
equal view.$('#percent_total').text(), '60%'