This repository has been archived by the owner on Dec 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 520
/
history-spec.coffee
164 lines (157 loc) · 8.76 KB
/
history-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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
require("./helpers")
{ vows: vows, assert: assert, zombie: zombie, brains: brains } = require("vows")
jsdom = require("jsdom")
brains.get "/boo", (req, res)->
response = if req.query.redirected then "Redirected" else "Eeek!"
res.send "<html><title>#{response}</title></html>"
brains.get "/redirect", (req, res)->
res.redirect "/boo?redirected=true"
vows.describe("History").addBatch(
"new window":
topic: -> new zombie.Browser().window
"should start out empty": (window)-> assert.length window.history, 0
"should start out with no location": (window)-> assert.isUndefined window.location.href
"go forward":
topic: (window)->
window.history.forward()
window
"should have no effect": (window)->
assert.length window.history, 0
assert.isUndefined window.location.href
"go backwards":
topic: (window)->
window.history.back()
window
"should have no effect": (window)->
assert.length window.history, 0
assert.isUndefined window.location.href
"history":
"pushState":
zombie.wants "http://localhost:3003/"
topic: (browser)->
browser.window.history.pushState { is: "start" }, null, "/start"
browser.window.history.pushState { is: "end" }, null, "/end"
@callback null, browser.window
"should add state to history": (window)-> assert.length window.history, 3
"should change location URL": (window)-> assert.equal window.location.href, "/end"
"go backwards":
topic: (window)->
window.addEventListener "popstate", (evt)=> @callback(null, evt)
window.history.back()
return
"should fire popstate event": (evt)-> assert.instanceOf evt, jsdom.dom.level3.events.Event
"should include state": (evt)-> assert.equal evt.state.is, "start"
"go forwards":
zombie.wants "http://localhost:3003/"
topic: (browser)->
browser.window.history.pushState { is: "start" }, null, "/start"
browser.window.history.pushState { is: "end" }, null, "/end"
browser.window.history.back()
browser.window.addEventListener "popstate", (evt)=> @callback(null, evt)
browser.window.history.forward()
return
"should fire popstate event": (evt)-> assert.instanceOf evt, jsdom.dom.level3.events.Event
"should include state": (evt)-> assert.equal evt.state.is, "end"
"replaceState":
zombie.wants "http://localhost:3003/"
topic: (browser)->
browser.window.history.pushState { is: "start" }, null, "/start"
browser.window.history.replaceState { is: "end" }, null, "/end"
@callback null, browser.window
"should not add state to history": (window)-> assert.length window.history, 2
"should change location URL": (window)-> assert.equal window.location.href, "/end"
"go backwards":
topic: (browser)->
browser.window.addEventListener "popstate", (evt)=>
browser.window.popstate = true
browser.window.history.back()
@callback null, browser.window
"should change location URL": (window)-> assert.equal window.location.href, "http://localhost:3003/"
"should not fire popstate event": (window)-> assert.isUndefined window.popstate
"location":
"open page":
zombie.wants "http://localhost:3003/"
"should add page to history": (browser)-> assert.length browser.window.history, 1
"should change location URL": (browser)-> assert.equal browser.location, "http://localhost:3003/"
"should load document": (browser)-> assert.match browser.html(), /Tap, Tap/
"should set window location": (browser)-> assert.equal browser.window.location.href, "http://localhost:3003/"
"should set document location": (browser)-> assert.equal browser.document.location.href, "http://localhost:3003/"
"change location":
zombie.wants "http://localhost:3003/"
topic: (browser)->
browser.window.location = "http://localhost:3003/boo"
browser.window.document.addEventListener "DOMContentLoaded", => @callback null, browser
return
"should add page to history": (browser)-> assert.length browser.window.history, 2
"should change location URL": (browser)-> assert.equal browser.location, "http://localhost:3003/boo"
"should load document": (browser)-> assert.match browser.html(), /Eeek!/
"change pathname":
zombie.wants "http://localhost:3003/"
topic: (browser)->
browser.window.location.pathname = "/boo"
browser.window.document.addEventListener "DOMContentLoaded", => @callback null, browser
return
"should add page to history": (browser)-> assert.length browser.window.history, 2
"should change location URL": (browser)-> assert.equal browser.location, "http://localhost:3003/boo"
"should load document": (browser)-> assert.match browser.html(), /Eeek!/
"change hash":
zombie.wants "http://localhost:3003/"
topic: (browser)->
browser.document.innerHTML = "Wolf"
browser.window.addEventListener "hashchange", => @callback null, browser
browser.window.location.hash = "boo"
return
"should add page to history": (browser)-> assert.length browser.window.history, 2
"should change location URL": (browser)-> assert.equal browser.location, "http://localhost:3003/#boo"
"should not reload document": (browser)-> assert.match browser.document.innerHTML, /Wolf/
"assign":
zombie.wants "http://localhost:3003/"
topic: (browser)->
@window = browser.window
browser.window.location.assign "http://localhost:3003/boo"
browser.document.addEventListener "DOMContentLoaded", => @callback null, browser
return
"should add page to history": (browser)-> assert.length browser.window.history, 2
"should change location URL": (browser)-> assert.equal browser.location, "http://localhost:3003/boo"
"should load document": (browser)-> assert.match browser.html(), /Eeek!/
"should load document in new window": (browser)-> assert.ok browser.window != @window
"replace":
zombie.wants "http://localhost:3003/"
topic: (browser)->
@window = browser.window
browser.window.location.replace "http://localhost:3003/boo"
browser.window.document.addEventListener "DOMContentLoaded", => @callback null, browser
return
"should not add page to history": (browser)-> assert.length browser.window.history, 1
"should change location URL": (browser)-> assert.equal browser.location, "http://localhost:3003/boo"
"should load document": (browser)-> assert.match browser.html(), /Eeek!/
"should load document in new window": (browser)-> assert.ok browser.window != @window
"reload":
zombie.wants "http://localhost:3003/"
topic: (browser)->
@window = browser.window
browser.window.document.innerHTML = "Wolf"
browser.window.location.reload()
browser.window.document.addEventListener "DOMContentLoaded", => @callback null, browser
return
"should not add page to history": (browser)-> assert.length browser.window.history, 1
"should not change location URL": (browser)-> assert.equal browser.location, "http://localhost:3003/"
"should reload document": (browser)-> assert.match browser.html(), /Tap, Tap/
"should reload document in new window": (browser)-> assert.ok browser.window != @window
"components":
zombie.wants "http://localhost:3003/"
topic: (browser)-> browser.location
"should include protocol": (location)-> assert.equal location.protocol, "http:"
"should include hostname": (location)-> assert.equal location.hostname, "localhost"
"should include port": (location)-> assert.equal location.port, 3003
"should include hostname and port": (location)-> assert.equal location.host, "localhost:3003"
"should include pathname": (location)-> assert.equal location.pathname, "/"
"should include search": (location)-> assert.equal location.search, ""
"should include hash": (location)-> assert.equal location.hash, ""
"redirect":
zombie.wants "http://localhost:3003/redirect"
"should redirect to final destination": (browser)-> assert.equal browser.location, "http://localhost:3003/boo?redirected=true"
"should pass query parameter": (browser)-> assert.equal browser.text("title"), "Redirected"
"should not add location in history": (browser)-> assert.length browser.window.history, 1
"should indicate last request followed a redirect": (browser)-> assert.ok browser.redirected
).export(module)