https://github.com/jednano/location
A Location class that implements the Location interface of the Web API.
https://github.com/jednano/location
api location typescript typescript-library web web-api
Last synced: 3 months ago
JSON representation
A Location class that implements the Location interface of the Web API.
- Host: GitHub
- URL: https://github.com/jednano/location
- Owner: jednano
- License: mit
- Created: 2019-02-23T06:36:25.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T16:41:21.000Z (over 3 years ago)
- Last Synced: 2026-03-03T04:29:44.837Z (4 months ago)
- Topics: api, location, typescript, typescript-library, web, web-api
- Language: TypeScript
- Size: 1.44 MB
- Stars: 10
- Watchers: 1
- Forks: 2
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @jedmao/location
[](https://github.com/jedmao/location/actions)
[](https://www.npmjs.org/package/@jedmao/location)
[](https://www.npmjs.org/package/@jedmao/location)
[](https://codecov.io/gh/jedmao/location)
[](https://bundlephobia.com/result?p=@jedmao/location)
[](https://bundlephobia.com/result?p=@jedmao/location)
[](https://github.com/prettier/prettier)
[](https://twitter.com/sindresorhus/status/457989012528316416?ref_src=twsrc%5Etfw&ref_url=https%3A%2F%2Fwww.quora.com%2FWhat-does-the-unicorn-approved-shield-mean-in-GitHub)
A `LocationMock` class that extends [`URL`][] and implements the [`Location interface of the Web API`](https://developer.mozilla.org/en-US/docs/Web/API/Location). As always, with first-class TypeScript support!
## Installation
```bash
npm i --save-dev @jedmao/location
```
## Usage
```ts
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:
```ts
new LocationMock('http://jed:secret@test:42/foo?bar=baz#qux')
```
Which returns the following object:
```ts
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](https://jestjs.io/) like so:
```ts
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')
})
```
[`url`]: https://developer.mozilla.org/en-US/docs/Web/API/URL