summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2021-04-28 14:43:50 -0500
committerGravatar GitHub <noreply@github.com> 2021-04-28 14:43:50 -0500
commit633bb46dc4b83ec39004260ff44ddafa1963cf79 (patch)
tree9524eea0a84858bd353e8e39c633ac9d4829b148
parent6bc51facce11e97392d4830e0f745ea7482c38d4 (diff)
downloadastro-633bb46dc4b83ec39004260ff44ddafa1963cf79.tar.gz
astro-633bb46dc4b83ec39004260ff44ddafa1963cf79.tar.zst
astro-633bb46dc4b83ec39004260ff44ddafa1963cf79.zip
Fix: plain string children bug (#138)
* fix: string children bug * test: add string children test * test: add other child test scenarios
-rw-r--r--src/frontend/render/utils.ts13
-rw-r--r--test/astro-children.test.js75
-rw-r--r--test/fixtures/astro-children/astro.config.mjs5
-rw-r--r--test/fixtures/astro-children/src/components/Component.jsx5
-rw-r--r--test/fixtures/astro-children/src/components/Component.svelte3
-rw-r--r--test/fixtures/astro-children/src/components/Component.vue9
-rw-r--r--test/fixtures/astro-children/src/pages/markup.astro21
-rw-r--r--test/fixtures/astro-children/src/pages/multiple.astro24
-rw-r--r--test/fixtures/astro-children/src/pages/strings.astro21
9 files changed, 172 insertions, 4 deletions
diff --git a/src/frontend/render/utils.ts b/src/frontend/render/utils.ts
index 2a1cc9ecf..9d55626fe 100644
--- a/src/frontend/render/utils.ts
+++ b/src/frontend/render/utils.ts
@@ -16,7 +16,10 @@ function childrenToTree(children: string[]) {
*/
export const childrenToVnodes = moize.deep(function childrenToVnodes(h: any, children: string[]) {
const tree = childrenToTree(children);
- const vnodes = tree.map((subtree) => toH(h, subtree));
+ const vnodes = tree.map((subtree) => {
+ if (subtree.type === 'text') return subtree.value;
+ return toH(h, subtree);
+ });
return vnodes;
});
@@ -37,11 +40,13 @@ export const childrenToH = moize.deep(function childrenToH(renderer: ComponentRe
return { ...vnode, __SERIALIZED };
};
const serializeChild = (child: unknown) => {
- if (typeof child === 'string') return `\`${child}\``;
- if (typeof child === 'number' || typeof child === 'boolean') return `${child}`;
+ if (['string', 'number', 'boolean'].includes(typeof child)) return JSON.stringify(child);
if (child === null) return `null`;
if ((child as any).__SERIALIZED) return (child as any).__SERIALIZED;
return innerH(child).__SERIALIZED;
};
- return tree.map((subtree) => toH(innerH, subtree).__SERIALIZED);
+ return tree.map((subtree) => {
+ if (subtree.type === 'text') return JSON.stringify(subtree.value);
+ return toH(innerH, subtree).__SERIALIZED
+ });
});
diff --git a/test/astro-children.test.js b/test/astro-children.test.js
new file mode 100644
index 000000000..368cfc9f9
--- /dev/null
+++ b/test/astro-children.test.js
@@ -0,0 +1,75 @@
+import { suite } from 'uvu';
+import * as assert from 'uvu/assert';
+import { doc } from './test-utils.js';
+import { setup, setupBuild } from './helpers.js';
+
+const ComponentChildren = suite('Component children tests');
+
+setup(ComponentChildren, './fixtures/astro-children');
+setupBuild(ComponentChildren, './fixtures/astro-children');
+
+ComponentChildren('Passes string children to framework components', async ({ runtime }) => {
+ let result = await runtime.load('/strings');
+
+ assert.equal(result.statusCode, 200);
+ const $ = doc(result.contents);
+
+ const $preact = $('#preact');
+ assert.equal($preact.text().trim(), 'Hello world', 'Can pass text to Preact components');
+
+ const $vue = $('#vue');
+ assert.equal($vue.text().trim(), 'Hello world', 'Can pass text to Vue components');
+
+ const $svelte = $('#svelte');
+ assert.equal($svelte.text().trim(), 'Hello world', 'Can pass text to Svelte components');
+});
+
+ComponentChildren('Passes markup children to framework components', async ({ runtime }) => {
+ let result = await runtime.load('/markup');
+
+ assert.equal(result.statusCode, 200);
+ const $ = doc(result.contents);
+
+ const $preact = $('#preact > h1');
+ assert.equal($preact.text().trim(), 'Hello world', 'Can pass markup to Preact components');
+
+ const $vue = $('#vue > h1');
+ assert.equal($vue.text().trim(), 'Hello world', 'Can pass markup to Vue components');
+
+ const $svelte = $('#svelte > h1');
+ assert.equal($svelte.text().trim(), 'Hello world', 'Can pass markup to Svelte components');
+});
+
+ComponentChildren('Passes multiple children to framework components', async ({ runtime }) => {
+ let result = await runtime.load('/multiple');
+
+ assert.equal(result.statusCode, 200);
+ const $ = doc(result.contents);
+
+ const $preact = $('#preact');
+ assert.equal($preact.children().length, 2, 'Can pass multiple children to Preact components');
+ assert.equal($preact.children(':first-child').text().trim(), 'Hello world');
+ assert.equal($preact.children(':last-child').text().trim(), 'Goodbye world');
+
+ const $vue = $('#vue');
+ assert.equal($vue.children().length, 2, 'Can pass multiple children to Vue components');
+ assert.equal($vue.children(':first-child').text().trim(), 'Hello world');
+ assert.equal($vue.children(':last-child').text().trim(), 'Goodbye world');
+
+ const $svelte = $('#svelte');
+ assert.equal($svelte.children().length, 2, 'Can pass multiple children to Svelte components');
+ assert.equal($svelte.children(':first-child').text().trim(), 'Hello world');
+ assert.equal($svelte.children(':last-child').text().trim(), 'Goodbye world');
+});
+
+ComponentChildren('Can be built', async ({ build }) => {
+ try {
+ await build();
+ assert.ok(true, 'Can build a project with component children');
+ } catch (err) {
+ console.log(err);
+ assert.ok(false, 'build threw');
+ }
+});
+
+ComponentChildren.run();
diff --git a/test/fixtures/astro-children/astro.config.mjs b/test/fixtures/astro-children/astro.config.mjs
new file mode 100644
index 000000000..e2a209f83
--- /dev/null
+++ b/test/fixtures/astro-children/astro.config.mjs
@@ -0,0 +1,5 @@
+export default {
+ extensions: {
+ '.jsx': 'preact'
+ },
+};
diff --git a/test/fixtures/astro-children/src/components/Component.jsx b/test/fixtures/astro-children/src/components/Component.jsx
new file mode 100644
index 000000000..bf9280f86
--- /dev/null
+++ b/test/fixtures/astro-children/src/components/Component.jsx
@@ -0,0 +1,5 @@
+import { h } from 'preact';
+
+export default function PreactComponent({ children }) {
+ return <div id="preact">{children}</div>
+}
diff --git a/test/fixtures/astro-children/src/components/Component.svelte b/test/fixtures/astro-children/src/components/Component.svelte
new file mode 100644
index 000000000..4276a78b8
--- /dev/null
+++ b/test/fixtures/astro-children/src/components/Component.svelte
@@ -0,0 +1,3 @@
+<div id="svelte">
+ <slot />
+</div>
diff --git a/test/fixtures/astro-children/src/components/Component.vue b/test/fixtures/astro-children/src/components/Component.vue
new file mode 100644
index 000000000..22e6e1143
--- /dev/null
+++ b/test/fixtures/astro-children/src/components/Component.vue
@@ -0,0 +1,9 @@
+<template>
+ <div id="vue">
+ <slot />
+ </div>
+</template>
+
+<script>
+export default {}
+</script>
diff --git a/test/fixtures/astro-children/src/pages/markup.astro b/test/fixtures/astro-children/src/pages/markup.astro
new file mode 100644
index 000000000..b771c2433
--- /dev/null
+++ b/test/fixtures/astro-children/src/pages/markup.astro
@@ -0,0 +1,21 @@
+---
+import PreactComponent from '../components/Component.jsx';
+import VueComponent from '../components/Component.vue';
+import SvelteComponent from '../components/Component.svelte';
+---
+<html>
+<head><title>Children</title></head>
+<body>
+ <PreactComponent>
+ <h1>Hello world</h1>
+ </PreactComponent>
+
+ <VueComponent>
+ <h1>Hello world</h1>
+ </VueComponent>
+
+ <SvelteComponent>
+ <h1>Hello world</h1>
+ </SvelteComponent>
+</body>
+</html>
diff --git a/test/fixtures/astro-children/src/pages/multiple.astro b/test/fixtures/astro-children/src/pages/multiple.astro
new file mode 100644
index 000000000..8c2f73a91
--- /dev/null
+++ b/test/fixtures/astro-children/src/pages/multiple.astro
@@ -0,0 +1,24 @@
+---
+import PreactComponent from '../components/Component.jsx';
+import VueComponent from '../components/Component.vue';
+import SvelteComponent from '../components/Component.svelte';
+---
+<html>
+<head><title>Children</title></head>
+<body>
+ <PreactComponent>
+ <h1>Hello world</h1>
+ <h1>Goodbye world</h1>
+ </PreactComponent>
+
+ <VueComponent>
+ <h1>Hello world</h1>
+ <h1>Goodbye world</h1>
+ </VueComponent>
+
+ <SvelteComponent>
+ <h1>Hello world</h1>
+ <h1>Goodbye world</h1>
+ </SvelteComponent>
+</body>
+</html>
diff --git a/test/fixtures/astro-children/src/pages/strings.astro b/test/fixtures/astro-children/src/pages/strings.astro
new file mode 100644
index 000000000..10b1a887f
--- /dev/null
+++ b/test/fixtures/astro-children/src/pages/strings.astro
@@ -0,0 +1,21 @@
+---
+import PreactComponent from '../components/Component.jsx';
+import VueComponent from '../components/Component.vue';
+import SvelteComponent from '../components/Component.svelte';
+---
+<html>
+<head><title>Children</title></head>
+<body>
+ <PreactComponent>
+ Hello world
+ </PreactComponent>
+
+ <VueComponent>
+ Hello world
+ </VueComponent>
+
+ <SvelteComponent>
+ Hello world
+ </SvelteComponent>
+</body>
+</html>