Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#678@patch: Assign of window.location.href. #685

Merged

Conversation

Mas0nShi
Copy link
Contributor

Fixes #678.


describe('href', () => {
it('Set href.', () => {
location.replace(HREF);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should test against a plain string like /Foo instead of using a valid url

Copy link
Contributor Author

@Mas0nShi Mas0nShi Dec 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you means location.href = '/foo' directly, like this:

const {Window} = require('happy-dom');
const window = new Window();

window.location.href = '/foo'

or

const {Window} = require('happy-dom');
const window = new Window();
window.location.href = ''
window.location.href = '/foo'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. I'm watching this from my phone but it seems that HREF is a complete and valid url.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. I'm watching this from my phone but it seems that HREF is a complete and valid url.

we need have a valid url before we location.href = '/foo', it's same in browser. /foo only a path, it's not valid url. you can set href /foo because it's replace pathname only.
you can test in your browser devtools:

location.href = 'about:blank' // the blank page in browser.

location.href = '/foo'

// Uncaught DOMException: Failed to set the 'href' property on 'Location': '/foo' is not a valid URL.
//    at <anonymous>:1:15

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ilteoood as @Mas0nShi said, the URL needs to be relative to something and by default the URL is set to "about:blank".

Only setting location.href = '/foo' will fail with an error as it is not possible to construct a relative URL from "about:blank". Happy DOM could use a different default value for the URL, but what would that be?

After this fix, you will still have to use a setup script for your tests and set a default URL for the URL:s to be relative from.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added unit tests that demonstrates this.

@hugo-vrijswijk
Copy link

I hate to be that guy, but any update on this? It's currently blocking me from using happy-dom instead of jsdom

@capricorn86
Copy link
Owner

I hate to be that guy, but any update on this? It's currently blocking me from using happy-dom instead of jsdom

@hugo-vrijswijk the build for this PR is failing and I would also like to look into the solution a bit more. I am currently working on #677, which adds support for FormData and implements "fetch" into Happy DOM. It is a bit time consuming. I will look into this as soon as I can 😅

capricorn86
capricorn86 previously approved these changes Apr 12, 2023
@capricorn86 capricorn86 merged commit fdbfc53 into capricorn86:master Apr 12, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

regression: assign of window.location.href
4 participants