Skip to content

Commit 052ca8c

Browse files
author
Stewie (Nicholas Stewart)
committed
Refactor coffeescript and specs
Refs: SIS-2715 Test Plan: QA will review the functionality in the final commit. Change-Id: I6e1fbb1fc11cff2f711943e0f2bdfe2a33e811b3 Reviewed-on: https://gerrit.instructure.com/102914 Tested-by: Jenkins Reviewed-by: Nick Houle <nhoule@instructure.com> Product-Review: Stewie aka Nicholas Stewart <nstewart@instructure.com> QA-Review: Stewie aka Nicholas Stewart <nstewart@instructure.com>
1 parent b1bd99a commit 052ca8c

2 files changed

Lines changed: 59 additions & 75 deletions

File tree

app/coffeescripts/views/DiscussionTopics/EditView.coffee

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,29 @@ define [
2121
'compiled/util/deparam'
2222
'compiled/jquery.rails_flash_notifications' #flashMessage
2323
'jsx/shared/helpers/numberHelper'
24-
], (I18n, ValidatedFormView, AssignmentGroupSelector, GradingTypeSelector,
25-
GroupCategorySelector, PeerReviewsSelector, PostToSisSelector, _, template, RichContentEditor,
26-
htmlEscape, DiscussionTopic, Announcement, Assignment, $, preventDefault, MissingDateDialog, KeyboardShortcuts,
27-
ConditionalRelease, deparam, flashMessage, numberHelper) ->
24+
], (
25+
I18n,
26+
ValidatedFormView,
27+
AssignmentGroupSelector,
28+
GradingTypeSelector,
29+
GroupCategorySelector,
30+
PeerReviewsSelector,
31+
PostToSisSelector,
32+
_,
33+
template,
34+
RichContentEditor,
35+
htmlEscape,
36+
DiscussionTopic,
37+
Announcement,
38+
Assignment,
39+
$,
40+
preventDefault,
41+
MissingDateDialog,
42+
KeyboardShortcuts,
43+
ConditionalRelease,
44+
deparam,
45+
flashMessage,
46+
numberHelper) ->
2847

2948
RichContentEditor.preloadRemoteModule()
3049

@@ -361,10 +380,10 @@ ConditionalRelease, deparam, flashMessage, numberHelper) ->
361380
_validateTitle: (data, errors) =>
362381
max_name_length = 256
363382
if data.assignment.attributes.post_to_sis == '1' && ENV.MAX_NAME_LENGTH_REQUIRED_FOR_ACCOUNT == true
364-
max_name_length = ENV.MAX_NAME_LENGTH + 1
383+
max_name_length = ENV.MAX_NAME_LENGTH
365384
if $.trim(data.title.toString()).length > max_name_length
366385
errors["title"] = [
367-
message: I18n.t "Title is too long, must be under %{length} characters", length: max_name_length
386+
message: I18n.t("Title is too long, must be under %{length} characters", length: (max_name_length + 1))
368387
]
369388
errors
370389

spec/coffeescripts/views/DiscussionTopics/EditViewSpec.coffee

Lines changed: 34 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,21 @@ define [
1414
'helpers/fakeENV'
1515
'jsx/shared/rce/RichContentEditor'
1616
'helpers/jquery.simulate'
17-
], ($, _, SectionCollection, Assignment, DueDateList, Section, DiscussionTopic,
18-
Announcement, DueDateOverrideView, EditView, AssignmentGroupCollection,
19-
GroupCategorySelector, fakeENV, RichContentEditor) ->
17+
], (
18+
$,
19+
_,
20+
SectionCollection,
21+
Assignment,
22+
DueDateList,
23+
Section,
24+
DiscussionTopic,
25+
Announcement,
26+
DueDateOverrideView,
27+
EditView,
28+
AssignmentGroupCollection,
29+
GroupCategorySelector,
30+
fakeENV,
31+
RichContentEditor) ->
2032

2133
editView = (opts = {}, discussOpts = {}) ->
2234
modelClass = if opts.isAnnouncement then Announcement else DiscussionTopic
@@ -42,6 +54,16 @@ GroupCategorySelector, fakeENV, RichContentEditor) ->
4254
(app.assignmentGroupCollection = new AssignmentGroupCollection).contextAssetString = ENV.context_asset_string
4355
app.render()
4456

57+
nameLengthHelper = (view, length, maxNameLengthRequiredForAccount, maxNameLength, postToSis) ->
58+
ENV.MAX_NAME_LENGTH_REQUIRED_FOR_ACCOUNT = maxNameLengthRequiredForAccount
59+
ENV.MAX_NAME_LENGTH = maxNameLength
60+
ENV.IS_LARGE_ROSTER = true
61+
ENV.CONDITIONAL_RELEASE_SERVICE_ENABLED = false
62+
title = 'a'.repeat(length)
63+
assignment = view.assignment
64+
assignment.attributes.post_to_sis = postToSis
65+
return view.validateBeforeSave({title: title, set_assignment: '1', assignment: assignment}, [])
66+
4567
QUnit.module 'EditView',
4668
setup: ->
4769
fakeENV.setup()
@@ -169,86 +191,29 @@ GroupCategorySelector, fakeENV, RichContentEditor) ->
169191
view.loadConditionalRelease()
170192
equal 1, view.$conditionalReleaseTarget.children().size()
171193

