Skip to content

Commit

Permalink
feat: multiple slots
Browse files Browse the repository at this point in the history
  • Loading branch information
sand4rt committed Jan 1, 2023
1 parent 061e9fd commit c1f0aa3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ct-web-lit/src/tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ test.fixme('render a component as slot', async ({ mount }) => {
await expect(component).toContainText('Submit');
});

test.fixme('render a component with multiple slots', async ({ mount }) => {
test('render a component with multiple slots', async ({ mount }) => {
const component = await mount(DefaultSlot, {
slots: {
default: [
Expand Down
2 changes: 1 addition & 1 deletion ct-web/src/tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ test.fixme('render a component as slot', async ({ mount }) => {
await expect(component).toContainText('Submit');
});

test.fixme('render a component with multiple slots', async ({ mount }) => {
test('render a component with multiple slots', async ({ mount }) => {
const component = await mount(DefaultSlot, {
slots: {
default: [
Expand Down
2 changes: 1 addition & 1 deletion playwright-ct-web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sand4rt/experimental-ct-web",
"version": "0.0.6",
"version": "0.0.7",
"description": "Playwright Component Testing for Web Components",
"homepage": "https://playwright.dev",
"engines": {
Expand Down
27 changes: 24 additions & 3 deletions playwright-ct-web/registerSource.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ export function register(components) {
registry.set(name, value);
}

/**
* @param {string} html
* @return {DocumentFragment}
*/
function stringToHtml(html) {
return document.createRange().createContextualFragment(html);
}

/**
* @param {string | string[]} slot
*/
function createSlots(slot) {
if (typeof slot === 'string')
return [stringToHtml(slot)];

if (Array.isArray(slot))
return slot.map(stringToHtml);

throw Error(`Invalid slot received.`);
}

/**
* @param {Component} component
*/
Expand Down Expand Up @@ -46,12 +67,12 @@ function createComponent(component) {
webComponent.addEventListener(key, event => listener(/** @type {CustomEvent} */ (event).detail));

for (const [key, value] of Object.entries(component.options?.slots || {})) {
const slot = document.createRange().createContextualFragment(value);

if (key !== 'default')
throw new Error('named slots are not yet supported');

webComponent.appendChild(slot);
createSlots(value).forEach(slot => {
webComponent.appendChild(slot);
})
}

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

0 comments on commit c1f0aa3

Please sign in to comment.