Skip to content

Commit

Permalink
Merge pull request #555 from preactjs/iso-normalize-children
Browse files Browse the repository at this point in the history
Fix crash when passing array children to preact-iso
  • Loading branch information
marvinhagemeister committed Apr 26, 2021
2 parents 8a2d8d6 + c7ca0a4 commit 5516a7c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
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();
});
});

0 comments on commit 5516a7c

Please sign in to comment.