forked from assaf/zombie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser-spec.coffee
143 lines (130 loc) · 5.54 KB
/
browser-spec.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
require("./helpers")
{ vows: vows, assert: assert, zombie: zombie, brains: brains } = require("vows")
jsdom = require("jsdom")
fs = require("fs")
brains.get "/scripted", (req, res)-> res.send """
<html>
<head>
<title>Whatever</title>
<script src="/jquery.js"></script>
</head>
<body>Hello World</body>
<script>
$(function() { $("title").text("Awesome") })
</script>
</html>
"""
brains.get "/living", (req, res)-> res.send """
<html>
<head>
<script src="/jquery.js"></script>
<script src="/sammy.js"></script>
<script src="/app.js"></script>
</head>
<body>
<div id="main">
<a href="/dead">Kill</a>
<form action="#/dead" method="post">
<label>Email <input type="text" name="email"></label>
<label>Password <input type="password" name="password"></label>
<button>Sign Me Up</button>
</form>
</div>
<div class="now">Walking Aimlessly</div>
</body>
</html>
"""
brains.get "/app.js", (req, res)-> res.send """
Sammy("#main", function(app) {
app.get("#/", function(context) {
document.title = "The Living";
});
app.get("#/dead", function(context) {
context.swap("The Living Dead");
});
app.post("#/dead", function(context) {
document.title = "Signed up";
});
});
$(function() { Sammy("#main").run("#/") });
"""
brains.get "/dead", (req, res)-> res.send """
<html>
<head>
<script src="/jquery.js"></script>
</head>
<body>
<script>
$(function() { document.title = "The Dead" });
</script>
</body>
</html>
"""
vows.describe("Browser").addBatch(
"open page":
zombie.wants "http://localhost:3003/scripted"
"should create HTML document": (browser)-> assert.instanceOf browser.document, jsdom.dom.level3.html.HTMLDocument
"should load document from server": (browser)-> assert.match browser.html(), /<body>Hello World<\/body>/
"should load external scripts": (browser)->
assert.ok jQuery = browser.window.jQuery, "window.jQuery not available"
assert.typeOf jQuery.ajax, "function"
"should run jQuery.onready": (browser)-> assert.equal browser.document.title, "Awesome"
"run app":
zombie.wants "http://localhost:3003/living"
"should execute route": (browser)-> assert.equal browser.document.title, "The Living"
"should change location": (browser)-> assert.equal browser.location, "http://localhost:3003/living#/"
"move around":
topic: (browser)->
browser.window.location.hash = "/dead"
browser.wait @callback
"should execute route": (browser)-> assert.equal browser.text("#main"), "The Living Dead"
"should change location": (browser)-> assert.equal browser.location, "http://localhost:3003/living#/dead"
"event emitter":
"successful":
topic: ->
browser = new zombie.Browser
browser.on "loaded", (browser)=> @callback null, browser
browser.wants "http://localhost:3003/"
"should fire load event": (browser)-> assert.ok browser.visit
"error":
topic: ->
browser = new zombie.Browser
browser.on "error", (err)=> @callback null, err
browser.wants "http://localhost:3003/deadend"
"should fire onerror event": (err)->
assert.ok err.message && err.stack
assert.equal err.message, "Could not load document at http://localhost:3003/deadend, got 404"
"wait over":
topic: ->
browser = new zombie.Browser
browser.on "drain", (browser)=> @callback null, browser
browser.wants "http://localhost:3003/"
"should fire done event": (browser)-> assert.ok browser.visit
"content selection":
zombie.wants "http://localhost:3003/living"
"query text":
topic: (browser)-> browser
"should query from document": (browser)-> assert.equal browser.text(".now"), "Walking Aimlessly"
"should query from context": (browser)-> assert.equal browser.text(".now", browser.body), "Walking Aimlessly"
"should query from context": (browser)-> assert.equal browser.text(".now", browser.querySelector("#main")), ""
"should combine multiple elements": (browser)-> assert.equal browser.text("form label"), "Email Password "
"query html":
topic: (browser)-> browser
"should query from document": (browser)-> assert.equal browser.html(".now"), "<div class=\"now\">Walking Aimlessly</div>"
"should query from context": (browser)-> assert.equal browser.html(".now", browser.body), "Walking Aimlessly"
"should query from context": (browser)-> assert.equal browser.html(".now", browser.querySelector("#main")), ""
"should combine multiple elements": (browser)-> assert.equal browser.html("title, #main a"), "<title>The Living</title><a href=\"/dead\">Kill</a>"
"click link":
zombie.wants "http://localhost:3003/living"
topic: (browser)->
browser.clickLink "Kill", @callback
"should change location": (browser)-> assert.equal browser.location, "http://localhost:3003/dead"
"should run all events": (browser)-> assert.equal browser.document.title, "The Dead"
"live events":
zombie.wants "http://localhost:3003/living"
topic: (browser)->
browser.fill("Email", "armbiter@zombies").fill("Password", "br41nz").
pressButton "Sign Me Up", @callback
"should change location": (browser)-> assert.equal browser.location, "http://localhost:3003/living#/"
"should process event": (browser)-> assert.equal browser.document.title, "Signed up"
).export(module)