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
28 changes: 28 additions & 0 deletions integration-tests/fragment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { screen } from "@testing-library/dom";
import userEvent from "@testing-library/user-event";

import { attachComponent, createElement, Fragment, createState } from "../src";
import type { VelesChildren } from "../src/types";

describe("<Fragment>", () => {
let cleanup: Function | undefined;
Expand All @@ -11,6 +12,33 @@ describe("<Fragment>", () => {
cleanup = undefined;
});

test("flattens child arrays passed through a wrapper component", () => {
const visible$ = createState(false);

function Wrapper({ children }: { children?: VelesChildren }) {
return createElement(Fragment, {
children: [children, visible$.render(() => null)] as unknown as VelesChildren,
});
}

function App() {
return createElement(Wrapper, {
children: [
createElement("span", { "data-testid": "first-child", children: "First" }),
createElement("span", { "data-testid": "second-child", children: "Second" }),
],
});
}

cleanup = attachComponent({
htmlElement: document.body,
component: createElement(App),
});

expect(screen.getByTestId("first-child").textContent).toBe("First");
expect(screen.getByTestId("second-child").textContent).toBe("Second");
});

test("supports <Fragment> components rendering correctly", () => {
function App() {
return createElement("div", {
Expand Down
23 changes: 22 additions & 1 deletion src/create-element/parse-children.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ import type {
VelesElementProps,
} from "../types";

type ParsedChild =
| string
| number
| VelesElement
| VelesComponentObject
| VelesStringElement
| undefined
| null;

function flattenChildren(children: unknown): ParsedChild[] {
const flattenedChildren: ParsedChild[] = [];

function append(child: unknown) {
if (Array.isArray(child)) child.forEach(append);
else flattenedChildren.push(child as ParsedChild);
}

append(children);
return flattenedChildren;
}

function parseChildren({
children,
htmlElement,
Expand All @@ -27,7 +48,7 @@ function parseChildren({
// when they are executed
let lastInsertedNode: null | HTMLElement | Text | VelesComponentObject = null;

(Array.isArray(children) ? children : [children]).forEach((childComponent) => {
flattenChildren(children).forEach((childComponent) => {
if (typeof childComponent === "string") {
const textNode = createTextElement(childComponent);
htmlElement.append(textNode.html);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"types": ["vitest/globals", "@testing-library/jest-dom"],
"moduleResolution": "bundler",
"noEmit": true,
"skipLibCheck": true
}
}
3 changes: 3 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
resolve: {
extensions: [".ts", ".tsx", ".mjs", ".js", ".mts", ".jsx", ".json"],
},
test: {
environment: "jsdom",
globals: true,
Expand Down
Loading