make setPath always async

This commit is contained in:
Leo Horie 2016-07-01 13:03:07 -04:00
parent 520d15a060
commit e8e0bae726
10 changed files with 154 additions and 61 deletions

View file

@ -5,6 +5,7 @@ var parseQueryString = require("../querystring/parse")
module.exports = function($window) {
var supportsPushState = typeof $window.history.pushState === "function" && $window.location.protocol !== "file:"
var callAsync = typeof setImmediate === "function" ? setImmediate : setTimeout
var prefix = "#!"
function setPrefix(value) {prefix = value}
@ -60,7 +61,7 @@ module.exports = function($window) {
if (supportsPushState) {
if (options && options.replace) $window.history.replaceState(null, null, prefix + path)
else $window.history.pushState(null, null, prefix + path)
$window.onpopstate()
callAsync($window.onpopstate)
}
else $window.location.href = prefix + path
}

View file

@ -6,6 +6,7 @@
<body>
<script src="../../module/module.js"></script>
<script src="../../ospec/ospec.js"></script>
<script src="../../test-utils/callAsync.js"></script>
<script src="../../test-utils/parseURL.js"></script>
<script src="../../test-utils/pushStateMock.js"></script>

View file

@ -17,6 +17,13 @@ o.spec("Router.defineRoutes", function() {
onFail = o.spy()
})
o("calls onRouteChange on init", function() {
$window.location.href = prefix + "/a"
router.defineRoutes({"/a": {data: 1}}, onRouteChange, onFail)
o(onRouteChange.callCount).equals(1)
})
o("resolves to route", function() {
$window.location.href = prefix + "/test"
router.defineRoutes({"/test": {data: 1}}, onRouteChange, onFail)

View file

@ -1,6 +1,7 @@
"use strict"
var o = require("../../ospec/ospec")
var callAsync = require("../../test-utils/callAsync")
var pushStateMock = require("../../test-utils/pushStateMock")
var Router = require("../../router/router")
@ -17,6 +18,28 @@ o.spec("Router.setPath", function() {
onFail = o.spy()
})
o("setPath calls onRouteChange asynchronously", function(done) {
$window.location.href = prefix + "/a"
router.defineRoutes({"/a": {data: 1}, "/b": {data: 2}}, onRouteChange, onFail)
router.setPath("/b")
o(onRouteChange.callCount).equals(1)
callAsync(function() {
o(onRouteChange.callCount).equals(2)
done()
})
})
o("setPath calls onFail asynchronously", function(done) {
$window.location.href = prefix + "/a"
router.defineRoutes({"/a": {data: 1}, "/b": {data: 2}}, onRouteChange, onFail)
router.setPath("/c")
o(onFail.callCount).equals(0)
callAsync(function() {
o(onFail.callCount).equals(1)
done()
})
})
o("sets route via API", function() {
$window.location.href = prefix + "/test"
router.defineRoutes({"/test": {data: 1}, "/other/:a/:b...": {data: 2}}, onRouteChange, onFail)