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

Assertions on window.location.href changes from react-router-dom no longer work with happy-dom 14.12.1 and above #1479

Open
ahayes91 opened this issue Jul 5, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@ahayes91
Copy link

ahayes91 commented Jul 5, 2024

Describe the bug
We have some tests that clicking on React Router Dom links and assert that the window.location.href has been updated accordingly in the test.
When using happy-dom 14.12.0, these tests pass, but with happy-dom 14.12.1 and above, these tests fail.

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://stackblitz.com/edit/vitejs-vite-ib4ffy?file=package.json StackBlitz.
  2. Run npm i and npm run test in the terminal.
  3. See the test fails
  4. Change the happy-dom version in package.json to "happy-dom":"14.12.0"
  5. See the test passes

Expected behavior
Tests should still pass on latest happy-dom version

Screenshots
N/A

Device:
N/A

Additional context
This issue looks related to #1465.

We could also argue that assertions on the window.location.href globals aren't advisable in any case, and there are alternative ways to test the Link URL / routing change (we can see that the change in the Route by the Link in MemoryRouter of that repo is correctly detected with the assertion expect(await screen.findByText('This should show TODOs')).toBeInTheDocument();, no matter what happy-dom version is being used).
However, it is handy for very small unit tests on components with Links where you don't want to render the full DOM of a Routes app.
And even if we decide not to fix this issue, it would be good to understand where it is originating from and why the change in that PR has caused this.

Thank you!

@ahayes91 ahayes91 added the bug Something isn't working label Jul 5, 2024
@dteoh
Copy link

dteoh commented Nov 5, 2024

I am also experiencing this issue after upgrading.

In my situation I am not using a framework like React.

I have to change the location being loaded based on some dynamic condition, and I was doing through something like:

function clickHandler(e) {
  e.preventDefault();
  if (somethingHappened) {
    window.location.href = "/another-url";
  } else {
    executeSomething();
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants