Skip to content

Commit

Permalink
chore: [#1101] Adds unit tests for Location
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Feb 27, 2024
1 parent deb3da4 commit 7c48e4e
Showing 1 changed file with 220 additions and 8 deletions.
228 changes: 220 additions & 8 deletions packages/happy-dom/test/location/Location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ const HREF = 'https://google.com/some-path/?key=value&key2=value2#hash';

describe('Location', () => {
let browserFrame: IBrowserFrame;
let location: Location;

beforeEach(() => {
browserFrame = new BrowserFrame(new Browser().newPage());
location = new Location(browserFrame, 'about:blank');
});

describe('get hash()', () => {
it('Returns the hash of the URL.', () => {
location = new Location(
const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);
Expand All @@ -37,7 +35,7 @@ describe('Location', () => {
events.push(<HashChangeEvent>event);
});

location = new Location(
const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2'
);
Expand Down Expand Up @@ -70,8 +68,82 @@ describe('Location', () => {
});
});

describe('get host()', () => {
it('Returns the host of the URL.', () => {
const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);

expect(location.host).toBe('localhost:8080');
});
});

describe('set host()', () => {
it('Sets the host of the URL.', () => {
let newURL: string | null = null;

vi.spyOn(browserFrame, 'goto').mockImplementation(async (url: string) => {
newURL = url;
return null;
});

const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);

location.host = 'localhost:8081';

expect(<string>(<unknown>newURL)).toBe(
'https://localhost:8081/some-path/?key=value&key2=value2#hash'
);
});
});

describe('get hostname()', () => {
it('Returns the hostname of the URL.', () => {
const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);

expect(location.hostname).toBe('localhost');
});
});

describe('set hostname()', () => {
it('Sets the hostname of the URL.', () => {
let newURL: string | null = null;

vi.spyOn(browserFrame, 'goto').mockImplementation(async (url: string) => {
newURL = url;
return null;
});

const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);

location.hostname = 'localhost2';

expect(<string>(<unknown>newURL)).toBe(
'https://localhost2:8080/some-path/?key=value&key2=value2#hash'
);
});
});

describe('get href()', () => {
it('Returns the URL.', () => {
expect(new Location(browserFrame, 'about:blank').href).toBe('about:blank');
expect(new Location(browserFrame, HREF).href).toBe(HREF);
});
});

describe('set href()', () => {
it('Calls browserFrame.goto() to navigate to the URL.', () => {
const location = new Location(browserFrame, 'about:blank');
let calledURL: string | null = null;
let calledOptions: IGoToOptions | undefined = undefined;

Expand All @@ -90,6 +162,8 @@ describe('Location', () => {
});

it('Handles promise rejections.', async () => {
const location = new Location(browserFrame, 'about:blank');

vi.spyOn(browserFrame, 'goto').mockImplementation((): Promise<IResponse | null> => {
return Promise.reject(new Error('Test error'));
});
Expand All @@ -104,15 +178,145 @@ describe('Location', () => {
});
});

describe('get href()', () => {
it('Returns the URL.', () => {
expect(location.href).toBe('about:blank');
expect(new Location(browserFrame, HREF).href).toBe(HREF);
describe('get origin()', () => {
it('Returns the origin of the URL.', () => {
const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);
expect(location.origin).toBe('https://localhost:8080');
});
});

describe('get pathname()', () => {
it('Returns the pathname of the URL.', () => {
const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);
expect(location.pathname).toBe('/some-path/');
});
});

describe('set pathname()', () => {
it('Sets the pathname of the URL.', () => {
let newURL: string | null = null;

vi.spyOn(browserFrame, 'goto').mockImplementation(async (url: string) => {
newURL = url;
return null;
});

const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);

location.pathname = '/some-path2/';

expect(<string>(<unknown>newURL)).toBe(
'https://localhost:8080/some-path2/?key=value&key2=value2#hash'
);
});
});

describe('get port()', () => {
it('Returns the port of the URL.', () => {
const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);
expect(location.port).toBe('8080');
});
});

describe('set port()', () => {
it('Sets the port of the URL.', () => {
let newURL: string | null = null;

vi.spyOn(browserFrame, 'goto').mockImplementation(async (url: string) => {
newURL = url;
return null;
});

const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);

location.port = '8081';

expect(<string>(<unknown>newURL)).toBe(
'https://localhost:8081/some-path/?key=value&key2=value2#hash'
);
});
});

describe('get protocol()', () => {
it('Returns the protocol of the URL.', () => {
const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);
expect(location.protocol).toBe('https:');
});
});

describe('set protocol()', () => {
it('Sets the protocol of the URL.', () => {
let newURL: string | null = null;

vi.spyOn(browserFrame, 'goto').mockImplementation(async (url: string) => {
newURL = url;
return null;
});

const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);

location.protocol = 'http:';

expect(<string>(<unknown>newURL)).toBe(
'http://localhost:8080/some-path/?key=value&key2=value2#hash'
);
});
});

describe('get search()', () => {
it('Returns the search of the URL.', () => {
const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);
expect(location.search).toBe('?key=value&key2=value2');
});
});

describe('set search()', () => {
it('Sets the search of the URL.', () => {
let newURL: string | null = null;

vi.spyOn(browserFrame, 'goto').mockImplementation(async (url: string) => {
newURL = url;
return null;
});

const location = new Location(
browserFrame,
'https://localhost:8080/some-path/?key=value&key2=value2#hash'
);

location.search = '?key3=value3';

expect(<string>(<unknown>newURL)).toBe('https://localhost:8080/some-path/?key3=value3#hash');
});
});

describe('replace()', () => {
it('Calls browserFrame.goto() to navigate to the URL.', () => {
const location = new Location(browserFrame, 'about:blank');
let calledURL: string | null = null;
let calledOptions: IGoToOptions | undefined = undefined;

Expand All @@ -131,6 +335,8 @@ describe('Location', () => {
});

it('Handles promise rejections.', async () => {
const location = new Location(browserFrame, 'about:blank');

vi.spyOn(browserFrame, 'goto').mockImplementation((): Promise<IResponse | null> => {
return Promise.reject(new Error('Test error'));
});
Expand All @@ -147,6 +353,7 @@ describe('Location', () => {

describe('assign()', () => {
it('Calls browserFrame.goto() to navigate to the URL.', () => {
const location = new Location(browserFrame, 'about:blank');
let calledURL: string | null = null;
let calledOptions: IGoToOptions | undefined = undefined;

Expand All @@ -165,6 +372,8 @@ describe('Location', () => {
});

it('Handles promise rejections.', async () => {
const location = new Location(browserFrame, 'about:blank');

vi.spyOn(browserFrame, 'goto').mockImplementation((): Promise<IResponse | null> => {
return Promise.reject(new Error('Test error'));
});
Expand All @@ -181,6 +390,7 @@ describe('Location', () => {

describe('reload()', () => {
it('Reloads the page by calling browserFrame.goto() with the same URL.', () => {
const location = new Location(browserFrame, 'about:blank');
let calledURL: string | null = null;
let calledOptions: IGoToOptions | undefined = undefined;

Expand All @@ -199,6 +409,8 @@ describe('Location', () => {
});

it('Handles promise rejections.', async () => {
const location = new Location(browserFrame, 'about:blank');

vi.spyOn(browserFrame, 'goto').mockImplementation((): Promise<IResponse | null> => {
return Promise.reject(new Error('Test error'));
});
Expand Down

0 comments on commit 7c48e4e

Please sign in to comment.