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 = ""
@ -46,8 +47,8 @@ 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: { $window.location = {
get protocol() { get protocol() {
return protocol return protocol
}, },
@ -132,8 +133,8 @@ module.exports = function(options) {
past.push({url: url, isNew: isNew}) past.push({url: url, isNew: isNew})
future = [] future = []
}, },
}, }
history: { $window.history = {
pushState: function(data, title, url) { pushState: function(data, title, url) {
past.push({url: getURL(), isNew: false}) past.push({url: getURL(), isNew: false})
future = [] future = []
@ -161,10 +162,10 @@ module.exports = function(options) {
if (!entry.isNew) popstate() if (!entry.isNew) popstate()
} }
}, },
},
onpopstate: null,
onhashchange: null,
onunload: null,
} }
$window.onpopstate = null,
$window.onhashchange = null,
$window.onunload = null
return $window return $window
} }