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

Fix crash when passing array children to preact-iso #555

Merged
merged 1 commit into from
Apr 26, 2021
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
5 changes: 5 additions & 0 deletions .changeset/thirty-poets-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-iso': patch
---

Fix crash when passing dynamic arrays as children. This was caused by missing children normalization.
4 changes: 2 additions & 2 deletions packages/preact-iso/router.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, createContext, cloneElement } from 'preact';
import { h, createContext, cloneElement, toChildArray } from 'preact';
import { useContext, useMemo, useReducer, useEffect, useLayoutEffect, useRef } from 'preact/hooks';

let push;
Expand Down Expand Up @@ -106,7 +106,7 @@ export function Router(props) {
prev.current = cur.current;

let p, d, m;
[].concat(props.children || []).some(vnode => {
toChildArray(props.children).some(vnode => {
const matches = exec(path, vnode.props.path, (m = { path, query }));
if (matches) return (p = cloneElement(vnode, m));
if (vnode.props.default) d = cloneElement(vnode, m);
Expand Down
34 changes: 34 additions & 0 deletions packages/preact-iso/test/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,38 @@ describe('Router', () => {

pushState.mockRestore();
});

it('should normalize children', async () => {
let loc;
const pushState = jest.spyOn(history, 'pushState');
const Route = jest.fn(() => html`<a href="/foo#foo">foo</a>`);

const routes = ['/foo', '/bar'];
render(
html`
<${LocationProvider}>
<${Router}>
${routes.map(route => html`<${Route} path=${route} />`)}
<${Route} default />
<//>
<${() => {
loc = useLocation();
}} />
<//>
`,
scratch
);

expect(Route).toHaveBeenCalledTimes(1);
Route.mockClear();
await sleep(20);

scratch.querySelector('a[href="/foo#foo"]').click();
await sleep(100);
expect(Route).toHaveBeenCalledTimes(1);
expect(loc).toMatchObject({ url: '/foo#foo', path: '/foo' });
expect(pushState).toHaveBeenCalled();

pushState.mockRestore();
});
});