forked from assaf/zombie
/
jquery_compat_test.coffee
145 lines (105 loc) · 4.31 KB
/
jquery_compat_test.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{ assert, brains, Browser } = require("./helpers")
JQUERY_VERSIONS = ["1.4.4", "1.5.1", "1.6.3", "1.7.1", "1.8.0"]
test = (version)->
describe version, ->
before (done)->
brains.get "/compat/jquery-#{version}", (req, res)->
res.send """
<html>
<head>
<title>jQuery #{version}</title>
<script src="/jquery-#{version}.js"></script>
</head>
<body>
<select>
<option>None</option>
<option value="1">One</option>
</select>
<span id="option"></span>
<a href="#post">Post</a>
<div id="response"></div>
<input id="edit-subject" value="subject">
<textarea id="edit-note">note</textarea>
<form action="/zombie/dead-end">
<button class="some-class">Click Me</button>
</form>
</body>
<script>
$(function() {
$("select").bind("change", function() {
$("#option").text(this.value);
});
$("a[href='#post']").click(function() {
$.post("/compat/echo/jquery-#{version}", {"foo": "bar"}, function(response) {
$("#response").text(response);
});
return false;
});
});
</script>
</html>
"""
brains.post "/compat/echo/jquery-#{version}", (req, res)->
lines = ([key, value].join("=") for key, value of req.body)
res.send lines.join("\n")
brains.ready done
before (done)->
@browser = new Browser()
@browser.visit "http://localhost:3003/compat/jquery-#{version}", done
describe "selecting an option in a select element", ->
before (done)->
@browser.select "select", "1"
done()
it "should fire the change event", ->
assert.equal @browser.text("#option"), "1"
describe "jQuery.post", ->
before (done)->
@browser.clickLink "Post", done
it "should perform an AJAX POST request", ->
assert /foo=bar/.test(@browser.text("#response"))
describe "jQuery.globalEval", ->
it "should work as expected", ->
@browser.evaluate("(function () {
$.globalEval('var globalEvalWorks = true;');
})();")
assert.ok @browser.window.globalEvalWorks
describe "setting val to empty", ->
it "should set to empty", ->
assert @browser.query("input#edit-subject").value
@browser.window.$("input#edit-subject").val("")
assert !@browser.query("input#edit-subject").value
assert @browser.query("textarea#edit-note").textContent
@browser.window.$("textarea#edit-note").val("")
assert !@browser.query("textarea#edit-note").textContent
# See issue 235 https://github.com/assaf/zombie/issues/235
if version > "1.6"
describe "undefined attribute", ->
it "should return undefined", ->
assert.equal @browser.window.$("#response").attr("class"), undefined
describe "closest with attribute selector", ->
it "should find element", ->
@browser.window.$("#response").html("<div class='ok'>")
assert.equal @browser.window.$("#response .ok").closest("[id]").attr("id"), "response"
# Using new event delegation introduced in 1.7
if version > "1.7"
describe "event handling", ->
it "should catch live event handler", (done)->
browser = new Browser()
browser.visit "http://localhost:3003/compat/jquery-#{version}", ->
browser.window.$(browser.document).live "click", ".some-class", (event)->
done()
browser.pressButton "Click Me"
it "should respect preventDefault in event delegation", (done)->
browser = new Browser()
browser.visit("http://localhost:3003/compat/jquery-#{version}")
.then ->
browser.window.$(browser.document).on "click", ".some-class", (event)->
event.preventDefault()
return
browser.pressButton "Click Me"
.then ->
assert browser.location.pathname != "/zombie/dead-end"
.then(done, done)
describe "Compatibility with jQuery", ->
for version in JQUERY_VERSIONS
test.call this, version