modal.js 6 KB
Newer Older
1
$(function () {
XhmikosR's avatar
XhmikosR committed
2
  'use strict';
3

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

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

11
  module('modal', {
XhmikosR's avatar
XhmikosR committed
12
    setup: function () {
13
14
15
      // 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()
    },
XhmikosR's avatar
XhmikosR committed
16
    teardown: function () {
17
18
19
20
21
22
      $.fn.modal = $.fn.bootstrapModal
      delete $.fn.bootstrapModal
    }
  })

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

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

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

  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()
      })
45
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
46
47
48
49
50
51
52
53
54
55
56
57
58
  })

  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()
      })
59
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  })

  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')
      })
74
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
75
76
77
78
79
80
81
82
83
84
  })

  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')
85
        $(this).bootstrapModal('hide')
XhmikosR's avatar
XhmikosR committed
86
87
88
89
90
91
      })
      .on('hidden.bs.modal', function () {
        ok(!$('#modal-test').is(':visible'), 'modal hidden')
        $('#modal-test').remove()
        start()
      })
92
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
93
94
95
96
97
98
99
100
101
102
  })

  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')
103
        div.bootstrapModal('toggle')
XhmikosR's avatar
XhmikosR committed
104
105
106
107
108
109
      })
      .on('hidden.bs.modal', function () {
        ok(!$('#modal-test').is(':visible'), 'modal hidden')
        div.remove()
        start()
      })
110
      .bootstrapModal('toggle')
XhmikosR's avatar
XhmikosR committed
111
112
  })

113
  test('should remove from dom when click [data-dismiss="modal"]', function () {
XhmikosR's avatar
XhmikosR committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    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()
      })
128
      .bootstrapModal('toggle')
XhmikosR's avatar
XhmikosR committed
129
130
131
132
133
134
135
136
137
  })

  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')
138
        div.bootstrapModal('hide')
XhmikosR's avatar
XhmikosR committed
139
140
141
142
143
144
      })
      .on('hidden.bs.modal', function () {
        ok(!$('#modal-test').is(':visible'), 'modal hidden')
        div.remove()
        start()
      })
145
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
146
147
148
149
150
151
152
  })

  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
153
      .on('shown.bs.modal', function () {
XhmikosR's avatar
XhmikosR committed
154
155
156
157
158
        ok($('#modal-test').length, 'modal insterted into dom')
        $('.contents').click()
        ok($('#modal-test').is(':visible'), 'modal visible')
        $('#modal-test').click()
      })
159
      .on('hidden.bs.modal', function () {
XhmikosR's avatar
XhmikosR committed
160
161
162
163
        ok(!$('#modal-test').is(':visible'), 'modal hidden')
        div.remove()
        start()
      })
164
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
165
166
167
168
169
170
171
172
173
174
  })

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

187
  test('should close reopened modal with [data-dismiss="modal"] click', function () {
XhmikosR's avatar
XhmikosR committed
188
189
190
191
    stop()
    $.support.transition = false
    var div = $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"></div></div></div>')
    div
192
      .on('shown.bs.modal', function () {
XhmikosR's avatar
XhmikosR committed
193
194
195
196
197
        $('#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
198
          start()
199
        }).bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
200
      })
201
      .bootstrapModal('show')
XhmikosR's avatar
XhmikosR committed
202
203
204

    div.remove()
  })
205
})