2022-04-10 20:29:46 -05:00
|
|
|
import { expect } from 'chai'
|
2022-03-07 16:36:22 -05:00
|
|
|
import { polyfill } from '../mod.js'
|
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
describe('Custom Elements', () => {
|
|
|
|
const target = {}
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
beforeEach(() => polyfill(target))
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
it('Includes Custom Element functionality', () => {
|
|
|
|
expect(target).to.have.property('CustomElementRegistry')
|
|
|
|
expect(target).to.have.property('customElements')
|
|
|
|
expect(target).to.have.property('HTMLElement')
|
|
|
|
})
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
it('Supports Custom Element creation', () => {
|
|
|
|
const CustomElement = class HTMLCustomElement extends target.HTMLElement {}
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
target.customElements.define('custom-element', CustomElement)
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
expect(target.customElements.get('custom-element')).to.equal(CustomElement)
|
|
|
|
expect(target.customElements.getName(CustomElement)).to.equal(
|
|
|
|
'custom-element'
|
|
|
|
)
|
|
|
|
})
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
it('Supports Custom Elements created from Document', () => {
|
|
|
|
expect(target.document.body.localName).to.equal('body')
|
|
|
|
expect(target.document.body.tagName).to.equal('BODY')
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
expect(
|
|
|
|
target.document.createElement('custom-element').constructor.name
|
|
|
|
).to.equal('HTMLUnknownElement')
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
const CustomElement = class HTMLCustomElement extends target.HTMLElement {}
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
target.customElements.define('custom-element', CustomElement)
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
expect(
|
|
|
|
target.document.createElement('custom-element').constructor.name
|
|
|
|
).to.equal('HTMLCustomElement')
|
|
|
|
})
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
it('Supports Custom Elements with properties', () => {
|
|
|
|
const testSymbol = Symbol.for('webapi.test')
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
const CustomElement = class HTMLCustomElement extends target.HTMLElement {
|
|
|
|
otherMethod = () => testSymbol
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
method() {
|
|
|
|
return this.otherMethod()
|
|
|
|
}
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
static method() {
|
|
|
|
return this.otherMethod()
|
|
|
|
}
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
static otherMethod() {
|
|
|
|
return testSymbol
|
|
|
|
}
|
|
|
|
}
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
target.customElements.define('custom-element', CustomElement)
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
expect(CustomElement.method()).to.equal(testSymbol)
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
const customElement = new CustomElement()
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
expect(customElement.method()).to.equal(testSymbol)
|
|
|
|
})
|
2022-03-07 16:36:22 -05:00
|
|
|
})
|