modal.js 5.98 KB
Newer Older
1
2
$(function () {

3
  module('modal plugin')
XhmikosR's avatar
XhmikosR committed
4
5
6
7
8
9

  test('should be defined on jquery object', function () {
    var div = $('<div id="modal-test"></div>')
    ok(div.modal, 'modal method is defined')
  })

10
11
12
13
14
15
16
17
18
19
20
21
  module('modal', {
    setup: function() {
      // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
      $.fn.bootstrapModal = $.fn.modal.noConflict()
    },
    teardown: function() {
      $.fn.modal = $.fn.bootstrapModal
      delete $.fn.bootstrapModal
    }
  })

  test('should provide no conflict', function () {
22
    ok(!$.fn.modal, 'modal was set back to undefined (orig value)')
23
24
  })

XhmikosR's avatar
XhmikosR committed
25
26
  test('should return element', function () {
    var div = $('<div id="modal-test"></div>')
27
    ok(div.bootstrapModal() == div, 'document.body returned')
XhmikosR's avatar
XhmikosR committed
28
29
30
31
    $('#modal-test').remove()
  })

  test('should expose defaults var for settings', function () {
32
    ok($.fn.bootstrapModal.Constructor.DEFAULTS, 'default object exposed')
XhmikosR's avatar
XhmikosR committed
33
34
35
36
37
38
39
40
41
42
43
  })

  test('should insert into dom when show method is called', function () {
    stop()
    $.support.transition = false
    $('<div id="modal-test"></div>')
      .on('shown.bs.modal', function () {
        ok($('#modal-test').length, 'modal inserted into dom')
        $(this).remove()
        start()
      })
44
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
45
46
47
48
49
50
51
52
53
54
55
56
57
  })

  test('should fire show event', function () {
    stop()
    $.support.transition = false
    $('<div id="modal-test"></div>')
      .on('show.bs.modal', function () {
        ok(true, 'show was called')
      })
      .on('shown.bs.modal', function () {
        $(this).remove()
        start()
      })
58
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  })

  test('should not fire shown when default prevented', function () {
    stop()
    $.support.transition = false
    $('<div id="modal-test"></div>')
      .on('show.bs.modal', function (e) {
        e.preventDefault()
        ok(true, 'show was called')
        start()
      })
      .on('shown.bs.modal', function () {
        ok(false, 'shown was called')
      })
73
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
74
75
76
77
78
79
80
81
82
83
  })

  test('should hide modal when hide is called', function () {
    stop()
    $.support.transition = false

    $('<div id="modal-test"></div>')
      .on('shown.bs.modal', function () {
        ok($('#modal-test').is(':visible'), 'modal visible')
        ok($('#modal-test').length, 'modal inserted into dom')
84
        $(this).bootstrapModal('hide')
XhmikosR's avatar
XhmikosR committed
85
86
87
88
89
90
      })
      .on('hidden.bs.modal', function () {
        ok(!$('#modal-test').is(':visible'), 'modal hidden')
        $('#modal-test').remove()
        start()
      })
91
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
92
93
94
95
96
97
98
99
100
101
  })

  test('should toggle when toggle is called', function () {
    stop()
    $.support.transition = false
    var div = $('<div id="modal-test"></div>')
    div
      .on('shown.bs.modal', function () {
        ok($('#modal-test').is(':visible'), 'modal visible')
        ok($('#modal-test').length, 'modal inserted into dom')
102
        div.bootstrapModal('toggle')
XhmikosR's avatar
XhmikosR committed
103
104
105
106
107
108
      })
      .on('hidden.bs.modal', function () {
        ok(!$('#modal-test').is(':visible'), 'modal hidden')
        div.remove()
        start()
      })
109
      .bootstrapModal('toggle')
XhmikosR's avatar
XhmikosR committed
110
111
  })

112
  test('should remove from dom when click [data-dismiss="modal"]', function () {
XhmikosR's avatar
XhmikosR committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    stop()
    $.support.transition = false
    var div = $('<div id="modal-test"><span class="close" data-dismiss="modal"></span></div>')
    div
      .on('shown.bs.modal', function () {
        ok($('#modal-test').is(':visible'), 'modal visible')
        ok($('#modal-test').length, 'modal inserted into dom')
        div.find('.close').click()
      })
      .on('hidden.bs.modal', function () {
        ok(!$('#modal-test').is(':visible'), 'modal hidden')
        div.remove()
        start()
      })
127
      .bootstrapModal('toggle')
XhmikosR's avatar
XhmikosR committed
128
129
130
131
132
133
134
135
136
  })

  test('should allow modal close with "backdrop:false"', function () {
    stop()
    $.support.transition = false
    var div = $('<div>', { id: 'modal-test', 'data-backdrop': false })
    div
      .on('shown.bs.modal', function () {
        ok($('#modal-test').is(':visible'), 'modal visible')
137
        div.bootstrapModal('hide')
XhmikosR's avatar
XhmikosR committed
138
139
140
141
142
143
      })
      .on('hidden.bs.modal', function () {
        ok(!$('#modal-test').is(':visible'), 'modal hidden')
        div.remove()
        start()
      })
144
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
145
146
147
148
149
150
151
  })

  test('should close modal when clicking outside of modal-content', function () {
    stop()
    $.support.transition = false
    var div = $('<div id="modal-test"><div class="contents"></div></div>')
    div
152
      .on('shown.bs.modal', function () {
XhmikosR's avatar
XhmikosR committed
153
154
155
156
157
        ok($('#modal-test').length, 'modal insterted into dom')
        $('.contents').click()
        ok($('#modal-test').is(':visible'), 'modal visible')
        $('#modal-test').click()
      })
158
      .on('hidden.bs.modal', function () {
XhmikosR's avatar
XhmikosR committed
159
160
161
162
        ok(!$('#modal-test').is(':visible'), 'modal hidden')
        div.remove()
        start()
      })
163
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
164
165
166
167
168
169
170
171
172
173
  })

  test('should trigger hide event once when clicking outside of modal-content', function () {
    stop()
    $.support.transition = false

    var triggered
    var div = $('<div id="modal-test"><div class="contents"></div></div>')

    div
174
      .on('shown.bs.modal', function () {
XhmikosR's avatar
XhmikosR committed
175
176
177
        triggered = 0
        $('#modal-test').click()
      })
178
      .on('hide.bs.modal', function () {
XhmikosR's avatar
XhmikosR committed
179
180
181
182
        triggered += 1
        ok(triggered === 1, 'modal hide triggered once')
        start()
      })
183
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
184
185
  })

186
  test('should close reopened modal with [data-dismiss="modal"] click', function () {
XhmikosR's avatar
XhmikosR committed
187
188
189
190
    stop()
    $.support.transition = false
    var div = $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"></div></div></div>')
    div
191
      .on('shown.bs.modal', function () {
XhmikosR's avatar
XhmikosR committed
192
193
194
195
196
        $('#close').click()
        ok(!$('#modal-test').is(':visible'), 'modal hidden')
      })
      .one('hidden.bs.modal', function () {
        div.one('hidden.bs.modal', function () {
Jacob Thornton's avatar
Jacob Thornton committed
197
          start()
198
        }).bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
199
      })
200
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
201
202
203

    div.remove()
  })
204
})