forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveDialogViewSpec.coffee
More file actions
193 lines (155 loc) · 5.69 KB
/
Copy pathMoveDialogViewSpec.coffee
File metadata and controls
193 lines (155 loc) · 5.69 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
define [
'underscore'
'jquery'
'Backbone'
'compiled/views/MoveDialogView'
'helpers/jquery.simulate'
], (_, $, Backbone, MoveDialogView) ->
server = null
class AssignmentStub extends Backbone.Model
name: -> @get('name')
toView: =>
name: @get('name')
id: @id
class Assignments extends Backbone.Collection
model: AssignmentStub
comparator: 'position'
class AssignmentGroups extends Backbone.Collection
comparator: 'position'
genSetup = ->
@assignments_1 = new Assignments((id: "#{i}", name: "Assignment #{i}") for i in [1..3])
@assignments_2 = new Assignments((id: "#{i}", name: "Assignment #{i}") for i in [5..10])
@assignmentGroups = new AssignmentGroups [
new Backbone.Model assignments: @assignments_1, id: "1"
new Backbone.Model assignments: @assignments_2, id: "2"
]
createDialog = (hasParentCollection, saveURL) ->
@model = @assignments_1.at(0)
@moveDialog = new MoveDialogView
model: @model
nested: true if hasParentCollection
parentCollection: @assignmentGroups if hasParentCollection
childKey: 'assignments'
parentKey: 'assignment_group_id'
saveURL: saveURL
QUnit.module 'MoveDialogView',
setup: ->
genSetup.call @
@update_spy = @spy MoveDialogView.prototype, 'updateListView'
createDialog.call @, true
teardown: ->
@moveDialog.remove()
test 'child views don\'t exist on init', ->
ok !@moveDialog.listView and !@moveDialog.parentListView
test '#getFormData returns ids', ->
@moveDialog.open()
expected = @assignments_1.pluck('id')
_.each @moveDialog.getFormData(), (val, ind) ->
equal val, expected[ind]
test '#getFormData adds model id at end if value is "last"', ->
@moveDialog.open()
expected = _.pluck @assignments_1.slice(1), 'id'
expected.push @assignments_1.at(0).id
lastSelect = @moveDialog.$('select').last()
lastSelect.find('option').last().prop('selected', true)
lastSelect.trigger('change')
_.each @moveDialog.getFormData(), (val, ind) ->
equal val, expected[ind]
test 'two selects are attached with #attachChildViews', ->
@moveDialog.open()
equal @moveDialog.$('select').length, 2
test 'changing the value of the parentList select sets the collection on the listView', ->
@moveDialog.open()
initialHTML = @moveDialog.$('select').last().html()
firstSelect = @moveDialog.$('select').first()
firstSelect.find('option').last().prop('selected', true)
firstSelect.trigger('change')
ok @update_spy.calledOnce
notEqual @moveDialog.$('select').last().html(), initialHTML
options = @moveDialog.$('select').last().find('option')
# each option value corresponds to an item id
_.each options, (ele, ind) =>
{value} = ele
if value != "last"
equal value, @assignments_2.at(ind).id
QUnit.module 'MoveDialogView without a parentCollection',
setup: ->
genSetup.call @
createDialog.call @, false
teardown: ->
@moveDialog.remove()
test 'only one select is attached with #attachChildViews', ->
@moveDialog.open()
equal @moveDialog.$('select').length, 1
QUnit.module 'MoveDialogView save and save success',
setup: ->
genSetup.call @
server = sinon.fakeServer.create()
@sort_spy = @spy Assignments.prototype, 'sort'
@reset_spy = @spy Assignments.prototype, 'reset'
teardown: ->
_.each server.requests, -> server.respond()
server.restore()
@moveDialog.remove()
test '@saveURL as a string', ->
saveURL = "/test"
createDialog.call @, true, saveURL
@moveDialog.open()
@moveDialog.submit()
equal server.requests[0].url, saveURL
test '@saveURL as a function', ->
# give it something that will be
# defined on the @moveDialog
saveURL = ->
"/test/#{@childKey}"
createDialog.call @, true, saveURL
@moveDialog.open()
@moveDialog.submit()
equal server.requests[0].url, saveURL.call @moveDialog
test 'collection #sort and #reset are called on success', ->
saveURL = "/test"
createDialog.call @, true, saveURL
@moveDialog.open()
@moveDialog.submit()
server.respond 'POST', '/test', [
200
'Content-Type': 'application/json'
JSON.stringify [2,1,3]
]
ok @sort_spy.called
ok @reset_spy.called
test 'parentKey value on the model is updated on save success when the model moves collections', ->
saveURL = "/test"
createDialog.call @, true, saveURL
# we don't initially have a parentKey relationship on the stub model
ok !@model.has('assignment_group_id')
@moveDialog.open()
# need to change the parentCollection selector
firstSelect = @moveDialog.$('select').first()
firstSelect.find('option').last().prop('selected', true)
firstSelect.trigger('change')
@moveDialog.submit()
server.respond 'POST', '/test', [
200
'Content-Type': 'application/json'
JSON.stringify [2,1,3]
]
ok @model.has('assignment_group_id')
test 'parentKey value on the model is updated on save success and is a string', ->
saveURL = "/test"
createDialog.call @, true, saveURL
# we don't initially have a parentKey relationship on the stub model
ok !@model.has('assignment_group_id')
@moveDialog.open()
# need to change the parentCollection selector
firstSelect = @moveDialog.$('select').first()
firstSelect.find('option').last().prop('selected', true)
firstSelect.trigger('change')
@moveDialog.submit()
server.respond 'POST', '/test', [
200
'Content-Type': 'application/json'
JSON.stringify [2,1,3]
]
ok @model.has('assignment_group_id')
ok typeof @model.get('assignment_group_id') == 'string'