|
| 1 | +import * as assert from 'node:assert/strict'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | +import * as cheerio from 'cheerio'; |
| 4 | +import { LitElement, html } from 'lit'; |
| 5 | +// Must come after lit import because @lit/reactive-element defines |
| 6 | +// globalThis.customElements which the server shim expects to be defined. |
| 7 | +import server from '../server.js'; |
| 8 | + |
| 9 | +const { check, renderToStaticMarkup } = server; |
| 10 | + |
| 11 | +describe('check', () => { |
| 12 | + it('should be false with no component', async () => { |
| 13 | + assert.equal(await check(), false); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should be false with a registered non-lit component', async () => { |
| 17 | + const tagName = 'non-lit-component'; |
| 18 | + // Lit no longer shims HTMLElement globally, so we need to do it ourselves. |
| 19 | + if (!globalThis.HTMLElement) { |
| 20 | + globalThis.HTMLElement = class {}; |
| 21 | + } |
| 22 | + customElements.define(tagName, class TestComponent extends HTMLElement {}); |
| 23 | + assert.equal(await check(tagName), false); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should be true with a registered lit component', async () => { |
| 27 | + const tagName = 'lit-component'; |
| 28 | + customElements.define(tagName, class extends LitElement {}); |
| 29 | + assert.equal(await check(tagName), true); |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +describe('renderToStaticMarkup', () => { |
| 34 | + it('should throw error if trying to render an unregistered component', async () => { |
| 35 | + const tagName = 'non-registrered-component'; |
| 36 | + try { |
| 37 | + await renderToStaticMarkup(tagName); |
| 38 | + } catch (e) { |
| 39 | + assert.equal(e instanceof TypeError, true); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + it('should render empty component with default markup', async () => { |
| 44 | + const tagName = 'nothing-component'; |
| 45 | + customElements.define(tagName, class extends LitElement {}); |
| 46 | + const render = await renderToStaticMarkup(tagName); |
| 47 | + assert.deepEqual(render, { |
| 48 | + html: `<${tagName}><template shadowroot="open" shadowrootmode="open"><!--lit-part--><!--/lit-part--></template></${tagName}>`, |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should render component with default markup', async () => { |
| 53 | + const tagName = 'simple-component'; |
| 54 | + customElements.define( |
| 55 | + tagName, |
| 56 | + class extends LitElement { |
| 57 | + render() { |
| 58 | + return html`<p>hola</p>`; |
| 59 | + } |
| 60 | + } |
| 61 | + ); |
| 62 | + const render = await renderToStaticMarkup(tagName); |
| 63 | + const $ = cheerio.load(render.html); |
| 64 | + assert.equal($(`${tagName} template`).html().includes('<p>hola</p>'), true); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should render component with properties and attributes', async () => { |
| 68 | + const tagName = 'props-and-attrs-component'; |
| 69 | + const attr1 = 'test'; |
| 70 | + const prop1 = 'Daniel'; |
| 71 | + customElements.define( |
| 72 | + tagName, |
| 73 | + class extends LitElement { |
| 74 | + static properties = { |
| 75 | + prop1: { type: String }, |
| 76 | + }; |
| 77 | + |
| 78 | + constructor() { |
| 79 | + super(); |
| 80 | + this.prop1 = 'someone'; |
| 81 | + } |
| 82 | + |
| 83 | + render() { |
| 84 | + return html`<p>Hello ${this.prop1}</p>`; |
| 85 | + } |
| 86 | + } |
| 87 | + ); |
| 88 | + const render = await renderToStaticMarkup(tagName, { prop1, attr1 }); |
| 89 | + const $ = cheerio.load(render.html); |
| 90 | + assert.equal($(tagName).attr('attr1'), attr1); |
| 91 | + assert.equal($(`${tagName} template`).text().includes(`Hello ${prop1}`), true); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should render nested components', async () => { |
| 95 | + const tagName = 'parent-component'; |
| 96 | + const childTagName = 'child-component'; |
| 97 | + customElements.define( |
| 98 | + childTagName, |
| 99 | + class extends LitElement { |
| 100 | + render() { |
| 101 | + return html`<p>child</p>`; |
| 102 | + } |
| 103 | + } |
| 104 | + ); |
| 105 | + customElements.define( |
| 106 | + tagName, |
| 107 | + class extends LitElement { |
| 108 | + render() { |
| 109 | + return html`<child-component></child-component>`; |
| 110 | + } |
| 111 | + } |
| 112 | + ); |
| 113 | + const render = await renderToStaticMarkup(tagName); |
| 114 | + const $ = cheerio.load(render.html); |
| 115 | + assert.equal($(`${tagName} template`).text().includes('child'), true); |
| 116 | + // Child component should have `defer-hydration` attribute so it'll only |
| 117 | + // hydrate after the parent hydrates |
| 118 | + assert.equal($(childTagName).attr('defer-hydration'), ''); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should render DSD attributes based on shadowRootOptions', async () => { |
| 122 | + const tagName = 'shadow-root-options-component'; |
| 123 | + customElements.define( |
| 124 | + tagName, |
| 125 | + class extends LitElement { |
| 126 | + static shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true }; |
| 127 | + } |
| 128 | + ); |
| 129 | + const render = await renderToStaticMarkup(tagName); |
| 130 | + assert.deepEqual(render, { |
| 131 | + html: `<${tagName}><template shadowroot=\"open\" shadowrootmode=\"open\" shadowrootdelegatesfocus><!--lit-part--><!--/lit-part--></template></${tagName}>`, |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments