test(@astrojs/lit): adding tests (#3375)
* test(@astrojs/lit): adding tests * changeset
This commit is contained in:
parent
b6f7a4fb4b
commit
fe61e469b2
5 changed files with 100 additions and 9 deletions
5
.changeset/red-bikes-happen.md
Normal file
5
.changeset/red-bikes-happen.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/lit': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Added tests and fix a small edge case for when you call render with no props/attrs
|
|
@ -28,14 +28,16 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
|
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
|
||||||
"build:ci": "astro-scripts build \"src/**/*.ts\"",
|
"build:ci": "astro-scripts build \"src/**/*.ts\"",
|
||||||
"dev": "astro-scripts dev \"src/**/*.ts\""
|
"dev": "astro-scripts dev \"src/**/*.ts\"",
|
||||||
|
"test": "mocha"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@lit-labs/ssr": "^2.1.0"
|
"@lit-labs/ssr": "^2.1.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"astro": "workspace:*",
|
"astro": "workspace:*",
|
||||||
"astro-scripts": "workspace:*"
|
"astro-scripts": "workspace:*",
|
||||||
|
"cheerio": "^1.0.0-rc.10"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@webcomponents/template-shadowroot": "^0.1.0",
|
"@webcomponents/template-shadowroot": "^0.1.0",
|
||||||
|
|
|
@ -29,12 +29,14 @@ function* render(tagName, attrs, children) {
|
||||||
|
|
||||||
// LitElementRenderer creates a new element instance, so copy over.
|
// LitElementRenderer creates a new element instance, so copy over.
|
||||||
const Ctr = getCustomElementConstructor(tagName);
|
const Ctr = getCustomElementConstructor(tagName);
|
||||||
for (let [name, value] of Object.entries(attrs)) {
|
if (attrs) {
|
||||||
// check if this is a reactive property
|
for (let [name, value] of Object.entries(attrs)) {
|
||||||
if (name in Ctr.prototype) {
|
// check if this is a reactive property
|
||||||
instance.setProperty(name, value);
|
if (name in Ctr.prototype) {
|
||||||
} else {
|
instance.setProperty(name, value);
|
||||||
instance.setAttribute(name, value);
|
} else {
|
||||||
|
instance.setAttribute(name, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
80
packages/integrations/lit/test/server.test.js
Normal file
80
packages/integrations/lit/test/server.test.js
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
import { expect } from 'chai';
|
||||||
|
import server from '../server.js'
|
||||||
|
import { LitElement, html } from 'lit'
|
||||||
|
import * as cheerio from 'cheerio'
|
||||||
|
|
||||||
|
const { check, renderToStaticMarkup } = server
|
||||||
|
|
||||||
|
describe('check', () => {
|
||||||
|
it('should be false with no component', async () => {
|
||||||
|
expect(await check()).to.equal(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should be false with a registered non-lit component', async () => {
|
||||||
|
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 () => {
|
||||||
|
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 () => {
|
||||||
|
const tagName = 'non-registrered-component'
|
||||||
|
try {
|
||||||
|
await renderToStaticMarkup(tagName)
|
||||||
|
} catch (e) {
|
||||||
|
expect(e).to.be.an.instanceOf(TypeError)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should render emtpy component with default markup', async () => {
|
||||||
|
const tagName = 'nothing-component'
|
||||||
|
customElements.define(tagName, class extends LitElement {})
|
||||||
|
const render = await renderToStaticMarkup(tagName)
|
||||||
|
expect(render).to.deep.equal({
|
||||||
|
html: `<${tagName}><template shadowroot="open"><!--lit-part--><!--/lit-part--></template></${tagName}>`
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should render component with default markup', async () => {
|
||||||
|
const tagName = 'simple-component'
|
||||||
|
customElements.define(tagName, class extends LitElement {
|
||||||
|
render() {
|
||||||
|
return html`<p>hola</p>`
|
||||||
|
}
|
||||||
|
})
|
||||||
|
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 () => {
|
||||||
|
const tagName = 'props-and-attrs-component'
|
||||||
|
const attr1 = 'test'
|
||||||
|
const prop1 = 'Daniel'
|
||||||
|
customElements.define(tagName, class extends LitElement {
|
||||||
|
static properties = {
|
||||||
|
prop1: { type: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.prop1 = 'someone';
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return html`<p>Hello ${this.prop1}</p>`
|
||||||
|
}
|
||||||
|
})
|
||||||
|
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}`)
|
||||||
|
})
|
||||||
|
})
|
|
@ -1349,11 +1349,13 @@ importers:
|
||||||
'@lit-labs/ssr': ^2.1.0
|
'@lit-labs/ssr': ^2.1.0
|
||||||
astro: workspace:*
|
astro: workspace:*
|
||||||
astro-scripts: workspace:*
|
astro-scripts: workspace:*
|
||||||
|
cheerio: ^1.0.0-rc.10
|
||||||
dependencies:
|
dependencies:
|
||||||
'@lit-labs/ssr': 2.1.0
|
'@lit-labs/ssr': 2.1.0
|
||||||
devDependencies:
|
devDependencies:
|
||||||
astro: link:../../astro
|
astro: link:../../astro
|
||||||
astro-scripts: link:../../../scripts
|
astro-scripts: link:../../../scripts
|
||||||
|
cheerio: 1.0.0-rc.10
|
||||||
|
|
||||||
packages/integrations/netlify:
|
packages/integrations/netlify:
|
||||||
specifiers:
|
specifiers:
|
||||||
|
@ -5485,7 +5487,7 @@ packages:
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
/boolbase/1.0.0:
|
/boolbase/1.0.0:
|
||||||
resolution: {integrity: sha1-aN/1++YMUes3cl6p4+0xDcwed24=}
|
resolution: {integrity: sha512-JZOSA7Mo9sNGB8+UjSgzdLtokWAky1zbztM3WRLCbZ70/3cTANmQmOdR7y2g+J0e2WXywy1yS468tY+IruqEww==}
|
||||||
|
|
||||||
/boxen/6.2.1:
|
/boxen/6.2.1:
|
||||||
resolution: {integrity: sha512-H4PEsJXfFI/Pt8sjDWbHlQPx4zL/bvSQjcilJmaulGt5mLDorHOHpmdXAJcBcmru7PhYSp/cDMWRko4ZUMFkSw==}
|
resolution: {integrity: sha512-H4PEsJXfFI/Pt8sjDWbHlQPx4zL/bvSQjcilJmaulGt5mLDorHOHpmdXAJcBcmru7PhYSp/cDMWRko4ZUMFkSw==}
|
||||||
|
|
Loading…
Reference in a new issue