Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ export function Router(props) {
const [c, update] = useReducer(c => c + 1, 0);

const { url, query, wasPush, path } = useLocation();
if (!url) {
throw new Error(`preact-iso's <Router> must be used within a <LocationProvider>, see: https://github.com/preactjs/preact-iso#locationprovider`);
}
const { rest = path, params = {} } = useContext(RouteContext);

const isLoading = useRef(false);
Expand Down
18 changes: 17 additions & 1 deletion test/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ describe('Router', () => {
});


it('should throw a clear error if the LocationProvider is missing', () => {
const Home = () => <h1>Home</h1>;

try {
render(
<Router>
<Home path="/" test="2" />
</Router>,
scratch
);
expect.fail('should have thrown');
} catch (e) {
expect(e.message).to.include('must be used within a <LocationProvider>');
}
});

it('should strip trailing slashes from path', async () => {
render(
<LocationProvider url="/a/">
Expand Down Expand Up @@ -953,7 +969,7 @@ describe('Router', () => {
);

shadowlink.click();

await sleep(1);

expect(loc).to.deep.include({ url: '/shadow' });
Expand Down