Skip to content

Commit c1f0aa3

Browse files
committed
feat: multiple slots
1 parent 061e9fd commit c1f0aa3

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

Diff for: ct-web-lit/src/tests.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ test.fixme('render a component as slot', async ({ mount }) => {
9494
await expect(component).toContainText('Submit');
9595
});
9696

97-
test.fixme('render a component with multiple slots', async ({ mount }) => {
97+
test('render a component with multiple slots', async ({ mount }) => {
9898
const component = await mount(DefaultSlot, {
9999
slots: {
100100
default: [

Diff for: ct-web/src/tests.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ test.fixme('render a component as slot', async ({ mount }) => {
9494
await expect(component).toContainText('Submit');
9595
});
9696

97-
test.fixme('render a component with multiple slots', async ({ mount }) => {
97+
test('render a component with multiple slots', async ({ mount }) => {
9898
const component = await mount(DefaultSlot, {
9999
slots: {
100100
default: [

Diff for: playwright-ct-web/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sand4rt/experimental-ct-web",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "Playwright Component Testing for Web Components",
55
"homepage": "https://playwright.dev",
66
"engines": {

Diff for: playwright-ct-web/registerSource.mjs

+24-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@ export function register(components) {
1515
registry.set(name, value);
1616
}
1717

18+
/**
19+
* @param {string} html
20+
* @return {DocumentFragment}
21+
*/
22+
function stringToHtml(html) {
23+
return document.createRange().createContextualFragment(html);
24+
}
25+
26+
/**
27+
* @param {string | string[]} slot
28+
*/
29+
function createSlots(slot) {
30+
if (typeof slot === 'string')
31+
return [stringToHtml(slot)];
32+
33+
if (Array.isArray(slot))
34+
return slot.map(stringToHtml);
35+
36+
throw Error(`Invalid slot received.`);
37+
}
38+
1839
/**
1940
* @param {Component} component
2041
*/
@@ -46,12 +67,12 @@ function createComponent(component) {
4667
webComponent.addEventListener(key, event => listener(/** @type {CustomEvent} */ (event).detail));
4768

4869
for (const [key, value] of Object.entries(component.options?.slots || {})) {
49-
const slot = document.createRange().createContextualFragment(value);
50-
5170
if (key !== 'default')
5271
throw new Error('named slots are not yet supported');
5372

54-
webComponent.appendChild(slot);
73+
createSlots(value).forEach(slot => {
74+
webComponent.appendChild(slot);
75+
})
5576
}
5677

5778
for (const [key, value] of Object.entries(component.options?.props || {}))

0 commit comments

Comments
 (0)