astro/packages/integrations/lit/test/server.test.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.5 KiB
JavaScript
Raw Normal View History

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