Skip to content

Commit

Permalink
fix(mock-doc): set hostname when location is updated (#2689)
Browse files Browse the repository at this point in the history
* fix(mock-doc): set hostname when location is updated

The hostname was not updated when a new href is set on the window location, it looks like this was an oversight, this commit makes it so the hostname is populated like everything else.

* chore(test): add test for MockLocation behaviour
  • Loading branch information
cam-narzt committed Oct 14, 2020
1 parent e8ced45 commit 9598a05
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/mock-doc/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class MockLocation implements Location {
this._href = url.href;
this.protocol = url.protocol;
this.host = url.host;
this.hostname = url.hostname;
this.port = url.port;
this.pathname = url.pathname;
this.search = url.search;
Expand Down
95 changes: 95 additions & 0 deletions src/mock-doc/test/location.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { MockWindow } from '../window';

describe('location.href', () => {
let win: MockWindow;
beforeEach(() => {
win = new MockWindow(`
<html>
<head>
</head>
</head>
`);
win.location.href = "http://stencil:secret@stenciljs.com:3000/path/to/page?var=var#hash"
});

it('window.location.href', () => {
expect(win.location.href).toBe("http://stencil:secret@stenciljs.com:3000/path/to/page?var=var#hash");
});
it('window.location.protocol', () => {
expect(win.location.protocol).toBe("http:");
});
it('window.location.host', () => {
expect(win.location.host).toBe("stenciljs.com:3000");
});
it('window.location.hostname', () => {
expect(win.location.hostname).toBe("stenciljs.com");
});
it('window.location.port', () => {
expect(win.location.port).toBe("3000");
});
it('window.location.pathname', () => {
expect(win.location.pathname).toBe("/path/to/page");
});
it('window.location.search', () => {
expect(win.location.search).toBe("?var=var");
});
it('window.location.hash', () => {
expect(win.location.hash).toBe("#hash");
});
it('window.location.username', () => {
expect(win.location.username).toBe("stencil");
});
it('window.location.password', () => {
expect(win.location.password).toBe("secret");
});
it('window.location.origin', () => {
expect(win.location.origin).toBe("http://stenciljs.com:3000");
});
});

describe('location', () => {
let win: MockWindow;
beforeEach(() => {
win = new MockWindow(`
<html>
<head>
</head>
</head>
`);
win.location = "http://stencil:secret@stenciljs.com:3000/path/to/page?var=var#hash"
});

it('window.location.href', () => {
expect(win.location.href).toBe("http://stencil:secret@stenciljs.com:3000/path/to/page?var=var#hash");
});
it('window.location.protocol', () => {
expect(win.location.protocol).toBe("http:");
});
it('window.location.host', () => {
expect(win.location.host).toBe("stenciljs.com:3000");
});
it('window.location.hostname', () => {
expect(win.location.hostname).toBe("stenciljs.com");
});
it('window.location.port', () => {
expect(win.location.port).toBe("3000");
});
it('window.location.pathname', () => {
expect(win.location.pathname).toBe("/path/to/page");
});
it('window.location.search', () => {
expect(win.location.search).toBe("?var=var");
});
it('window.location.hash', () => {
expect(win.location.hash).toBe("#hash");
});
it('window.location.username', () => {
expect(win.location.username).toBe("stencil");
});
it('window.location.password', () => {
expect(win.location.password).toBe("secret");
});
it('window.location.origin', () => {
expect(win.location.origin).toBe("http://stenciljs.com:3000");
});
});

0 comments on commit 9598a05

Please sign in to comment.