forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditViewSpec.coffee
More file actions
295 lines (256 loc) · 11.3 KB
/
Copy pathEditViewSpec.coffee
File metadata and controls
295 lines (256 loc) · 11.3 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
define [
'jquery'
'underscore'
'compiled/collections/SectionCollection'
'compiled/models/Assignment'
'compiled/models/DueDateList'
'compiled/models/Section'
'compiled/models/DiscussionTopic'
'compiled/models/Announcement'
'compiled/views/assignments/DueDateOverride'
'compiled/views/DiscussionTopics/EditView'
'compiled/collections/AssignmentGroupCollection'
'compiled/views/assignments/GroupCategorySelector'
'helpers/fakeENV'
'jsx/shared/rce/RichContentEditor'
'helpers/jquery.simulate'
], (
$,
_,
SectionCollection,
Assignment,
DueDateList,
Section,
DiscussionTopic,
Announcement,
DueDateOverrideView,
EditView,
AssignmentGroupCollection,
GroupCategorySelector,
fakeENV,
RichContentEditor) ->
editView = (opts = {}, discussOpts = {}) ->
modelClass = if opts.isAnnouncement then Announcement else DiscussionTopic
if opts.withAssignment
assignmentOpts = _.extend {}, opts.assignmentOpts,
name: 'Test Assignment'
assignment_overrides: []
discussOpts.assignment = assignmentOpts
discussion = new modelClass(discussOpts, parse: true)
assignment = discussion.get('assignment')
sectionList = new SectionCollection [Section.defaultDueDateSection()]
dueDateList = new DueDateList assignment.get('assignment_overrides'), sectionList, assignment
app = new EditView
model: discussion
permissions: opts.permissions || {}
views:
'js-assignment-overrides': new DueDateOverrideView
model: dueDateList
views: {}
(app.assignmentGroupCollection = new AssignmentGroupCollection).contextAssetString = ENV.context_asset_string
app.render()
nameLengthHelper = (view, length, maxNameLengthRequiredForAccount, maxNameLength, postToSis) ->
ENV.MAX_NAME_LENGTH_REQUIRED_FOR_ACCOUNT = maxNameLengthRequiredForAccount
ENV.MAX_NAME_LENGTH = maxNameLength
ENV.IS_LARGE_ROSTER = true
ENV.CONDITIONAL_RELEASE_SERVICE_ENABLED = false
title = 'a'.repeat(length)
assignment = view.assignment
assignment.attributes.post_to_sis = postToSis
return view.validateBeforeSave({title: title, set_assignment: '1', assignment: assignment}, [])
QUnit.module 'EditView',
setup: ->
fakeENV.setup()
teardown: ->
fakeENV.teardown()
editView: ->
editView.apply(this, arguments)
test 'renders', ->
view = @editView()
ok view
test 'tells RCE to manage the parent', ->
lne = @stub(RichContentEditor, 'loadNewEditor')
EditView.prototype.loadNewEditor.call()
ok lne.firstCall.args[1].manageParent, 'manageParent flag should be set'
test 'shows error message on assignment point change with submissions', ->
view = @editView
withAssignment: true,
assignmentOpts: { has_submitted_submissions: true }
view.renderGroupCategoryOptions()
ok view.$el.find('#discussion_point_change_warning'), 'rendered change warning'
view.$el.find('#discussion_topic_assignment_points_possible').val(1)
view.$el.find('#discussion_topic_assignment_points_possible').trigger("change")
equal view.$el.find('#discussion_point_change_warning').attr('aria-expanded'), "true", 'change warning aria-expanded true'
view.$el.find('#discussion_topic_assignment_points_possible').val(0)
view.$el.find('#discussion_topic_assignment_points_possible').trigger("change")
equal view.$el.find('#discussion_point_change_warning').attr('aria-expanded'), "false", 'change warning aria-expanded false'
test 'hides the published icon for announcements', ->
view = @editView(isAnnouncement: true)
equal view.$el.find('.published-status').length, 0
test 'validates the group category for non-assignment discussions', ->
clock = sinon.useFakeTimers()
view = @editView()
clock.tick(1)
data = { group_category_id: 'blank' }
errors = view.validateBeforeSave(data, [])
ok errors["newGroupCategory"][0]["message"]
clock.restore()
test 'does not render #podcast_has_student_posts_container for non-course contexts', ->
# not a course context because we are not passing contextType into the
# EditView constructor
view = @editView({ withAssignment: true, permissions: { CAN_MODERATE: true } })
equal view.$el.find('#podcast_enabled').length, 1
equal view.$el.find('#podcast_has_student_posts_container').length, 0
test 'routes to discussion details normally', ->
view = @editView({}, { html_url: 'http://foo' })
equal view.locationAfterSave({}), 'http://foo'
test 'routes to return_to', ->
view = @editView({}, { html_url: 'http://foo' })
equal view.locationAfterSave({ return_to: 'http://bar' }), 'http://bar'
test 'cancels to env normally', ->
ENV.CANCEL_TO = 'http://foo'
view = @editView()
equal view.locationAfterCancel({}), 'http://foo'
test 'cancels to return_to', ->
ENV.CANCEL_TO = 'http://foo'
view = @editView()
equal view.locationAfterCancel({ return_to: 'http://bar' }), 'http://bar'
QUnit.module 'EditView - ConditionalRelease',
setup: ->
fakeENV.setup()
ENV.CONDITIONAL_RELEASE_SERVICE_ENABLED = true
ENV.CONDITIONAL_RELEASE_ENV = { assignment: { id: 1 }, jwt: 'foo' }
$(document).on 'submit', -> false
teardown: ->
fakeENV.teardown()
$(document).off 'submit'
editView: ->
editView.apply(this, arguments)
test 'does not show conditional release tab when feature not enabled', ->
ENV.CONDITIONAL_RELEASE_SERVICE_ENABLED = false
view = @editView()
equal view.$el.find('#mastery-paths-editor').length, 0
equal view.$el.find('#discussion-edit-view').hasClass('ui-tabs'), false
test 'shows disabled conditional release tab when feature enabled, but not assignment', ->
view = @editView()
view.renderTabs()
view.loadConditionalRelease()
equal view.$el.find('#mastery-paths-editor').length, 1
equal view.$discussionEditView.hasClass('ui-tabs'), true
equal view.$discussionEditView.tabs("option", "disabled"), true
test 'shows enabled conditional release tab when feature enabled, and assignment', ->
view = @editView({ withAssignment: true })
view.renderTabs()
view.loadConditionalRelease()
equal view.$el.find('#mastery-paths-editor').length, 1
equal view.$discussionEditView.hasClass('ui-tabs'), true
equal view.$discussionEditView.tabs("option", "disabled"), false
test 'enables conditional release tab when changed to assignment', ->
view = @editView()
view.loadConditionalRelease()
view.renderTabs()
equal view.$discussionEditView.tabs("option", "disabled"), true
view.$useForGrading.prop('checked', true)
view.$useForGrading.trigger('change')
equal view.$discussionEditView.tabs("option", "disabled"), false
test 'disables conditional release tab when changed from assignment', ->
view = @editView({ withAssignment: true })
view.loadConditionalRelease()
view.renderTabs()
equal view.$discussionEditView.tabs("option", "disabled"), false
view.$useForGrading.prop('checked', false)
view.$useForGrading.trigger('change')
equal view.$discussionEditView.tabs("option", "disabled"), true
test 'renders conditional release tab content', ->
view = @editView({ withAssignment: true })
view.loadConditionalRelease()
equal 1, view.$conditionalReleaseTarget.children().size()
test "has an error when a title is 257 chars", ->
view = @editView({ withAssignment: true})
errors = nameLengthHelper(view, 257, false, 30, '1')
equal errors["title"][0]["message"], "Title is too long, must be under 257 characters"
test "allows dicussion to save when a title is 256 chars, MAX_NAME_LENGTH is not required and post_to_sis is true", ->
view = @editView({ withAssignment: true})
errors = nameLengthHelper(view, 256, false, 30, '1')
equal errors.length, 0
test "has an error when a title > MAX_NAME_LENGTH chars if MAX_NAME_LENGTH is custom, required and post_to_sis is true", ->
view = @editView({ withAssignment: true})
errors = nameLengthHelper(view, 40, true, 30, '1')
equal errors["title"][0]["message"], "Title is too long, must be under 31 characters"
test "allows discussion to save when title > MAX_NAME_LENGTH chars if MAX_NAME_LENGTH is custom, required and post_to_sis is false", ->
view = @editView({ withAssignment: true})
errors = nameLengthHelper(view, 40, true, 30, '0')
equal errors.length, 0
test "allows discussion to save when title < MAX_NAME_LENGTH chars if MAX_NAME_LENGTH is custom, required and post_to_sis is true", ->
view = @editView({ withAssignment: true})
errors = nameLengthHelper(view, 30, true, 40, '1')
equal errors.length, 0
test 'conditional release editor is updated on tab change', ->
view = @editView({ withAssignment: true })
view.renderTabs()
view.renderGroupCategoryOptions()
view.loadConditionalRelease()
stub = @stub(view.conditionalReleaseEditor, 'updateAssignment')
view.$discussionEditView.tabs("option", "active", 1)
ok stub.calledOnce
stub.reset()
view.$discussionEditView.tabs("option", "active", 0)
view.onChange()
view.$discussionEditView.tabs("option", "active", 1)
ok stub.calledOnce
test 'validates conditional release', (assert) ->
resolved = assert.async()
view = @editView({ withAssignment: true })
_.defer =>
stub = @stub(view.conditionalReleaseEditor, 'validateBeforeSave').returns 'foo'
errors = view.validateBeforeSave(view.getFormData(), {})
ok errors['conditional_release'] == 'foo'
resolved()
test 'calls save in conditional release', (assert) ->
resolved = assert.async()
view = @editView({ withAssignment: true })
_.defer =>
superPromise = $.Deferred().resolve({}).promise()
crPromise = $.Deferred().resolve({}).promise()
mockSuper = sinon.mock(EditView.__super__)
mockSuper.expects('saveFormData').returns superPromise
stub = @stub(view.conditionalReleaseEditor, 'save').returns crPromise
finalPromise = view.saveFormData()
finalPromise.then ->
mockSuper.verify()
ok stub.calledOnce
resolved()
test 'does not call conditional release save for an announcement', (assert) ->
resolved = assert.async()
view = @editView({ isAnnouncement: true })
_.defer =>
superPromise = $.Deferred().resolve({}).promise()
mockSuper = sinon.mock(EditView.__super__)
mockSuper.expects('saveFormData').returns superPromise
savePromise = view.saveFormData()
savePromise.then ->
mockSuper.verify()
notOk view.conditionalReleaseEditor
resolved()
test 'switches to conditional tab if save error contains conditional release error', (assert) ->
resolved = assert.async()
view = @editView({ withAssignment: true })
_.defer =>
view.$discussionEditView.tabs('option', 'active', 0)
view.showErrors({
foo: {type: 'bar'},
conditional_release: {type: 'bat'}
})
equal 1, view.$discussionEditView.tabs('option', 'active')
resolved()
test 'switches to details tab if save error does not contain conditional release error', (assert) ->
resolved = assert.async()
view = @editView({ withAssignment: true })
_.defer =>
view.$discussionEditView.tabs('option', 'active', 1)
view.showErrors({
foo: {type: 'bar'},
baz: {type: 'bat'}
})
equal 0, view.$discussionEditView.tabs('option', 'active')
resolved()