Adapt tests for debouncedAsync.

This commit is contained in:
Pierre-Yves Gerardy 2016-09-02 23:01:40 +02:00
parent 2e6a2ae5d9
commit 879b9d6883
2 changed files with 114 additions and 175 deletions

View file

@ -28,7 +28,7 @@ o.spec("route", function() {
route.prefix(prefix) route.prefix(prefix)
}) })
o("renders into `root`", function(done) { o("renders into `root`", function() {
$window.location.href = prefix + "/" $window.location.href = prefix + "/"
route(root, "/", { route(root, "/", {
"/" : { "/" : {
@ -38,11 +38,21 @@ o.spec("route", function() {
} }
}) })
callAsync(function() { o(root.firstChild.nodeName).equals("DIV")
o(root.firstChild.nodeName).equals("DIV") })
o("routed mount points can redraw synchronoulsy (#1275)", function() {
var view = o.spy()
$window.location.href = prefix + "/"
route(root, "/", {"/":{view:view}})
o(view.callCount).equals(1)
redraw.publish(true)
o(view.callCount).equals(2)
done()
})
}) })
o("default route doesn't break back button", function(done) { o("default route doesn't break back button", function(done) {
@ -84,7 +94,7 @@ o.spec("route", function() {
} }
}) })
o("redraws when render function is executed", function(done) { o("redraws when render function is executed", function() {
var onupdate = o.spy() var onupdate = o.spy()
var oninit = o.spy() var oninit = o.spy()
@ -100,18 +110,11 @@ o.spec("route", function() {
} }
}) })
callAsync(function() { o(oninit.callCount).equals(1)
o(oninit.callCount).equals(1)
redraw.publish() redraw.publish(true)
// Wrapped to give time for the rate-limited redraw to fire o(onupdate.callCount).equals(1)
setTimeout(function() {
o(onupdate.callCount).equals(1)
done()
}, FRAME_BUDGET)
})
}) })
o("redraws on events", function(done) { o("redraws on events", function(done) {
@ -135,23 +138,21 @@ o.spec("route", function() {
} }
}) })
callAsync(function() { root.firstChild.dispatchEvent(e)
root.firstChild.dispatchEvent(e)
o(oninit.callCount).equals(1) o(oninit.callCount).equals(1)
o(onclick.callCount).equals(1) o(onclick.callCount).equals(1)
o(onclick.this).equals(root.firstChild) o(onclick.this).equals(root.firstChild)
o(onclick.args[0].type).equals("click") o(onclick.args[0].type).equals("click")
o(onclick.args[0].target).equals(root.firstChild) o(onclick.args[0].target).equals(root.firstChild)
// Wrapped to give time for the rate-limited redraw to fire // Wrapped to give time for the rate-limited redraw to fire
setTimeout(function() { setTimeout(function() {
o(onupdate.callCount).equals(1) o(onupdate.callCount).equals(1)
done() done()
}, FRAME_BUDGET) }, FRAME_BUDGET * 2)
})
}) })
o("event handlers can skip redraw", function(done) { o("event handlers can skip redraw", function(done) {
@ -177,21 +178,19 @@ o.spec("route", function() {
} }
}) })
callAsync(function() { root.firstChild.dispatchEvent(e)
root.firstChild.dispatchEvent(e)
o(oninit.callCount).equals(1) o(oninit.callCount).equals(1)
// Wrapped to ensure no redraw fired // Wrapped to ensure no redraw fired
setTimeout(function() { setTimeout(function() {
o(onupdate.callCount).equals(0) o(onupdate.callCount).equals(0)
done() done()
}, FRAME_BUDGET) }, FRAME_BUDGET)
})
}) })
o("changes location on route.link", function(done) { o("changes location on route.link", function() {
var e = $window.document.createEvent("MouseEvents") var e = $window.document.createEvent("MouseEvents")
e.initEvent("click", true, true) e.initEvent("click", true, true)
@ -213,20 +212,16 @@ o.spec("route", function() {
} }
}) })
callAsync(function() { var slash = prefix[0] === "/" ? "" : "/"
var slash = prefix[0] === "/" ? "" : "/"
o($window.location.href).equals(env.protocol + "//" + (env.hostname === "/" ? "" : env.hostname) + slash + (prefix ? prefix + "/" : "")) o($window.location.href).equals(env.protocol + "//" + (env.hostname === "/" ? "" : env.hostname) + slash + (prefix ? prefix + "/" : ""))
root.firstChild.dispatchEvent(e) root.firstChild.dispatchEvent(e)
o($window.location.href).equals(env.protocol + "//" + (env.hostname === "/" ? "" : env.hostname) + slash + (prefix ? prefix + "/" : "") + "test") o($window.location.href).equals(env.protocol + "//" + (env.hostname === "/" ? "" : env.hostname) + slash + (prefix ? prefix + "/" : "") + "test")
done()
})
}) })
o("accepts RouteResolver", function(done) { o("accepts RouteResolver", function() {
var matchCount = 0 var matchCount = 0
var renderCount = 0 var renderCount = 0
var Component = { var Component = {
@ -235,7 +230,7 @@ o.spec("route", function() {
} }
} }
$window.location.href = prefix + "/" $window.location.href = prefix + "/abc"
route(root, "/abc", { route(root, "/abc", {
"/:id" : { "/:id" : {
onmatch: function(resolve, args, requestedPath) { onmatch: function(resolve, args, requestedPath) {
@ -256,16 +251,12 @@ o.spec("route", function() {
}, },
}) })
setTimeout(function() { o(matchCount).equals(1)
o(matchCount).equals(1) o(renderCount).equals(1)
o(renderCount).equals(1) o(root.firstChild.nodeName).equals("DIV")
o(root.firstChild.nodeName).equals("DIV")
done()
}, FRAME_BUDGET)
}) })
o("accepts RouteResolver without `render` method as payload", function(done) { o("accepts RouteResolver without `render` method as payload", function() {
var matchCount = 0 var matchCount = 0
var Component = { var Component = {
view: function() { view: function() {
@ -273,7 +264,7 @@ o.spec("route", function() {
} }
} }
$window.location.href = prefix + "/" $window.location.href = prefix + "/abc"
route(root, "/abc", { route(root, "/abc", {
"/:id" : { "/:id" : {
onmatch: function(resolve, args, requestedPath) { onmatch: function(resolve, args, requestedPath) {
@ -287,16 +278,12 @@ o.spec("route", function() {
}, },
}) })
setTimeout(function() { o(matchCount).equals(1)
o(matchCount).equals(1)
o(root.firstChild.nodeName).equals("DIV") o(root.firstChild.nodeName).equals("DIV")
done()
}, FRAME_BUDGET)
}) })
o("accepts RouteResolver without `onmatch` method as payload", function(done) { o("accepts RouteResolver without `onmatch` method as payload", function() {
var renderCount = 0 var renderCount = 0
var Component = { var Component = {
view: function() { view: function() {
@ -304,7 +291,7 @@ o.spec("route", function() {
} }
} }
$window.location.href = prefix + "/" $window.location.href = prefix + "/abc"
route(root, "/abc", { route(root, "/abc", {
"/:id" : { "/:id" : {
render: function(vnode) { render: function(vnode) {
@ -317,16 +304,10 @@ o.spec("route", function() {
}, },
}) })
setTimeout(function() { o(root.firstChild.nodeName).equals("DIV")
o(root.firstChild.nodeName).equals("DIV")
done()
}, FRAME_BUDGET)
}) })
o("RouteResolver `render` does not have component semantics", function(done, timeout) { o("RouteResolver `render` does not have component semantics", function(done) {
timeout(60)
var renderCount = 0 var renderCount = 0
var A = { var A = {
view: function() { view: function() {
@ -334,7 +315,7 @@ o.spec("route", function() {
} }
} }
$window.location.href = prefix + "/" $window.location.href = prefix + "/a"
route(root, "/a", { route(root, "/a", {
"/a" : { "/a" : {
render: function(vnode) { render: function(vnode) {
@ -348,21 +329,19 @@ o.spec("route", function() {
}, },
}) })
var dom = root.firstChild
o(root.firstChild.nodeName).equals("DIV")
route.set("/b")
setTimeout(function() { setTimeout(function() {
var dom = root.firstChild o(root.firstChild).equals(dom)
o(root.firstChild.nodeName).equals("DIV")
route.set("/b") done()
setTimeout(function() {
o(root.firstChild).equals(dom)
done()
}, FRAME_BUDGET)
}, FRAME_BUDGET) }, FRAME_BUDGET)
}) })
o("calls onmatch and view correct number of times", function(done) { o("calls onmatch and view correct number of times", function() {
var matchCount = 0 var matchCount = 0
var renderCount = 0 var renderCount = 0
var Component = { var Component = {
@ -385,25 +364,19 @@ o.spec("route", function() {
}, },
}) })
callAsync(function() { o(matchCount).equals(1)
o(matchCount).equals(1) o(renderCount).equals(1)
o(renderCount).equals(1)
redraw.publish() redraw.publish(true)
setTimeout(function() { o(matchCount).equals(1)
o(matchCount).equals(1) o(renderCount).equals(2)
o(renderCount).equals(2)
done()
}, FRAME_BUDGET)
})
}) })
o("onmatch can redirect to another route", function(done) { o("onmatch can redirect to another route", function(done) {
var redirected = false var redirected = false
$window.location.href = prefix + "/" $window.location.href = prefix + "/a"
route(root, "/a", { route(root, "/a", {
"/a" : { "/a" : {
onmatch: function() { onmatch: function() {
@ -427,7 +400,7 @@ o.spec("route", function() {
o("onmatch can redirect to another route that has RouteResolver", function(done) { o("onmatch can redirect to another route that has RouteResolver", function(done) {
var redirected = false var redirected = false
$window.location.href = prefix + "/" $window.location.href = prefix + "/a"
route(root, "/a", { route(root, "/a", {
"/a" : { "/a" : {
onmatch: function() { onmatch: function() {
@ -477,59 +450,35 @@ o.spec("route", function() {
}, FRAME_BUDGET) }, FRAME_BUDGET)
}) })
o("the previous view redraws while onmatch resolution is pending (#1268)", function(done, timeout) { o("the previous view redraws while onmatch resolution is pending (#1268)", function(done) {
timeout(FRAME_BUDGET * 5)
var view = o.spy() var view = o.spy()
var onmatch = o.spy() var onmatch = o.spy()
$window.location.href = prefix + "/" $window.location.href = prefix + "/a"
route(root, "/", { route(root, "/", {
"/": {view: view}, "/a": {view: view},
"/2": {onmatch: onmatch} "/b": {onmatch: onmatch}
}) })
setTimeout(function() { o(view.callCount).equals(1)
o(view.callCount).equals(1) o(onmatch.callCount).equals(0)
o(onmatch.callCount).equals(0)
route.set("/b")
route.set("/2")
setTimeout(function(){
setTimeout(function(){
o(view.callCount).equals(1)
o(onmatch.callCount).equals(1)
redraw.publish()
setTimeout(function() {
o(view.callCount).equals(2)
o(onmatch.callCount).equals(1)
done()
}, FRAME_BUDGET)
}, FRAME_BUDGET)
}, FRAME_BUDGET)
})
o("routed mount points can redraw synchronoulsy (#1275)", function(done) {
var view = o.spy()
$window.location.href = prefix + "/"
route(root, "/", {"/":{view:view}})
setTimeout(function() {
o(view.callCount).equals(1) o(view.callCount).equals(1)
o(onmatch.callCount).equals(1)
redraw.publish(true) redraw.publish(true)
o(view.callCount).equals(2) o(view.callCount).equals(2)
o(onmatch.callCount).equals(1)
done() done()
}, FRAME_BUDGET) }, FRAME_BUDGET)
}) })
o("m.route.set(m.route.get()) re-runs the resolution logic (#1180)", function(done, timeout){ o("m.route.set(m.route.get()) re-runs the resolution logic (#1180)", function(done){
timeout(FRAME_BUDGET * 3)
var onmatch = o.spy(function(resolve){resolve()}) var onmatch = o.spy(function(resolve){resolve()})
$window.location.href = prefix + "/" $window.location.href = prefix + "/"
@ -540,16 +489,14 @@ o.spec("route", function() {
} }
}) })
o(onmatch.callCount).equals(1)
route.set(route.get())
setTimeout(function() { setTimeout(function() {
o(onmatch.callCount).equals(1) o(onmatch.callCount).equals(2)
route.set(route.get()) done()
setTimeout(function() {
o(onmatch.callCount).equals(2)
done()
}, FRAME_BUDGET)
}, FRAME_BUDGET) }, FRAME_BUDGET)
}) })
@ -562,19 +509,18 @@ o.spec("route", function() {
}) })
setTimeout(function() { o(route.get()).equals("/")
o(route.get()).equals("/")
route.set("/2")
route.set("/2")
o(route.get()).equals("/")
done() setTimeout(function(){
o(route.get()).equals("/")
done()
}, FRAME_BUDGET) }, FRAME_BUDGET)
}) })
o("routing with RouteResolver works more than once (#1286)", function(done, timeout){ o("routing with RouteResolver works more than once (#1286)", function(done, timeout){
timeout(FRAME_BUDGET * 4) timeout(FRAME_BUDGET * 3)
$window.location.href = prefix + "/a" $window.location.href = prefix + "/a"
route(root, '/a', { route(root, '/a', {
@ -590,26 +536,24 @@ o.spec("route", function() {
} }
}) })
route.set('/b')
setTimeout(function(){ setTimeout(function(){
route.set('/b') route.set('/a')
setTimeout(function(){ setTimeout(function(){
route.set('/a') o(root.firstChild.nodeName).equals("A")
setTimeout(function(){ done()
o(root.firstChild.nodeName).equals("A")
done()
}, FRAME_BUDGET)
}, FRAME_BUDGET) }, FRAME_BUDGET)
}, FRAME_BUDGET) }, FRAME_BUDGET)
}) })
o("calling route.set invalidates pending onmatch resolution", function(done, timeout) { o("calling route.set invalidates pending onmatch resolution", function(done, timeout) {
timeout(100) timeout(50)
var resolved var resolved
$window.location.href = prefix + "/" $window.location.href = prefix + "/a"
route(root, "/a", { route(root, "/a", {
"/a": { "/a": {
onmatch: function(resolve) { onmatch: function(resolve) {
@ -621,15 +565,14 @@ o.spec("route", function() {
view: function() {resolved = "b"} view: function() {resolved = "b"}
} }
}) })
route.set("/b")
setTimeout(function() { setTimeout(function() {
route.set("/b") o(resolved).equals("b")
setTimeout(function() { done()
o(resolved).equals("b") }, 30)
done()
}, 30)
}, FRAME_BUDGET)
}) })
}) })
}) })

View file

@ -285,18 +285,14 @@ o.spec("Router.defineRoutes", function() {
}) })
}) })
o("replays", function(done) { o("replays", function() {
$window.location.href = prefix + "/test" $window.location.href = prefix + "/test"
var replay = router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail) var replay = router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)
replay() replay()
callAsync(function() { o(onRouteChange.callCount).equals(2)
o(onRouteChange.callCount).equals(2) o(onRouteChange.args).deepEquals([{data: 1}, {}, "/test", "/test"])
o(onRouteChange.args).deepEquals([{data: 1}, {}, "/test", "/test"]) o(onFail.callCount).equals(0)
o(onFail.callCount).equals(0)
done()
})
}) })
}) })
}) })