2022-05-16 16:16:30 +00:00
|
|
|
import { expect } from 'chai';
|
2022-05-16 16:17:25 +00:00
|
|
|
import { LitElement, html } from 'lit';
|
2023-03-29 16:43:40 +00:00
|
|
|
// Must come after lit import because @lit/reactive-element defines
|
|
|
|
// globalThis.customElements which the server shim expects to be defined.
|
|
|
|
import server from '../server.js';
|
2022-05-16 16:17:25 +00:00
|
|
|
import * as cheerio from 'cheerio';
|
2022-05-16 16:16:30 +00:00
|
|
|
|
2022-05-16 16:17:25 +00:00
|
|
|
const { check, renderToStaticMarkup } = server;
|
2022-05-16 16:16:30 +00:00
|
|
|
|
|
|
|
describe('check', () => {
|
|
|
|
it('should be false with no component', async () => {
|
2022-05-16 16:17:25 +00:00
|
|
|
expect(await check()).to.equal(false);
|
|
|
|
});
|
2022-05-16 16:16:30 +00:00
|
|
|
|
|
|
|
it('should be false with a registered non-lit component', async () => {
|
2022-05-16 16:17:25 +00:00
|
|
|
const tagName = 'non-lit-component';
|
2023-03-29 16:43:40 +00:00
|
|
|
// Lit no longer shims HTMLElement globally, so we need to do it ourselves.
|
|
|
|
if (!globalThis.HTMLElement) {
|
|
|
|
globalThis.HTMLElement = class {};
|
|
|
|
}
|
2022-05-16 16:17:25 +00:00
|
|
|
customElements.define(tagName, class TestComponent extends HTMLElement {});
|
|
|
|
expect(await check(tagName)).to.equal(false);
|
|
|
|
});
|
2022-05-16 16:16:30 +00:00
|
|
|
|
|
|
|
it('should be true with a registered lit component', async () => {
|
2022-05-16 16:17:25 +00:00
|
|
|
const tagName = 'lit-component';
|
|
|
|
customElements.define(tagName, class extends LitElement {});
|
|
|
|
expect(await check(tagName)).to.equal(true);
|
|
|
|
});
|
|
|
|
});
|
2022-05-16 16:16:30 +00:00
|
|
|
|
|
|
|
describe('renderToStaticMarkup', () => {
|
|
|
|
it('should throw error if trying to render an unregistered component', async () => {
|
2022-05-16 16:17:25 +00:00
|
|
|
const tagName = 'non-registrered-component';
|
2022-05-16 16:16:30 +00:00
|
|
|
try {
|
2022-05-16 16:17:25 +00:00
|
|
|
await renderToStaticMarkup(tagName);
|
2022-05-16 16:16:30 +00:00
|
|
|
} catch (e) {
|
2022-05-16 16:17:25 +00:00
|
|
|
expect(e).to.be.an.instanceOf(TypeError);
|
2022-05-16 16:16:30 +00:00
|
|
|
}
|
2022-05-16 16:17:25 +00:00
|
|
|
});
|
2022-05-16 16:16:30 +00:00
|
|
|
|
2022-12-19 11:00:00 +00:00
|
|
|
it('should render empty component with default markup', async () => {
|
2022-05-16 16:17:25 +00:00
|
|
|
const tagName = 'nothing-component';
|
|
|
|
customElements.define(tagName, class extends LitElement {});
|
|
|
|
const render = await renderToStaticMarkup(tagName);
|
2022-05-16 16:16:30 +00:00
|
|
|
expect(render).to.deep.equal({
|
2023-02-01 13:44:56 +00:00
|
|
|
html: `<${tagName}><template shadowroot="open" shadowrootmode="open"><!--lit-part--><!--/lit-part--></template></${tagName}>`,
|
2022-05-16 16:17:25 +00:00
|
|
|
});
|
|
|
|
});
|
2022-05-16 16:16:30 +00:00
|
|
|
|
|
|
|
it('should render component with default markup', async () => {
|
2022-05-16 16:17:25 +00:00
|
|
|
const tagName = 'simple-component';
|
|
|
|
customElements.define(
|
|
|
|
tagName,
|
|
|
|
class extends LitElement {
|
|
|
|
render() {
|
|
|
|
return html`<p>hola</p>`;
|
|
|
|
}
|
2022-05-16 16:16:30 +00:00
|
|
|
}
|
2022-05-16 16:17:25 +00:00
|
|
|
);
|
|
|
|
const render = await renderToStaticMarkup(tagName);
|
|
|
|
const $ = cheerio.load(render.html);
|
|
|
|
expect($(`${tagName} template`).html()).to.contain('<p>hola</p>');
|
|
|
|
});
|
2022-05-16 16:16:30 +00:00
|
|
|
|
|
|
|
it('should render component with properties and attributes', async () => {
|
2022-05-16 16:17:25 +00:00
|
|
|
const tagName = 'props-and-attrs-component';
|
|
|
|
const attr1 = 'test';
|
|
|
|
const prop1 = 'Daniel';
|
|
|
|
customElements.define(
|
|
|
|
tagName,
|
|
|
|
class extends LitElement {
|
|
|
|
static properties = {
|
|
|
|
prop1: { type: String },
|
|
|
|
};
|
2022-05-16 16:16:30 +00:00
|
|
|
|
2022-05-16 16:17:25 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.prop1 = 'someone';
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return html`<p>Hello ${this.prop1}</p>`;
|
|
|
|
}
|
2022-05-16 16:16:30 +00:00
|
|
|
}
|
2022-05-16 16:17:25 +00:00
|
|
|
);
|
|
|
|
const render = await renderToStaticMarkup(tagName, { prop1, attr1 });
|
|
|
|
const $ = cheerio.load(render.html);
|
|
|
|
expect($(tagName).attr('attr1')).to.equal(attr1);
|
|
|
|
expect($(`${tagName} template`).text()).to.contain(`Hello ${prop1}`);
|
|
|
|
});
|
2023-02-24 18:10:56 +00:00
|
|
|
|
|
|
|
it('should render DSD attributes based on shadowRootOptions', async () => {
|
2023-03-29 16:43:40 +00:00
|
|
|
const tagName = 'shadow-root-options-component';
|
2023-02-24 18:13:07 +00:00
|
|
|
customElements.define(
|
|
|
|
tagName,
|
|
|
|
class extends LitElement {
|
|
|
|
static shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true };
|
|
|
|
}
|
|
|
|
);
|
2023-02-24 18:10:56 +00:00
|
|
|
const render = await renderToStaticMarkup(tagName);
|
|
|
|
expect(render).to.deep.equal({
|
|
|
|
html: `<${tagName}><template shadowroot=\"open\" shadowrootmode=\"open\" shadowrootdelegatesfocus><!--lit-part--><!--/lit-part--></template></${tagName}>`,
|
|
|
|
});
|
|
|
|
});
|
2022-05-16 16:17:25 +00:00
|
|
|
});
|