2022-05-16 11:16:30 -05:00
|
|
|
import { expect } from 'chai';
|
2022-05-16 11:17:25 -05:00
|
|
|
import server from '../server.js';
|
|
|
|
import { LitElement, html } from 'lit';
|
|
|
|
import * as cheerio from 'cheerio';
|
2022-05-16 11:16:30 -05:00
|
|
|
|
2022-05-16 11:17:25 -05:00
|
|
|
const { check, renderToStaticMarkup } = server;
|
2022-05-16 11:16:30 -05:00
|
|
|
|
|
|
|
describe('check', () => {
|
|
|
|
it('should be false with no component', async () => {
|
2022-05-16 11:17:25 -05:00
|
|
|
expect(await check()).to.equal(false);
|
|
|
|
});
|
2022-05-16 11:16:30 -05:00
|
|
|
|
|
|
|
it('should be false with a registered non-lit component', async () => {
|
2022-05-16 11:17:25 -05:00
|
|
|
const tagName = 'non-lit-component';
|
|
|
|
customElements.define(tagName, class TestComponent extends HTMLElement {});
|
|
|
|
expect(await check(tagName)).to.equal(false);
|
|
|
|
});
|
2022-05-16 11:16:30 -05:00
|
|
|
|
|
|
|
it('should be true with a registered lit component', async () => {
|
2022-05-16 11:17:25 -05:00
|
|
|
const tagName = 'lit-component';
|
|
|
|
customElements.define(tagName, class extends LitElement {});
|
|
|
|
expect(await check(tagName)).to.equal(true);
|
|
|
|
});
|
|
|
|
});
|
2022-05-16 11:16:30 -05:00
|
|
|
|
|
|
|
describe('renderToStaticMarkup', () => {
|
|
|
|
it('should throw error if trying to render an unregistered component', async () => {
|
2022-05-16 11:17:25 -05:00
|
|
|
const tagName = 'non-registrered-component';
|
2022-05-16 11:16:30 -05:00
|
|
|
try {
|
2022-05-16 11:17:25 -05:00
|
|
|
await renderToStaticMarkup(tagName);
|
2022-05-16 11:16:30 -05:00
|
|
|
} catch (e) {
|
2022-05-16 11:17:25 -05:00
|
|
|
expect(e).to.be.an.instanceOf(TypeError);
|
2022-05-16 11:16:30 -05:00
|
|
|
}
|
2022-05-16 11:17:25 -05:00
|
|
|
});
|
2022-05-16 11:16:30 -05:00
|
|
|
|
2022-12-19 06:00:00 -05:00
|
|
|
it('should render empty component with default markup', async () => {
|
2022-05-16 11:17:25 -05:00
|
|
|
const tagName = 'nothing-component';
|
|
|
|
customElements.define(tagName, class extends LitElement {});
|
|
|
|
const render = await renderToStaticMarkup(tagName);
|
2022-05-16 11:16:30 -05:00
|
|
|
expect(render).to.deep.equal({
|
2022-05-16 11:17:25 -05:00
|
|
|
html: `<${tagName}><template shadowroot="open"><!--lit-part--><!--/lit-part--></template></${tagName}>`,
|
|
|
|
});
|
|
|
|
});
|
2022-05-16 11:16:30 -05:00
|
|
|
|
|
|
|
it('should render component with default markup', async () => {
|
2022-05-16 11:17:25 -05:00
|
|
|
const tagName = 'simple-component';
|
|
|
|
customElements.define(
|
|
|
|
tagName,
|
|
|
|
class extends LitElement {
|
|
|
|
render() {
|
|
|
|
return html`<p>hola</p>`;
|
|
|
|
}
|
2022-05-16 11:16:30 -05:00
|
|
|
}
|
2022-05-16 11:17:25 -05:00
|
|
|
);
|
|
|
|
const render = await renderToStaticMarkup(tagName);
|
|
|
|
const $ = cheerio.load(render.html);
|
|
|
|
expect($(`${tagName} template`).html()).to.contain('<p>hola</p>');
|
|
|
|
});
|
2022-05-16 11:16:30 -05:00
|
|
|
|
|
|
|
it('should render component with properties and attributes', async () => {
|
2022-05-16 11:17:25 -05: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 11:16:30 -05:00
|
|
|
|
2022-05-16 11:17:25 -05:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.prop1 = 'someone';
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return html`<p>Hello ${this.prop1}</p>`;
|
|
|
|
}
|
2022-05-16 11:16:30 -05:00
|
|
|
}
|
2022-05-16 11:17:25 -05: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}`);
|
|
|
|
});
|
|
|
|
});
|