Skip to content

Commit c04f0df

Browse files
committed
feat: Added tests for reactify() outside a SvelteKit app
E2E tests for #49
1 parent c190640 commit c04f0df

File tree

10 files changed

+91
-0
lines changed

10 files changed

+91
-0
lines changed

playwright/tests/react-html.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { test, expect } from "@playwright/test";
2+
3+
test.use({ viewport: { width: 480, height: 360 } });
4+
test.describe("react.html", () => {
5+
test("should render the reactified Svelte component inside a React app", async ({
6+
page,
7+
}) => {
8+
await page.goto("/react.html");
9+
await expect(page.getByText("Scooby")).toBeVisible();
10+
await expect(page.locator("body")).toHaveScreenshot();
11+
});
12+
});

playwright/tests/ssr.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ test.describe("ssr", () => {
1818
"/fixtures/RestProps",
1919
"/fixtures/List",
2020
"/intrinsic-elements",
21+
"/fixtures/Dog",
2122
];
2223

2324
test("client-rendered", async ({ page }) => {
Loading
Loading

src/lib/reactify.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export default function reactify<P = any, E = any>(
8080
reactChildrenRef.current.parentElement !== svelteChildrenRef.current
8181
) {
8282
svelteChildrenRef.current.appendChild(reactChildrenRef.current);
83+
} else if (key === undefined) {
84+
reactChildrenRef.current.style.display = "contents";
8385
}
8486
} else if (svelteChildrenRef.current) {
8587
svelteChildrenRef.current.innerHTML = "";

src/tests/fixtures/App.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as React from "react";
2+
import { createRoot } from "react-dom/client";
3+
import { reactify } from "svelte-preprocess-react";
4+
import DogSvelte from "./Dog.svelte";
5+
import ChildrenSvelte from "./Children.svelte";
6+
7+
const el = document.querySelector("react-app");
8+
if (!el) {
9+
throw new Error("<react-app> not found");
10+
}
11+
const Dog = reactify(DogSvelte);
12+
const Children = reactify(ChildrenSvelte);
13+
14+
function App() {
15+
return (
16+
<div>
17+
<h1>React app</h1>
18+
<Dog name="Scooby doo" />
19+
<Children>
20+
<div style={{ color: "#0b6a84" }}>
21+
React element inside a reactified Svelte component
22+
</div>
23+
</Children>
24+
</div>
25+
);
26+
}
27+
createRoot(el).render(<App />);

src/tests/fixtures/Children.svelte

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script lang="ts">
2+
import type { Snippet } from "svelte";
3+
4+
type Props = {
5+
children: Snippet;
6+
};
7+
let { children }: Props = $props();
8+
</script>
9+
10+
<div class="wrapper">
11+
{@render children()}
12+
</div>
13+
14+
<style>
15+
.wrapper {
16+
margin: 1rem;
17+
padding: 2rem;
18+
border: 2px solid #108caf;
19+
display: flex;
20+
flex-direction: column;
21+
gap: 1rem;
22+
}
23+
</style>

src/tests/fixtures/Dog.svelte

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,14 @@
1111
</script>
1212

1313
<svelte-dog>{name}</svelte-dog>
14+
15+
<style>
16+
svelte-dog {
17+
display: inline-block;
18+
background: #a65c37;
19+
color: white;
20+
border-radius: 5rem 0 2rem 0;
21+
padding: 2rem;
22+
border: 4px solid #5dacdb;
23+
}
24+
</style>

static/react.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>React</title>
7+
<script type="module" src="@vite/client"></script>
8+
</head>
9+
<body>
10+
<react-app>
11+
<h1>Loading...</h1>
12+
</react-app>
13+
<script type="module" src="src/tests/fixtures/App.tsx"></script>
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)