The browserMock $window must be reachable from the pushStateMock

This commit is contained in:
Pierre-Yves Gerardy 2016-12-07 13:42:27 +01:00
parent 7cab626980
commit bd79297905
2 changed files with 120 additions and 119 deletions

View file

@ -5,14 +5,14 @@ var domMock = require("./domMock")
var xhrMock = require("./xhrMock") var xhrMock = require("./xhrMock")
module.exports = function(env) { module.exports = function(env) {
var $window = {} env = env || {}
var $window = env.window = {}
var dom = domMock() var dom = domMock()
var xhr = xhrMock() var xhr = xhrMock()
var ps = pushStateMock(env)
for (var key in dom) if (!$window[key]) $window[key] = dom[key] for (var key in dom) if (!$window[key]) $window[key] = dom[key]
for (var key in xhr) if (!$window[key]) $window[key] = xhr[key] for (var key in xhr) if (!$window[key]) $window[key] = xhr[key]
for (var key in ps) if (!$window[key]) $window[key] = ps[key] pushStateMock(env)
return $window return $window
} }

View file

@ -5,6 +5,7 @@ var parseURL = require("../test-utils/parseURL")
module.exports = function(options) { module.exports = function(options) {
if (options == null) options = {} if (options == null) options = {}
var $window = options.window || {}
var protocol = options.protocol || "http:" var protocol = options.protocol || "http:"
var hostname = options.hostname || "localhost" var hostname = options.hostname || "localhost"
var port = "" var port = ""
@ -32,7 +33,7 @@ module.exports = function(options) {
} }
return isNew return isNew
} }
function prefix(prefix, value) { function prefix(prefix, value) {
if (value === "") return "" if (value === "") return ""
return (value.charAt(0) !== prefix ? prefix : "") + value return (value.charAt(0) !== prefix ? prefix : "") + value
@ -46,125 +47,125 @@ module.exports = function(options) {
function unload() { function unload() {
if (typeof $window.onunload === "function") $window.onunload({type: "unload"}) if (typeof $window.onunload === "function") $window.onunload({type: "unload"})
} }
var $window = {
location: {
get protocol() {
return protocol
},
get hostname() {
return hostname
},
get port() {
return port
},
get pathname() {
return pathname
},
get search() {
return search
},
get hash() {
return hash
},
get origin() {
if (protocol === "file:") return "null"
return protocol + "//" + hostname + prefix(":", port)
},
get host() {
if (protocol === "file:") return ""
return hostname + prefix(":", port)
},
get href() {
return getURL()
},
set protocol(value) { $window.location = {
throw new Error("Protocol is read-only") get protocol() {
}, return protocol
set hostname(value) { },
unload() get hostname() {
past.push({url: getURL(), isNew: true}) return hostname
future = [] },
hostname = value get port() {
}, return port
set port(value) { },
if (protocol === "file:") throw new Error("Port is read-only under `file://` protocol") get pathname() {
unload() return pathname
past.push({url: getURL(), isNew: true}) },
future = [] get search() {
port = value return search
}, },
set pathname(value) { get hash() {
if (protocol === "file:") throw new Error("Pathname is read-only under `file://` protocol") return hash
unload() },
past.push({url: getURL(), isNew: true}) get origin() {
future = [] if (protocol === "file:") return "null"
pathname = prefix("/", value) return protocol + "//" + hostname + prefix(":", port)
}, },
set search(value) { get host() {
unload() if (protocol === "file:") return ""
past.push({url: getURL(), isNew: true}) return hostname + prefix(":", port)
future = [] },
search = prefix("?", value) get href() {
}, return getURL()
set hash(value) { },
var oldHash = hash
past.push({url: getURL(), isNew: false})
future = []
hash = prefix("#", value)
if (oldHash != hash) hashchange()
},
set origin(value) { set protocol(value) {
//origin is writable but ignored throw new Error("Protocol is read-only")
},
set host(value) {
//host is writable but ignored in Chrome
},
set href(value) {
var url = getURL()
var isNew = setURL(value)
if (isNew) {
setURL(url)
unload()
setURL(value)
}
past.push({url: url, isNew: isNew})
future = []
},
}, },
history: { set hostname(value) {
pushState: function(data, title, url) { unload()
past.push({url: getURL(), isNew: false}) past.push({url: getURL(), isNew: true})
future = [] future = []
setURL(url) hostname = value
}, },
replaceState: function(data, title, url) { set port(value) {
future = [] if (protocol === "file:") throw new Error("Port is read-only under `file://` protocol")
setURL(url) unload()
}, past.push({url: getURL(), isNew: true})
back: function() { future = []
var entry = past.pop() port = value
if (entry != null) { },
if (entry.isNew) unload() set pathname(value) {
future.push({url: getURL(), isNew: false}) if (protocol === "file:") throw new Error("Pathname is read-only under `file://` protocol")
setURL(entry.url) unload()
if (!entry.isNew) popstate() past.push({url: getURL(), isNew: true})
} future = []
}, pathname = prefix("/", value)
forward: function() { },
var entry = future.pop() set search(value) {
if (entry != null) { unload()
if (entry.isNew) unload() past.push({url: getURL(), isNew: true})
past.push({url: getURL(), isNew: false}) future = []
setURL(entry.url) search = prefix("?", value)
if (!entry.isNew) popstate() },
} set hash(value) {
}, var oldHash = hash
past.push({url: getURL(), isNew: false})
future = []
hash = prefix("#", value)
if (oldHash != hash) hashchange()
},
set origin(value) {
//origin is writable but ignored
},
set host(value) {
//host is writable but ignored in Chrome
},
set href(value) {
var url = getURL()
var isNew = setURL(value)
if (isNew) {
setURL(url)
unload()
setURL(value)
}
past.push({url: url, isNew: isNew})
future = []
}, },
onpopstate: null,
onhashchange: null,
onunload: null,
} }
$window.history = {
pushState: function(data, title, url) {
past.push({url: getURL(), isNew: false})
future = []
setURL(url)
},
replaceState: function(data, title, url) {
future = []
setURL(url)
},
back: function() {
var entry = past.pop()
if (entry != null) {
if (entry.isNew) unload()
future.push({url: getURL(), isNew: false})
setURL(entry.url)
if (!entry.isNew) popstate()
}
},
forward: function() {
var entry = future.pop()
if (entry != null) {
if (entry.isNew) unload()
past.push({url: getURL(), isNew: false})
setURL(entry.url)
if (!entry.isNew) popstate()
}
},
}
$window.onpopstate = null,
$window.onhashchange = null,
$window.onunload = null
return $window return $window
} }