Skip to content

Instantly share code, notes, and snippets.

@lebbe
Created August 7, 2023 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lebbe/b75509dd0f48fd0f6ef5bc143fbe5e3e to your computer and use it in GitHub Desktop.
Save lebbe/b75509dd0f48fd0f6ef5bc143fbe5e3e to your computer and use it in GitHub Desktop.
Location mocker (for use with jest or other test tools)
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