modal 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,338 +0,0 @@
1
- require('./test-env')
2
-
3
- var modal = require('..')
4
- , assert = require('assert')
5
-
6
- describe('modal', function () {
7
-
8
- afterEach(function () {
9
- $('body').empty()
10
- })
11
-
12
- describe('modal()', function () {
13
-
14
- it('should be a function', function () {
15
- assert.equal(typeof modal, 'function')
16
- })
17
-
18
- it('should append one modal element to the DOM when called', function () {
19
- modal({ fx: false })
20
- assert.equal($('.modal-overlay').length, 1)
21
- })
22
-
23
- })
24
-
25
- describe('template()', function () {
26
-
27
- describe('title', function () {
28
-
29
- it('should render a title if supplied', function () {
30
- modal({ fx: false, title: 'This is a test' })
31
- assert.equal($('.modal-title').text(), 'This is a test')
32
- })
33
-
34
- it('should not render a title element it falsy (\'\')', function () {
35
- modal({ fx: false, title: '' })
36
- assert.equal($('.modal-title').length, 0)
37
- })
38
-
39
- it('should not render a title element it falsy (false)', function () {
40
- modal({ fx: false, title: false })
41
- assert.equal($('.modal-title').length, 0)
42
- })
43
-
44
- it('should render a default title if none supplied', function () {
45
- modal({ fx: false })
46
- assert.equal($('.modal-title').text(), 'Are you sure?')
47
- })
48
-
49
- })
50
-
51
- describe('content', function () {
52
-
53
- it('should render content wrapped in a <p> if passed as a string', function () {
54
- modal({ fx: false, content: 'This is a test' })
55
- var $el = $('.js-content p')
56
- assert.equal($el.length, 1)
57
- assert.equal($el.text(), 'This is a test')
58
- })
59
-
60
- it('should just append content that is not a string (e.g DOM)', function () {
61
- var $bq = $('<blockquote/>').text('This is a different test')
62
- modal({ fx: false, content: $bq })
63
- var $el = $('.js-content blockquote')
64
- assert.equal($el.length, 1)
65
- assert.equal($el.text(), 'This is a different test')
66
- assert.equal($bq[0], $el[0])
67
- })
68
-
69
- })
70
-
71
- describe('controls', function () {
72
-
73
- it('should not render the controls element if no buttons are passed', function () {
74
- var buttons = []
75
- modal({ fx: false, buttons: buttons })
76
- assert.equal($('.modal-controls').length, 0)
77
- })
78
-
79
- it('should render the correct number of buttons', function () {
80
- var buttons = [ { text: 'Confirm' }, { text: 'Cancel' } ]
81
- modal({ fx: false, buttons: buttons })
82
- assert.equal($('.js-button').length, 2)
83
- })
84
-
85
- it('should display the correct text', function () {
86
- var buttons = [ { text: 'Button 1' }, { text: 'Button 2' } ]
87
- modal({ fx: false, buttons: buttons })
88
- $('.js-button').each(function (i) {
89
- assert.equal($(this).text(), buttons[i].text)
90
- })
91
- })
92
-
93
- it('should add the correct classes', function () {
94
- var buttons =
95
- [ { text: 'Button 1', className: 'one-class' }
96
- , { text: 'Button 2', className: 'multiple classes' }
97
- ]
98
- modal({ fx: false, buttons: buttons })
99
- assert($('.js-button').eq(0).hasClass('one-class'))
100
- assert($('.js-button').eq(1).hasClass('multiple'))
101
- assert($('.js-button').eq(1).hasClass('classes'))
102
- })
103
-
104
- })
105
-
106
- })
107
-
108
- describe('close()', function () {
109
-
110
- it('should remove the modal', function (done) {
111
- var m = modal({ fx: false })
112
- assert.equal($('.modal-overlay').length, 1)
113
- m.close()
114
- setTimeout(function () {
115
- assert.equal($('.modal-overlay').length, 0)
116
- done()
117
- }, 0)
118
- })
119
-
120
- it('should emit a close event', function (done) {
121
- var m = modal({ fx: false })
122
- m.on('close', function () {
123
- done()
124
- })
125
- m.close()
126
- })
127
-
128
- it('should emit a beforeClose event', function (done) {
129
- var m = modal({ fx: false })
130
- , beforeCloseCalled = false
131
- m.on('beforeClose', function () {
132
- beforeCloseCalled = true
133
- })
134
- m.on('close', function () {
135
- assert(beforeCloseCalled)
136
- done()
137
- })
138
- m.close()
139
- })
140
-
141
- it('should not close until all beforeClose listeners that accept a callback complete', function (done) {
142
- var m = modal({ fx: false })
143
- , beforeCloseCbCount = 0
144
- , beforeCloseHandler = function (cb) {
145
- setTimeout(function() {
146
- beforeCloseCbCount++
147
- cb()
148
- }, 100)
149
- }
150
-
151
- m.on('beforeClose', beforeCloseHandler)
152
- m.on('beforeClose', beforeCloseHandler)
153
- m.on('beforeClose', beforeCloseHandler)
154
-
155
- m.on('close', function () {
156
- assert.equal(beforeCloseCbCount, 3)
157
- done()
158
- })
159
- m.close()
160
- })
161
-
162
- it('should not wait for beforeClose callbacks for listeners that don’t accept them', function (done) {
163
- var m = modal({ fx: false })
164
- , beforeCloseCbCount = 0
165
- , beforeCloseHandlerWithoutCallback = function () {
166
- setTimeout(function() {
167
- beforeCloseCbCount++
168
- }, 100)
169
- }
170
- , beforeCloseHandlerWithCallback = function (cb) {
171
- setTimeout(function() {
172
- beforeCloseCbCount++
173
- cb()
174
- }, 50)
175
- }
176
-
177
- m.on('beforeClose', beforeCloseHandlerWithCallback)
178
- m.on('beforeClose', beforeCloseHandlerWithCallback)
179
- m.on('beforeClose', beforeCloseHandlerWithoutCallback)
180
- m.on('beforeClose', beforeCloseHandlerWithoutCallback)
181
-
182
- m.on('close', function () {
183
- assert.equal(beforeCloseCbCount, 2)
184
- done()
185
- })
186
- m.close()
187
- })
188
-
189
- })
190
-
191
- describe('centre()', function () {
192
-
193
- it('should centre to window height', function () {
194
- var m = modal({ fx: false })
195
- assert.equal(typeof m.centre, 'function')
196
-
197
- // jQuery requires a bit of persuasion the window really
198
- // is 1000px tall, hence these two assignments
199
- window.innerHeight = document.documentElement.clientHeight = 1000
200
-
201
- $('.modal-content').height(100)
202
- assert.equal($('.modal-content').outerHeight(), 100)
203
- $('.modal-content').css('top', 0)
204
-
205
- m.centre()
206
-
207
- assert.equal($('.modal-content').css('top'), '450px')
208
- })
209
-
210
- })
211
-
212
- describe('handeResize()', function () {
213
-
214
- it('should maintain postion in the centre of the screen')
215
-
216
- })
217
-
218
- describe('option: className', function () {
219
-
220
- it('should apply a class to the modal element if passed', function () {
221
- modal({ fx: false, className: 'my-custom-modal-class' })
222
- assert($('.js-modal').hasClass('my-custom-modal-class'))
223
- })
224
-
225
- it('should allow multiple classes (space delimited)', function () {
226
- modal({ fx: false, className: 'my-custom-modal-class my-other-class' })
227
- assert($('.js-modal').hasClass('my-custom-modal-class'))
228
- assert($('.js-modal').hasClass('my-other-class'))
229
- })
230
-
231
- })
232
-
233
- describe('option: clickOutsideToClose', function () {
234
-
235
- it ('should close the modal if set to true', function (done) {
236
- modal({ fx: false, clickOutsideToClose: true })
237
- assert.equal($('.modal-overlay').length, 1)
238
- $('.modal-overlay').click()
239
- setTimeout(function () {
240
- assert.equal($('.modal-overlay').length, 0)
241
- done()
242
- }, 0)
243
- })
244
-
245
- it('should not close the modal if set to false', function (done) {
246
- modal({ fx: false, clickOutsideToClose: false })
247
- assert.equal($('.modal-overlay').length, 1)
248
- $('.modal-overlay').click()
249
- setTimeout(function () {
250
- assert.equal($('.modal-overlay').length, 1)
251
- done()
252
- }, 1)
253
- })
254
-
255
- it ('should be true by default', function (done) {
256
- modal({ fx: false })
257
- assert.equal($('.modal-overlay').length, 1)
258
- $('.modal-overlay').click()
259
- setTimeout(function () {
260
- assert.equal($('.modal-overlay').length, 0)
261
- done()
262
- }, 0)
263
- })
264
-
265
- })
266
-
267
- describe('option: clickOutsideEvent', function () {
268
-
269
- it('should be fired when a click happens outside the modal', function (done) {
270
- modal({ fx: false, clickOutsideEvent: 'test' }).on('test', function () {
271
- done()
272
- })
273
- $('.modal-overlay').click()
274
- })
275
-
276
- it ('should be "cancel" by default', function (done) {
277
- modal({ fx: false }).on('cancel', function () {
278
- done()
279
- })
280
- $('.modal-overlay').click()
281
- })
282
-
283
- })
284
-
285
- describe('option: buttons', function () {
286
-
287
- it('should trigger the provided event', function (done) {
288
-
289
- modal(
290
- { fx: false
291
- , buttons: [ { text: 'Go', event: 'go' } ]
292
- }).on('go', function () {
293
- done()
294
- })
295
- $('.js-button').click()
296
-
297
- })
298
-
299
- it('should hook up buttons to keyup events if provided', function (done) {
300
- modal(
301
- { fx: false
302
- , buttons: [ { text: 'Stop', event: 'stop', keyCodes: [ 27 ] } ]
303
- }).on('stop', function () {
304
- done()
305
- })
306
-
307
- $(document).trigger({ type: 'keyup', keyCode: 27 })
308
-
309
- })
310
-
311
- it('should have defaults', function (done) {
312
- var i = 0
313
- function cb() {
314
- if (++i === 3) return done()
315
- setTimeout(fns[i], 10)
316
- }
317
- var fns =
318
- [ function () {
319
- modal({ fx: false }).on('cancel', cb)
320
- $('.js-button').eq(0).click()
321
- }
322
- , function () {
323
- modal({ fx: false }).on('confirm', cb)
324
- $('.js-button').eq(1).click()
325
- }
326
- , function () {
327
- modal({ fx: false }).on('cancel', cb)
328
- $(document).trigger({ type: 'keyup', keyCode: 27 })
329
- }
330
- ]
331
-
332
- fns[0]()
333
-
334
- })
335
-
336
- })
337
-
338
- })
package/test/test-env.js DELETED
@@ -1,6 +0,0 @@
1
- var jsdom = require('jsdom')
2
-
3
- var window = global.window = jsdom.jsdom().createWindow('<html><body></body></html>')
4
-
5
- window.jQuery = global.jQuery = global.$ = require('jquery').create(window)
6
- global.document = window.document