Comparing version 0.2.0 to 0.2.1
@@ -50,5 +50,5 @@ 'use strict'; | ||
window.addEventListener('popstate', function (event) { | ||
store.redirect(window.location.pathname); | ||
store.redirect(window.location.pathname + window.location.search + window.location.hash); | ||
}); | ||
module.exports = store; |
{ | ||
"name": "rootr", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Router for single page app.", | ||
@@ -5,0 +5,0 @@ "repository": { |
61
test.js
// mock browser globals | ||
var parseURL = require('url').parse; | ||
var historyQueue = [] | ||
@@ -18,12 +19,10 @@ | ||
function* goBack() { | ||
var item = historyQueue[historyQueue.length-2] | ||
window.location.pathname = item.path | ||
window.location.search = item.search | ||
windowCallbacks.popstate(item) | ||
historyQueue = historyQueue.slice(0, -1) | ||
// yield 3 times; once for each promise in redirectPath | ||
yield Promise.resolve() | ||
yield Promise.resolve() | ||
yield Promise.resolve() | ||
//console.log(historyQueue.length) | ||
var current = historyQueue.pop() | ||
var previous = historyQueue.pop() | ||
window.location.pathname = previous.pathname | ||
window.location.search = previous.search | ||
window.location.hash = previous.hash | ||
windowCallbacks.popstate(previous) | ||
} | ||
@@ -35,27 +34,9 @@ | ||
function parsePath(path) { | ||
var tmp = path.split('?') | ||
let pathname = tmp[0]; | ||
let search = tmp[1] || ''; | ||
let hash = ''; | ||
if (search && search.indexOf('#') > -1){ | ||
[search, hash] = search.split('#') | ||
} | ||
search = `?${search}` | ||
hash = `#${hash}` | ||
return { | ||
pathname, search, hash | ||
} | ||
} | ||
global.history = { | ||
pushState: (state, title, path) => { | ||
window.location = parsePath(path) | ||
historyQueue[historyQueue.length-1] = { state, title, path: window.location.pathname, search: window.location.search, hash: window.location.search } | ||
pushState: (state, title, url) => { | ||
window.location = parseURL(url) | ||
historyQueue.push(window.location); | ||
}, | ||
replaceState: (state, title, path) => { | ||
window.location = parsePath(path) | ||
replaceState: (state, title, url) => { | ||
window.location = parseURL(url) | ||
}, | ||
@@ -157,1 +138,17 @@ } | ||
}); | ||
test( 'goBack works correctly', function*(t) { | ||
t.plan(3) | ||
var result = yield location.open('/admin?foo=1#bar') | ||
yield location.open('/admin2') | ||
yield goBack(); | ||
var state = location.getState(); | ||
t.equal( state.pathname, '/admin') | ||
t.equal( state.search, '?foo=1') | ||
t.equal( state.hash, '#bar') | ||
}); |
110426
301