import {
component,
defineSlotComponent,
html,
metadata,
} from '../../src/fudgel.js';
defineSlotComponent();
component(
'parent-element',
{
template: html`{{name}}`,
},
class {
name = 'parent';
}
);
component(
'child-element',
{
template: html`
`,
useShadow: true,
},
class {
name = 'child';
slot: HTMLSlotElement;
onViewInit() {
const element = this[metadata]!.host;
for (const childNode of element.childNodes as unknown as ChildNode[]) {
this.slot.append(childNode.cloneNode(true));
}
}
}
);
component(
'parent-element-shadow',
{
template: html`{{name}}Correct`,
},
class {
name = 'parent';
}
);
component(
'child-element-shadow',
{
template: html`
Default - was not replaced
`,
useShadow: true,
},
class {
name = 'child';
}
);
component('content-projection', {
template: html``,
});
component(
'delayed-projection',
{
template: html`${generateContentDivs()}`,
},
class {
show = false;
onViewInit() {
this.show = true;
}
}
);
function generateContentDivs() {
return 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('').join(html`
`);
}
describe('basic initialization', () => {
beforeEach(() => {
cy.mount('');
});
it('projects content manually', () => {
cy.get('child-element')
.find('slot')
.then(element => {
expect(element[0].assignedNodes()[0].textContent).to.equal(
'parent'
);
});
});
});
describe('with shadow dom', () => {
beforeEach(() => {
cy.mount('');
});
it('projects content into slots', () => {
cy.get('#namedSlot')
.find('slot')
.then(element => {
expect(element[0].assignedElements()[0].textContent).to.equal(
'Correct'
);
});
// The interpretation of the parent's template should be done in the parent.
cy.get('#unnamedSlot')
.find('slot')
.then(element => {
expect(element[0].assignedNodes()[0].textContent).to.equal(
'parent'
);
});
});
});
describe('with content supplied', () => {
beforeEach(() => {
cy.mount(`Is this italics? YES`);
});
it('projects content', () => {
cy.get('content-projection slot-like i').should(
'contain.text',
'YES'
);
});
});