preactjs / preact-iso

Isomorphic utilities for Preact
MIT License
69 stars 9 forks source link

LocationProvider type is missing url prop #41

Closed florianeckerstorfer closed 2 weeks ago

florianeckerstorfer commented 2 weeks ago

I'm trying to test a component that uses useLocation with @testing-library/preact and I found no information in the docs on how to mock the location. After looking through the source code I figured out I can just provide the URL to the LocationProvider:

render(
  <LocationProvider url="/some/path">
    <My Component />
  </LocationProvider>
);

However, since url is missing from the Typescript type I get an TypeError in my code. Is there a reason why the prop is missing from the type or should it be added?

rschristian commented 2 weeks ago

Not missing; it was hastily added and the prop doesn't fully work. It'll be removed entirely in the next version.

Instead, patch globalThis.location, it's far more robust and ensures better compatibility with components.

https://github.com/preactjs/preact-iso/blob/4e94fb23bdaa71b7f222548b87df6a28530be5ab/src/prerender.js#L44-L59

I need to publish a new version of iso, but for the meantime, you can just copy/paste this.

florianeckerstorfer commented 2 weeks ago

Thanks for the response, works like a charm.

rschristian commented 2 weeks ago

Utility is available in 2.7.0

florianeckerstorfer commented 2 weeks ago

@rschristian Wow, that was fast. Thank you ❤️

rschristian commented 1 week ago

No prob, should've gotten that out sooner tbh! Glad it helps though