Socket
Book a DemoInstallSign in
Socket

@jedmao/location

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jedmao/location

A Location class that implements the Location interface of the Web API.

3.0.0
latest
Source
npmnpm
Version published
Weekly downloads
165K
14.96%
Maintainers
1
Weekly downloads
 
Created
Source

@jedmao/location

GitHub Actions NPM version npm license codecov BundlePhobia Minified BundlePhobia Minified + gzip code style: prettier Unicorn Approved

A LocationMock class that extends URL and implements the Location interface of the Web API. As always, with first-class TypeScript support!

Installation

npm i --save-dev @jedmao/location

Usage

import { LocationMock } from '@jedmao/location'

const loc = new LocationMock('http://foo.com/')
loc.assign('http://bar.com/')
loc.replace('http://baz.com/')
loc.reload()

Because this package extends URL, many features are provided for free. This means you can do this:

new LocationMock('http://jed:secret@test:42/foo?bar=baz#qux')

Which returns the following object:

LocationMock {
  href: 'http://jed:secret@test:42/foo?bar=baz#qux',
  origin: 'http://test:42',
  protocol: 'http:',
  username: 'jed',
  password: 'secret',
  host: 'test:42',
  hostname: 'test',
  port: '42',
  pathname: '/foo',
  search: '?bar=baz',
  searchParams: URLSearchParams { 'bar' => 'baz' },
  hash: '#qux' }

Mocking

A common use for this package is to mock the window.location, which you can do in Jest like so:

const { location: savedLocation } = window

beforeAll(() => {
  delete window.location
})

beforeEach(() => {
  window.location = new LocationMock('http://test/')
})

afterAll(() => {
  window.location = savedLocation
})

it('assigns /login', () => {
  const assign = jest
    .spyOn(window.location, 'assign')
    .mockImplementationOnce(() => {})

  window.location.assign('/login')

  expect(assign).toHaveBeenCalledWith('/login')
})

Keywords

location

FAQs

Package last updated on 08 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.