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

Jacob Thornton's avatar
Jacob Thornton committed
3
  module("modal")
4

Jacob Thornton's avatar
Jacob Thornton committed
5
6
7
8
9
    test("should provide no conflict", function () {
      var modal = $.fn.modal.noConflict()
      ok(!$.fn.modal, 'modal was set back to undefined (org value)')
      $.fn.modal = modal
    })
10

Jacob Thornton's avatar
Jacob Thornton committed
11
12
13
14
    test("should be defined on jquery object", function () {
      var div = $("<div id='modal-test'></div>")
      ok(div.modal, 'modal method is defined')
    })
15

Jacob Thornton's avatar
Jacob Thornton committed
16
17
18
19
20
    test("should return element", function () {
      var div = $("<div id='modal-test'></div>")
      ok(div.modal() == div, 'document.body returned')
      $('#modal-test').remove()
    })
21

Jacob Thornton's avatar
Jacob Thornton committed
22
23
24
    test("should expose defaults var for settings", function () {
      ok($.fn.modal.Constructor.DEFAULTS, 'default object exposed')
    })
25

Jacob Thornton's avatar
Jacob Thornton committed
26
27
28
29
30
31
32
33
34
35
36
    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()
        })
        .modal("show")
    })
37

Jacob Thornton's avatar
Jacob Thornton committed
38
39
40
41
42
43
44
45
46
47
48
49
50
    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()
        })
        .modal("show")
    })
51

Jacob Thornton's avatar
Jacob Thornton committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    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")
        })
        .modal("show")
    })
66

Jacob Thornton's avatar
Jacob Thornton committed
67
68
69
    test("should hide modal when hide is called", function () {
      stop()
      $.support.transition = false
70

Jacob Thornton's avatar
Jacob Thornton committed
71
72
73
74
75
76
77
78
79
80
81
82
83
      $("<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')
          $(this).modal("hide")
        })
        .on("hidden.bs.modal", function() {
          ok(!$('#modal-test').is(":visible"), 'modal hidden')
          $('#modal-test').remove()
          start()
        })
        .modal("show")
    })
84

Jacob Thornton's avatar
Jacob Thornton committed
85
86
87
88
89
90
91
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')
          div.modal("toggle")
        })
        .on("hidden.bs.modal", function() {
          ok(!$('#modal-test').is(":visible"), 'modal hidden')
          div.remove()
          start()
        })
        .modal("toggle")
    })
102

Jacob Thornton's avatar
Jacob Thornton committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    test("should remove from dom when click [data-dismiss=modal]", function () {
      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()
        })
        .modal("toggle")
    })
fat's avatar
shiiiit    
fat committed
120

Jacob Thornton's avatar
Jacob Thornton committed
121
122
123
124
125
126
127
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')
          div.modal("hide")
        })
        .on("hidden.bs.modal", function() {
          ok(!$('#modal-test').is(":visible"), 'modal hidden')
          div.remove()
          start()
        })
        .modal("show")
    })
fat's avatar
fat committed
137

Jacob Thornton's avatar
Jacob Thornton committed
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
    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
        .bind("shown.bs.modal", function () {
          ok($('#modal-test').length, 'modal insterted into dom')
          $('.contents').click()
          ok($('#modal-test').is(":visible"), 'modal visible')
          $('#modal-test').click()
        })
        .bind("hidden.bs.modal", function() {
          ok(!$('#modal-test').is(":visible"), 'modal hidden')
          div.remove()
          start()
        })
        .modal("show")
    })

    test("should trigger hide event once when clicking outside of modal-content", function () {
      stop()
      $.support.transition = false
      var div = $("<div id='modal-test'><div class='contents'></div></div>")
      var triggered
      div
        .bind("shown.bs.modal", function () {
          triggered = 0
          $('#modal-test').click()
        })
        .one("hidden.bs.modal", function() {
          div.modal("show")
        })
        .bind("hide.bs.modal", function () {
          triggered += 1
          ok(triggered === 1, 'modal hide triggered once')
          start()
        })
        .modal("show")
    })
177
})