Skip to content

Commit e72ea67

Browse files
committed
Invent navigate helper to untangle nested callbacks in tests
1 parent 3cf3de1 commit e72ea67

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

test/unit/helpers.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Navigation helper for the test iframe. Queues navigation actions and
2+
// callbacks, then executes them serially with respect to async. This is to
3+
// avoid deep nesting of callbacks in tests.
4+
//
5+
// After last successful navigation, asyncTest is automatically resumed.
6+
//
7+
// Usage:
8+
//
9+
// navigate(this.frame)
10+
// .pjax(pjaxOptions, function(frame){ ... }
11+
// .back(-1, function(frame){ ... }
12+
//
13+
function navigate(frame) {
14+
var queue = []
15+
var api = {}
16+
17+
api.pjax = function(options, callback) {
18+
queue.push([options, callback])
19+
return api
20+
}
21+
api.back = api.pjax
22+
23+
var workOff = function() {
24+
var item = queue.shift()
25+
if (!item) {
26+
start()
27+
return
28+
}
29+
30+
var target = item[0], callback = item[1]
31+
32+
frame.$(frame.document).one("pjax:end", function() {
33+
if (callback) callback(frame)
34+
setTimeout(workOff, 0)
35+
})
36+
37+
if (typeof target == "number") {
38+
frame.history.go(target)
39+
} else {
40+
frame.$.pjax(target)
41+
}
42+
}
43+
44+
setTimeout(workOff, 0)
45+
46+
return api
47+
}

test/views/qunit.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<script type="text/javascript" src="/vendor/qunit.js"></script>
1010
<script type="text/javascript">QUnit.config.testTimeout = 10000</script>
1111
<script type="text/javascript" src="/jquery.pjax.js"></script>
12+
<script type="text/javascript" src="/test/unit/helpers.js"></script>
1213
<script type="text/javascript" src="/test/unit/pjax.js"></script>
1314
<script type="text/javascript" src="/test/unit/fn_pjax.js"></script>
1415
<script type="text/javascript" src="/test/unit/pjax_fallback.js"></script>

0 commit comments

Comments
 (0)