Skip to content

Commit

Permalink
fix: [#1568] Accept objects with a stringifier such as a URL object i…
Browse files Browse the repository at this point in the history
…n Location.assign and Location.replace (#1569)

Co-authored-by: David Ortner <[email protected]>
  • Loading branch information
silverwind and capricorn86 authored Nov 6, 2024
1 parent c80a0e9 commit f5e1722
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/happy-dom/src/location/Location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,17 @@ export default class Location {
*
* @param url URL.
*/
public replace(url: string): void {
this.href = url;
public replace(url: string | URL): void {
this.href = String(url);
}

/**
* Loads the resource at the URL provided in parameter.
*
* @param url URL.
*/
public assign(url: string): void {
this.href = url;
public assign(url: string | URL): void {
this.href = String(url);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions packages/happy-dom/test/location/Location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,21 @@ describe('Location', () => {
browserFrame.page.virtualConsolePrinter.readAsString().startsWith('Error: Test error\n')
).toBe(true);
});

it('Accepts url as URL object.', () => {
const location = new Location(browserFrame, 'about:blank');
let calledURL: string | null = null;

vi.spyOn(browserFrame, 'goto').mockImplementation(
async (url: string): Promise<Response | null> => {
calledURL = url;
return null;
}
);

location.assign(new URL(HREF));
expect(calledURL).toBe(HREF);
});
});

describe('reload()', () => {
Expand Down

0 comments on commit f5e1722

Please sign in to comment.