From 83ab991102898e2ebea0918ca3a0b2c16c476231 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 8 Jan 2024 02:20:05 -0500 Subject: [PATCH] fix(element): ensure props.children is an array in createElement --- src/element/__snapshots__/create.test.ts.snap | 37 +++++++++++++++++++ src/element/create.test.ts | 3 ++ src/element/create.ts | 4 ++ 3 files changed, 44 insertions(+) diff --git a/src/element/__snapshots__/create.test.ts.snap b/src/element/__snapshots__/create.test.ts.snap index 433b9a4b..0b68a0fc 100644 --- a/src/element/__snapshots__/create.test.ts.snap +++ b/src/element/__snapshots__/create.test.ts.snap @@ -80,3 +80,40 @@ exports[`creates element [Function Component] 6`] = ` "type": [Function], } `; + +exports[`creates element [Function Component] 7`] = ` +{ + "key": null, + "props": { + "children": [ + [Function], + [Function], + ], + }, + "type": [Function], +} +`; + +exports[`creates element [Function Component] 8`] = ` +{ + "key": null, + "props": { + "children": [ + [Function], + ], + }, + "type": [Function], +} +`; + +exports[`creates element [Function Component] 9`] = ` +{ + "key": null, + "props": { + "children": [ + [Function], + ], + }, + "type": [Function], +} +`; diff --git a/src/element/create.test.ts b/src/element/create.test.ts index 4328472a..ff800a5c 100644 --- a/src/element/create.test.ts +++ b/src/element/create.test.ts @@ -10,6 +10,9 @@ it.each([ [Component, { style: { width: 100, height: 200 } }], [Component, { style: { width: 100, height: 200 } }, null], [Component, { style: { width: 100, height: 200 } }, Component], + [Component, {}, Component, Component], + [Component, { children: Component }], + [Component, { children: [Component] }], ])('creates element %p', (...args) => { expect( // eslint-disable-next-line prefer-spread diff --git a/src/element/create.ts b/src/element/create.ts index 97e4ec83..da3573f3 100644 --- a/src/element/create.ts +++ b/src/element/create.ts @@ -23,6 +23,10 @@ export function createElement( props.children = children; } + if (props.children && !Array.isArray(props.children)) { + props.children = [props.children]; + } + return { type, props,