forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWikiPageEditViewSpec.coffee
More file actions
336 lines (283 loc) · 9.67 KB
/
Copy pathWikiPageEditViewSpec.coffee
File metadata and controls
336 lines (283 loc) · 9.67 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
define [
'jquery'
'compiled/models/Assignment'
'compiled/models/WikiPage'
'compiled/views/wiki/WikiPageEditView'
'jsx/shared/rce/RichContentEditor',
'helpers/fixtures'
'helpers/editorUtils'
'helpers/fakeENV'
], ($, Assignment, WikiPage, WikiPageEditView, RichContentEditor, fixtures, editorUtils, fakeENV) ->
QUnit.module 'WikiPageEditView:Init',
setup: ->
@initSpy = sinon.spy(RichContentEditor, 'initSidebar')
teardown: ->
RichContentEditor.initSidebar.restore()
editorUtils.resetRCE()
$(window).off('beforeunload')
test 'init wiki sidebar during render', ->
wikiPageEditView = new WikiPageEditView
wikiPageEditView.render()
ok @initSpy.calledOnce, 'Called richContentEditor.initSidebar once'
test 'renders escaped angle brackets properly', ->
body = "<p><E></p>"
wikiPage = new WikiPage body: body
view = new WikiPageEditView model: wikiPage
view.render()
equal view.$wikiPageBody.val(), body
test 'conditional content is hidden when disabled', ->
view = new WikiPageEditView
WIKI_RIGHTS:
manage: true
view.render()
conditionalToggle = view.$el.find('#conditional_content')
equal conditionalToggle.length, 0, 'Toggle is hidden'
QUnit.module 'WikiPageEditView:ConditionalContent',
setup: ->
fakeENV.setup(CONDITIONAL_RELEASE_SERVICE_ENABLED: true)
teardown: ->
fakeENV.teardown()
test 'conditional content option hidden for insufficient rights', ->
view = new WikiPageEditView
WIKI_RIGHTS:
read: true
PAGE_RIGHTS:
read: true
update_content: true
view.render()
conditionalToggle = view.$el.find('#conditional_content')
equal conditionalToggle.length, 0, 'Toggle is hidden'
test 'conditional content option appears', ->
view = new WikiPageEditView
WIKI_RIGHTS:
manage: true
view.render()
conditionalToggle = view.$el.find('#conditional_content')
equal conditionalToggle.length, 1, 'Toggle is visible'
equal conditionalToggle.prop('checked'), false, 'Toggle is unchecked'
test 'conditional content option appears populated', ->
wikiPage = new WikiPage
set_assignment: true
assignment: new Assignment
set_assignment: true
view = new WikiPageEditView
model: wikiPage
WIKI_RIGHTS:
manage: true
view.render()
conditionalToggle = view.$el.find('#conditional_content')
equal conditionalToggle.prop('checked'), true, 'Toggle is checked'
test 'conditional content option does stuff', ->
wikiPage = new WikiPage
view = new WikiPageEditView
model: wikiPage
WIKI_RIGHTS:
manage: true
view.render()
conditionalToggle = view.$el.find('#conditional_content')
equal conditionalToggle.prop('checked'), false, 'Toggle is unchecked'
conditionalToggle.prop('checked', true)
assignment = view.getFormData().assignment
equal assignment.get('set_assignment'), '1', 'Sets assignment'
equal assignment.get('only_visible_to_overrides'), '1', 'Sets override visibility'
QUnit.module 'WikiPageEditView:UnsavedChanges',
setup: ->
fixtures.setup()
teardown: ->
fixtures.teardown()
editorUtils.resetRCE()
$(window).off('beforeunload')
setupUnsavedChangesTest = (test, attributes) ->
setup = ->
@wikiPage = new WikiPage attributes
@view = new WikiPageEditView model: @wikiPage
@view.$el.appendTo('#fixtures')
@view.render()
@titleInput = @view.$el.find('[name=title]')
@bodyInput = @view.$el.find('[name=body]')
# stub the 'is_dirty' RCE command. NOTE: this stubs only the editorBox
# version with the feature flag off. force these specs to start failing
# when run with the feature flag on, at which point this will need to be
# updated to stub remoteEditor instead
ok !@bodyInput.data('remoteEditor')
ok @bodyInput.data('rich_text')
model = @wikiPage
bodyInput = @bodyInput
editorBox = bodyInput.editorBox
@stub $.fn, 'editorBox', (options) ->
if options == 'is_dirty'
return bodyInput.val() != model.get('body')
else
editorBox.apply(this, arguments)
setup.call(test, attributes)
test 'check for unsaved changes on new model', ->
setupUnsavedChangesTest(this, title: '', body: '')
@titleInput.val('blah')
ok @view.getFormData().title == 'blah', "blah"
ok @view.hasUnsavedChanges(), 'Changed title'
@titleInput.val('')
ok !@view.hasUnsavedChanges(), 'Unchanged title'
@bodyInput.val('bloo')
ok @view.hasUnsavedChanges(), 'Changed body'
@bodyInput.val('')
ok !@view.hasUnsavedChanges(), 'Unchanged body'
test 'check for unsaved changes on model with data', ->
setupUnsavedChangesTest(this, title: 'nooo', body: 'blargh')
ok !@view.hasUnsavedChanges(), 'No changes'
@titleInput.val('')
ok @view.hasUnsavedChanges(), 'Changed title'
@titleInput.val('nooo')
ok !@view.hasUnsavedChanges(), 'Unchanged title'
@bodyInput.val('')
ok @view.hasUnsavedChanges(), 'Changed body'
test 'warn on cancel if unsaved changes', ->
setupUnsavedChangesTest(this, title: 'nooo', body: 'blargh')
@spy(@view, 'trigger')
@stub(window, 'confirm')
@titleInput.val('mwhaha')
window.confirm.returns(false)
@view.$el.find('.cancel').click()
ok window.confirm.calledOnce, 'Warn on cancel'
ok !@view.trigger.calledWith('cancel'), "Don't trigger cancel if declined"
window.confirm.reset()
@view.trigger.reset()
window.confirm.returns(true)
@view.$el.find('.cancel').click()
ok window.confirm.calledOnce, 'Warn on cancel again'
ok @view.trigger.calledWith('cancel'), 'Do trigger cancel if accepted'
test 'warn on leaving if unsaved changes', ->
setupUnsavedChangesTest(this, title: 'nooo', body: 'blargh')
strictEqual @view.onUnload({}), undefined, "No warning if not changed"
@titleInput.val('mwhaha')
ok @view.onUnload({}) isnt undefined, "Returns warning if changed"
QUnit.module 'WikiPageEditView:Validate'
test 'validation of the title is only performed if the title is present', ->
view = new WikiPageEditView
errors = view.validateFormData body: 'blah'
strictEqual errors['title'], undefined, 'no error when title is omitted'
errors = view.validateFormData title: 'blah', body: 'blah'
strictEqual errors['title'], undefined, 'no error when title is present'
errors = view.validateFormData title: '', body: 'blah'
ok errors['title'], 'error when title is present, but blank'
ok errors['title'][0].message, 'error message when title is present, but blank'
QUnit.module 'WikiPageEditView:JSON'
testRights = (subject, options) ->
test "#{subject}", ->
model = new WikiPage options.attributes, contextAssetString: options.contextAssetString
view = new WikiPageEditView
model: model
WIKI_RIGHTS: options.WIKI_RIGHTS
PAGE_RIGHTS: options.PAGE_RIGHTS
json = view.toJSON()
if options.IS
for key of options.IS
strictEqual json.IS[key], options.IS[key], "IS.#{key}"
if options.CAN
for key of options.CAN
strictEqual json.CAN[key], options.CAN[key], "CAN.#{key}"
if options.SHOW
for key of options.SHOW
strictEqual json.SHOW[key], options.SHOW[key], "SHOW.#{key}"
testRights 'IS (teacher)',
attributes:
editing_roles: 'teachers'
IS:
TEACHER_ROLE: true
STUDENT_ROLE: false
MEMBER_ROLE: false
ANYONE_ROLE: false
testRights 'IS (student)',
attributes:
editing_roles: 'teachers,students'
IS:
TEACHER_ROLE: false
STUDENT_ROLE: true
MEMBER_ROLE: false
ANYONE_ROLE: false
testRights 'IS (members)',
attributes:
editing_roles: 'members'
IS:
TEACHER_ROLE: false
STUDENT_ROLE: false
MEMBER_ROLE: true
ANYONE_ROLE: false
testRights 'IS (course anyone)',
attributes:
editing_roles: 'teachers,students,public'
IS:
TEACHER_ROLE: false
STUDENT_ROLE: false
MEMBER_ROLE: false
ANYONE_ROLE: true
testRights 'IS (group anyone)',
attributes:
editing_roles: 'members,public'
IS:
TEACHER_ROLE: false
STUDENT_ROLE: false
MEMBER_ROLE: false
ANYONE_ROLE: true
testRights 'IS (null)',
IS:
TEACHER_ROLE: true
STUDENT_ROLE: false
MEMBER_ROLE: false
ANYONE_ROLE: false
testRights 'CAN/SHOW (manage course)',
contextAssetString: 'course_73'
attributes:
url: 'test'
WIKI_RIGHTS:
manage: true
publish_page: true
PAGE_RIGHTS:
read: true
update: true
delete: true
CAN:
PUBLISH: true
DELETE: true
EDIT_TITLE: true
EDIT_ROLES: true
SHOW:
COURSE_ROLES: true
testRights 'CAN/SHOW (manage group)',
contextAssetString: 'group_73'
WIKI_RIGHTS:
manage: true
PAGE_RIGHTS:
read: true
CAN:
PUBLISH: false
DELETE: false
EDIT_TITLE: true # new record
EDIT_ROLES: true
SHOW:
COURSE_ROLES: false
testRights 'CAN/SHOW (update_content)',
contextAssetString: 'course_73'
attributes:
url: 'test'
WIKI_RIGHTS:
read: true
PAGE_RIGHTS:
read: true
update_content: true
CAN:
PUBLISH: false
DELETE: false
EDIT_TITLE: false
EDIT_ROLES: false
#SHOW:
#COURSE_ROLES: false # intentionally omitted as EDIT_ROLES === false
testRights 'CAN/SHOW (null)',
attributes:
url: 'test'
CAN:
PUBLISH: false
DELETE: false
EDIT_TITLE: false
EDIT_ROLES: false
#SHOW:
#COURSE_ROLES: false # intentionally omitted as EDIT_ROLES === false