forked from alexrk/mercury-rails
/
dialog_spec.js.coffee
274 lines (192 loc) · 8.85 KB
/
dialog_spec.js.coffee
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
require '/assets/mercury.js'
describe "Mercury.Dialog", ->
template 'mercury/dialog.html'
beforeEach ->
$.fx.off = true
afterEach ->
@dialog = null
delete(@dialog)
describe "constructor", ->
beforeEach ->
@buildSpy = spyOn(Mercury.Dialog.prototype, 'build').andCallFake(=>)
@bindEventsSpy = spyOn(Mercury.Dialog.prototype, 'bindEvents').andCallFake(=>)
@preloadSpy = spyOn(Mercury.Dialog.prototype, 'preload').andCallFake(=>)
it "expects a url and name", ->
@dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo')
expect(@dialog.url).toEqual('/evergreen/responses/blank.html')
expect(@dialog.name).toEqual('foo')
it "accepts options", ->
@dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo', {for: 'something', foo: 'bar'})
expect(@dialog.options).toEqual({for: 'something', foo: 'bar'})
it "calls build", ->
@dialog = new Mercury.Dialog()
expect(@buildSpy.callCount).toEqual(1)
it "calls bindEvents", ->
@dialog = new Mercury.Dialog()
expect(@bindEventsSpy.callCount).toEqual(1)
it "preloads if configured", ->
@dialog = new Mercury.Dialog()
expect(@preloadSpy.callCount).toEqual(1)
describe "#build", ->
beforeEach ->
@bindEventsSpy = spyOn(Mercury.Dialog.prototype, 'bindEvents').andCallFake(=>)
@preloadSpy = spyOn(Mercury.Dialog.prototype, 'preload').andCallFake(=>)
it "builds an element", ->
@dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo', {appendTo: '#test'})
html = $('<div>').html(@dialog.element).html()
expect(html).toContain('class="mercury-dialog mercury-foo-dialog loading"')
expect(html).toContain('style="display:none"')
it "appends to any element", ->
@dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo', {appendTo: '#dialog_container'})
expect($('#dialog_container .mercury-dialog').length).toEqual(1)
describe "#bindEvents", ->
it "only observes mousedown to stop the event"
describe "#preload", ->
beforeEach ->
@loadSpy = spyOn(Mercury.Dialog.prototype, 'load').andCallFake(=>)
it "calls load if configured", ->
@dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo', {appendTo: '#test', preload: true})
expect(@loadSpy.callCount).toEqual(1)
it "doesn't call load if configured", ->
@dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo', {appendTo: '#test', preload: false})
expect(@loadSpy.callCount).toEqual(0)
describe "#toggle", ->
beforeEach ->
@dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo', {appendTo: '#test'})
it "shows or hides", ->
expect(@dialog.element.css('display')).toEqual('none')
@dialog.toggle()
expect(@dialog.element.css('display')).toEqual('block')
@dialog.toggle()
expect(@dialog.element.css('display')).toEqual('none')
describe "#resize", ->
beforeEach ->
@dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo', {appendTo: '#test'})
it "calls show", ->
spy = spyOn(Mercury.Dialog.prototype, 'show').andCallFake(=>)
@dialog.resize()
expect(spy.callCount).toEqual(1)
describe "#show", ->
beforeEach ->
@dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo', {appendTo: '#test'})
it "triggers a custom event to hide all other dialogs", ->
spy = spyOn(Mercury, 'trigger').andCallFake(=>)
@dialog.show()
expect(spy.callCount).toEqual(1)
expect(spy.argsForCall[0]).toEqual(['hide:dialogs', @dialog])
describe "when already loaded", ->
beforeEach ->
@dialog.loaded = true
it "sets width and height on the element to auto", ->
@dialog.element.css({width: 100})
@dialog.show()
expect(@dialog.element.get(0).style.width).toEqual('auto')
expect(@dialog.element.get(0).style.height).toEqual('auto')
it "calls position", ->
spy = spyOn(Mercury.Dialog.prototype, 'position').andCallFake(=>)
@dialog.show()
expect(spy.callCount).toEqual(1)
expect(spy.argsForCall[0]).toEqual([true])
it "calls appear", ->
spy = spyOn(Mercury.Dialog.prototype, 'appear').andCallFake(=>)
@dialog.show()
expect(spy.callCount).toEqual(1)
describe "when not loaded", ->
it "calls position", ->
spy = spyOn(Mercury.Dialog.prototype, 'position').andCallFake(=>)
@dialog.show()
expect(spy.callCount).toEqual(1)
expect(spy.argsForCall[0]).toEqual([])
it "calls appear", ->
spy = spyOn(Mercury.Dialog.prototype, 'appear').andCallFake(=>)
@dialog.show()
expect(spy.callCount).toEqual(1)
describe "#position", ->
it "does nothing and is there as an interface method"
describe "#appear", ->
beforeEach ->
@dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo', {appendTo: '#test'})
it "animates the dialog in", ->
@dialog.appear()
expect(@dialog.element.css('display')).toEqual('block')
expect(parseFloat(@dialog.element.css('opacity')).toPrecision(2)).toEqual('0.95')
it "calls load if it's not already loaded", ->
describe "#hide", ->
beforeEach ->
@dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo', {appendTo: '#test'})
it "hides the dialog", ->
@dialog.element.css({display: 'block'})
@dialog.hide()
expect(@dialog.element.css('display')).toEqual('none')
expect(@dialog.visible).toEqual(false)
describe "#load", ->
beforeEach ->
@spyFunction = ->
Mercury.dialogHandlers.foo = ->
@dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo', {appendTo: '#test', for: $('#button')})
it "does nothing if there's no url", ->
spy = spyOn($, 'ajax').andCallFake(=>)
@dialog.url = false
@dialog.load()
expect(spy.callCount).toEqual(0)
describe "on a preloaded view", ->
beforeEach ->
Mercury.preloadedViews = {'/evergreen/responses/blank.html': 'this is the preloaded content'}
afterEach ->
Mercury.preloadedViews = {}
it "calls loadContent with the content in the preloaded view", ->
spy = spyOn(Mercury.Dialog.prototype, 'loadContent').andCallFake(=>)
@dialog.load()
expect(spy.callCount).toEqual(1)
expect(spy.argsForCall[0]).toEqual(['this is the preloaded content'])
describe "when not a preloaded view", ->
it "makes an ajax request", ->
spy = spyOn($, 'ajax').andCallFake(=>)
@dialog.load()
expect(spy.callCount).toEqual(1)
describe "on success", ->
beforeEach ->
@loadContentSpy = spyOn(Mercury.Dialog.prototype, 'loadContent').andCallFake(=>)
@ajaxSpy = spyOn($, 'ajax').andCallFake (url, options) =>
options.success('return value') if (options.success)
it "calls loadContent with data", ->
@dialog.load()
expect(@loadContentSpy.callCount).toEqual(1)
expect(@loadContentSpy.argsForCall[0]).toEqual(['return value'])
it "calls a dialog handler if there's one", ->
spy = spyOn(Mercury.dialogHandlers, 'foo').andCallFake(=>)
@dialog.load()
expect(spy.callCount).toEqual(1)
it "calls a callback if one was provided", ->
spy = spyOn(@, 'spyFunction').andCallFake(=>)
@dialog.load(@spyFunction)
expect(spy.callCount).toEqual(1)
describe "on failure", ->
beforeEach ->
@alertSpy = spyOn(window, 'alert').andCallFake(=>)
@ajaxSpy = spyOn($, 'ajax').andCallFake (url, options) =>
options.error() if (options.error)
it "hides", ->
spy = spyOn(Mercury.Dialog.prototype, 'hide').andCallFake(=>)
@dialog.load()
expect(spy.callCount).toEqual(1)
it "removes the pressed state for it's button", ->
$('#button').addClass('pressed')
@dialog.load()
expect($('#button').hasClass('pressed')).toEqual(false)
it "alerts the user", ->
@dialog.load()
expect(@alertSpy.callCount).toEqual(1)
expect(@alertSpy.argsForCall[0]).toEqual(['Mercury was unable to load /evergreen/responses/blank.html for the foo dialog.'])
describe "#loadContent", ->
beforeEach ->
@dialog = new Mercury.Dialog('/evergreen/responses/blank.html', 'foo', {appendTo: '#test'})
it "sets loaded to be true", ->
@dialog.loadContent()
expect(@dialog.loaded).toEqual(true)
it "removes the loading class from the element", ->
@dialog.loadContent()
expect(@dialog.element.hasClass('loading')).toEqual(false)
it "sets the element html to be the data passed to it", ->
@dialog.loadContent('hello world!')
expect(@dialog.element.html()).toEqual('hello world!')