Created
August 7, 2023 14:18
-
-
Save lebbe/b75509dd0f48fd0f6ef5bc143fbe5e3e to your computer and use it in GitHub Desktop.
Location mocker (for use with jest or other test tools)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function mockLocation( | |
href = 'http://www.platform.com/application/', | |
) { | |
delete global.window.location; | |
const url = new URL(href); | |
const noop = () => {}; | |
global.window.location = { | |
ancestorOrigins: null, | |
hash: url.hash, | |
host: url.host, | |
port: url.port, | |
protocol: url.protocol, | |
hostname: url.hostname, | |
href: url.href, | |
origin: url.origin, | |
pathname: url.pathname, | |
search: url.search, | |
assign: noop, | |
reload: noop, | |
replace: noop, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment