From abdeb3ddfee5f51d78c62f712166d0b70037ce34 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Thu, 4 Apr 2024 08:53:09 +0200 Subject: [PATCH] allow passing props to routes --- src/router.js | 2 +- test/router.test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/router.js b/src/router.js index 748c0db..78aa6d7 100644 --- a/src/router.js +++ b/src/router.js @@ -128,7 +128,7 @@ export function Router(props) { let p, d, m; toChildArray(props.children).some(vnode => { - const matches = exec(rest, vnode.props.path, (m = { path: rest, query, params, rest: '' })); + const matches = exec(rest, vnode.props.path, (m = { ...vnode.props, path: rest, query, params, rest: '' })); if (matches) return (p = cloneElement(vnode, m)); if (vnode.props.default) d = cloneElement(vnode, m); }); diff --git a/test/router.test.js b/test/router.test.js index e1f4bf5..cd4e069 100644 --- a/test/router.test.js +++ b/test/router.test.js @@ -23,6 +23,38 @@ describe('Router', () => { history.replaceState(null, null, '/'); }); + it('should allow passing props to a route', async () => { + const Home = jest.fn(() => html`

Home

`); + const stack = []; + let loc; + render( + html` + <${LocationProvider}> + <${Router} + onRouteChange=${url => { + stack.push(url); + }} + > + <${Home} path="/" test="2" /> + + <${() => { + loc = useLocation(); + }} /> + + `, + scratch + ); + + expect(scratch).toHaveProperty('textContent', 'Home'); + expect(Home).toHaveBeenCalledWith({ path: '/', query: {}, params: {}, rest: '', test: '2' }, expect.anything()); + expect(loc).toMatchObject({ + url: '/', + path: '/', + query: {}, + route: expect.any(Function) + }); + }); + it('should switch between synchronous routes', async () => { const Home = jest.fn(() => html`

Home

`); const Profiles = jest.fn(() => html`

Profiles

`);