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

fat's avatar
fat committed
3
    module("modal")
4

5
6
7
8
9
10
      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
      })

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

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

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

Jacob Thornton's avatar
Jacob Thornton committed
26
      test("should insert into dom when show method is called", function () {
Jacob Thornton's avatar
Jacob Thornton committed
27
        stop()
Jacob Thornton's avatar
Jacob Thornton committed
28
        $.support.transition = false
29
        $("<div id='modal-test'></div>")
30
          .on("shown.bs.modal", function () {
Mark Otto's avatar
Mark Otto committed
31
            ok($('#modal-test').length, 'modal inserted into dom')
32
            $(this).remove()
Jacob Thornton's avatar
Jacob Thornton committed
33
34
            start()
          })
35
          .modal("show")
36
37
      })

38
39
40
41
      test("should fire show event", function () {
        stop()
        $.support.transition = false
        $("<div id='modal-test'></div>")
42
          .on("show.bs.modal", function () {
43
44
            ok(true, "show was called")
          })
45
          .on("shown.bs.modal", function () {
46
47
48
49
50
51
52
53
54
55
            $(this).remove()
            start()
          })
          .modal("show")
      })

      test("should not fire shown when default prevented", function () {
        stop()
        $.support.transition = false
        $("<div id='modal-test'></div>")
56
          .on("show.bs.modal", function (e) {
57
58
59
60
            e.preventDefault()
            ok(true, "show was called")
            start()
          })
61
          .on("shown.bs.modal", function () {
62
63
64
65
66
            ok(false, "shown was called")
          })
          .modal("show")
      })

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

        $("<div id='modal-test'></div>")
72
          .on("shown.bs.modal", function () {
Jacob Thornton's avatar
Jacob Thornton committed
73
            ok($('#modal-test').is(":visible"), 'modal visible')
Mark Otto's avatar
Mark Otto committed
74
            ok($('#modal-test').length, 'modal inserted into dom')
75
            $(this).modal("hide")
Jacob Thornton's avatar
Jacob Thornton committed
76
          })
77
          .on("hidden.bs.modal", function() {
Jacob Thornton's avatar
Jacob Thornton committed
78
            ok(!$('#modal-test').is(":visible"), 'modal hidden')
79
            $('#modal-test').remove()
Jacob Thornton's avatar
Jacob Thornton committed
80
81
            start()
          })
Jacob Thornton's avatar
Jacob Thornton committed
82
          .modal("show")
83
84
      })

Jacob Thornton's avatar
Jacob Thornton committed
85
      test("should toggle when toggle is called", function () {
Jacob Thornton's avatar
Jacob Thornton committed
86
        stop()
87
        $.support.transition = false
Jacob Thornton's avatar
Jacob Thornton committed
88
        var div = $("<div id='modal-test'></div>")
Jacob Thornton's avatar
Jacob Thornton committed
89
        div
90
          .on("shown.bs.modal", function () {
Jacob Thornton's avatar
Jacob Thornton committed
91
            ok($('#modal-test').is(":visible"), 'modal visible')
Mark Otto's avatar
Mark Otto committed
92
            ok($('#modal-test').length, 'modal inserted into dom')
Jacob Thornton's avatar
Jacob Thornton committed
93
            div.modal("toggle")
Jacob Thornton's avatar
Jacob Thornton committed
94
          })
95
          .on("hidden.bs.modal", function() {
Jacob Thornton's avatar
Jacob Thornton committed
96
            ok(!$('#modal-test').is(":visible"), 'modal hidden')
Jacob Thornton's avatar
Jacob Thornton committed
97
            div.remove()
98
            start()
Jacob Thornton's avatar
Jacob Thornton committed
99
          })
Jacob Thornton's avatar
Jacob Thornton committed
100
          .modal("toggle")
101
102
      })

103
      test("should remove from dom when click [data-dismiss=modal]", function () {
Jacob Thornton's avatar
Jacob Thornton committed
104
        stop()
Jacob Thornton's avatar
Jacob Thornton committed
105
        $.support.transition = false
106
        var div = $("<div id='modal-test'><span class='close' data-dismiss='modal'></span></div>")
Jacob Thornton's avatar
Jacob Thornton committed
107
        div
108
          .on("shown.bs.modal", function () {
Jacob Thornton's avatar
Jacob Thornton committed
109
            ok($('#modal-test').is(":visible"), 'modal visible')
Mark Otto's avatar
Mark Otto committed
110
            ok($('#modal-test').length, 'modal inserted into dom')
Jacob Thornton's avatar
Jacob Thornton committed
111
112
            div.find('.close').click()
          })
113
          .on("hidden.bs.modal", function() {
Jacob Thornton's avatar
Jacob Thornton committed
114
            ok(!$('#modal-test').is(":visible"), 'modal hidden')
Jacob Thornton's avatar
Jacob Thornton committed
115
            div.remove()
116
            start()
Jacob Thornton's avatar
Jacob Thornton committed
117
          })
Jacob Thornton's avatar
Jacob Thornton committed
118
          .modal("toggle")
Jacob Thornton's avatar
Jacob Thornton committed
119
      })
fat's avatar
shiiiit    
fat committed
120
121
122
123
124
125

      test("should allow modal close with 'backdrop:false'", function () {
        stop()
        $.support.transition = false
        var div = $("<div>", { id: 'modal-test', "data-backdrop": false })
        div
126
          .on("shown.bs.modal", function () {
fat's avatar
shiiiit    
fat committed
127
128
129
            ok($('#modal-test').is(":visible"), 'modal visible')
            div.modal("hide")
          })
130
          .on("hidden.bs.modal", function() {
fat's avatar
shiiiit    
fat committed
131
132
133
134
135
136
            ok(!$('#modal-test').is(":visible"), 'modal hidden')
            div.remove()
            start()
          })
          .modal("show")
      })
fat's avatar
fat committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

      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")
      })
156
})