172-
test "has an error when a title > 255 chars", ->
173-
view = @editView({ withAssignment: true})
174-
assignment = view.assignment
175-
l1 = 'aaaaaaaaaa'
176-
l2 = l1 + l1 + l1 + l1 + l1 + l1
177-
l3 = l2 + l2 + l2 + l2 + l2 + l2
178-
179-
ENV.IS_LARGE_ROSTER = true
180-
ENV.CONDITIONAL_RELEASE_SERVICE_ENABLED = false
181-
errors = view.validateBeforeSave({title: l3, set_assignment: '1', assignment: assignment}, [])
182-
183-
equal errors["title"][0]["message"], "Title is too long, must be under 256 characters"
184-
185-
test "allows dicussion to save when a title < 255 chars, MAX_NAME_LENGTH is not required and post_to_sis is true", ->
194+
test "has an error when a title is 257 chars", ->
186195
view = @editView({ withAssignment: true})
187-
assignment = view.assignment
188-
assignment.attributes.post_to_sis = '1'
189-
l1 = 'aaaaaaaaaa'
196+
errors = nameLengthHelper(view, 257, false, 30, '0')
197+
equal errors["title"][0]["message"], "Title is too long, must be under 257 characters"
190198

191-
ENV.MAX_NAME_LENGTH_REQUIRED_FOR_ACCOUNT = false
192-
ENV.IS_LARGE_ROSTER = true
193-
ENV.CONDITIONAL_RELEASE_SERVICE_ENABLED = false
194-
errors = view.validateBeforeSave({title: l1, set_assignment: '1', assignment: assignment}, [])
195-
equal errors.length, 0
196-
197-
test "allows dicussion to save when a title < 255 chars, MAX_NAME_LENGTH is not required and post_to_sis is false", ->
199+
test "allows dicussion to save when a title is 256 chars, MAX_NAME_LENGTH is not required and post_to_sis is true", ->
198200
view = @editView({ withAssignment: true})
199-
assignment = view.assignment
200-
assignment.attributes.post_to_sis = '1'
201-
l1 = 'aaaaaaaaaa'
202-
203-
ENV.MAX_NAME_LENGTH_REQUIRED_FOR_ACCOUNT = false
204-
ENV.IS_LARGE_ROSTER = true
205-
ENV.CONDITIONAL_RELEASE_SERVICE_ENABLED = false
206-
errors = view.validateBeforeSave({title: l1, set_assignment: '1', assignment: assignment}, [])
201+
errors = nameLengthHelper(view, 256, false, 30, '1')
207202
equal errors.length, 0
208203

209204
test "has an error when a title > MAX_NAME_LENGTH chars if MAX_NAME_LENGTH is custom, required and post_to_sis is true", ->
210205
view = @editView({ withAssignment: true})
211-
assignment = view.assignment
212-
assignment.attributes.post_to_sis = '1'
213-
l1 = 'aaaaaaaaaa'
214-
215-
ENV.MAX_NAME_LENGTH = 5
216-
ENV.MAX_NAME_LENGTH_REQUIRED_FOR_ACCOUNT = true
217-
ENV.IS_LARGE_ROSTER = true
218-
ENV.CONDITIONAL_RELEASE_SERVICE_ENABLED = false
219-
errors = view.validateBeforeSave({title: l1, set_assignment: '1', assignment: assignment}, [])
220-
221-
equal errors["title"][0]["message"], "Title is too long, must be under #{ENV.MAX_NAME_LENGTH + 1} characters"
206+
errors = nameLengthHelper(view, 40, true, 30, '1')
207+
equal errors["title"][0]["message"], "Title is too long, must be under 31 characters"
222208

223209
test "allows discussion to save when title > MAX_NAME_LENGTH chars if MAX_NAME_LENGTH is custom, required and post_to_sis is false", ->
224210
view = @editView({ withAssignment: true})
225-
assignment = view.assignment
226-
assignment.attributes.post_to_sis = '0'
227-
l1 = 'aaaaaaaaaa'
228-
l2 = l1 + l1 + l1 + l1 + l1 + l1
229-
l3 = l2 + l2 + l2 + l2 + l2 + l2
230-
231-
ENV.MAX_NAME_LENGTH = 5
232-
ENV.MAX_NAME_LENGTH_REQUIRED_FOR_ACCOUNT = true
233-
ENV.IS_LARGE_ROSTER = true
234-
ENV.CONDITIONAL_RELEASE_SERVICE_ENABLED = false
235-
errors = view.validateBeforeSave({title: l3, set_assignment: '1', assignment: assignment}, [])
236-
211+
errors = nameLengthHelper(view, 40, true, 30, '0')
237212
equal errors.length, 0
238213

239-
240214
test "allows discussion to save when title < MAX_NAME_LENGTH chars if MAX_NAME_LENGTH is custom, required and post_to_sis is true", ->
241215
view = @editView({ withAssignment: true})
242-
assignment = view.assignment
243-
assignment.attributes.post_to_sis = '1'
244-
l1 = 'aaaaaaaaaa'
245-
246-
ENV.MAX_NAME_LENGTH = 20
247-
ENV.MAX_NAME_LENGTH_REQUIRED_FOR_ACCOUNT = true
248-
ENV.IS_LARGE_ROSTER = true
249-
ENV.CONDITIONAL_RELEASE_SERVICE_ENABLED = false
250-
errors = view.validateBeforeSave({title: l1, set_assignment: '1', assignment: assignment}, [])
251-
216+
errors = nameLengthHelper(view, 30, true, 40, '1')
252217
equal errors.length, 0
253218

254219
test 'conditional release editor is updated on tab change', ->

0 commit comments

Comments
 (0)