| 1 |
/** |
| 2 |
* @license |
| 3 |
* Copyright 2021 Google LLC |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
*/ |
| 6 |
|
| 7 |
import '@webcomponents/scoped-custom-element-registry/scoped-custom-element-registry.min.js'; |
| 8 |
import {LitElement, html, css} from 'lit'; |
| 9 |
import {ScopedRegistryHost} from '@lit-labs/scoped-registry-mixin'; |
| 10 |
import {assert} from 'chai'; |
| 11 |
|
| 12 |
// Prevent ie11 or other incompatible browsers from running |
| 13 |
// scoped-registry-mixin tests. |
| 14 |
export const canTest = |
| 15 |
window.ShadowRoot && |
| 16 |
// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 17 |
!(window as any).ShadyDOM?.inUse && |
| 18 |
// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 19 |
(window as any).ShadowRootInit; |
| 20 |
|
| 21 |
class SimpleGreeting extends LitElement { |
| 22 |
private name: String; |
| 23 |
|
| 24 |
static override get properties() { |
| 25 |
return {name: {type: String}}; |
| 26 |
} |
| 27 |
|
| 28 |
constructor() { |
| 29 |
super(); |
| 30 |
|
| 31 |
this.name = 'World'; |
| 32 |
} |
| 33 |
|
| 34 |
override render() { |
| 35 |
return html`<span>hello ${this.name}!</span>`; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
class ScopedComponent extends ScopedRegistryHost(LitElement) { |
| 40 |
static elementDefinitions = { |
| 41 |
'simple-greeting': SimpleGreeting, |
| 42 |
}; |
| 43 |
|
| 44 |
static override get styles() { |
| 45 |
return css` |
| 46 |
:host { |
| 47 |
color: #ff0000; |
| 48 |
} |
| 49 |
`; |
| 50 |
} |
| 51 |
|
| 52 |
override render() { |
| 53 |
return html` <simple-greeting |
| 54 |
id="greeting" |
| 55 |
name="scoped world" |
| 56 |
></simple-greeting>`; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
customElements.define('scoped-component', ScopedComponent); |
| 61 |
|
| 62 |
(canTest ? suite : suite.skip)('scoped-registry-mixin', () => { |
| 63 |
test(`host element should have a registry`, async () => { |
| 64 |
const container = document.createElement('div'); |
| 65 |
container.innerHTML = `<scoped-component></scoped-component>`; |
| 66 |
|
| 67 |
document.body.appendChild(container); |
| 68 |
|
| 69 |
const scopedComponent = container.firstChild as LitElement; |
| 70 |
await scopedComponent.updateComplete; |
| 71 |
// @ts-expect-error: customElements not yet in ShadowRoot type |
| 72 |
const registry = scopedComponent?.shadowRoot?.customElements; |
| 73 |
|
| 74 |
assert.exists(registry); |
| 75 |
}); |
| 76 |
|
| 77 |
test(`hosted element should not have a registry`, async () => { |
| 78 |
const container = document.createElement('div'); |
| 79 |
container.innerHTML = `<scoped-component></scoped-component>`; |
| 80 |
|
| 81 |
document.body.appendChild(container); |
| 82 |
|
| 83 |
const scopedComponent = container.firstChild as LitElement; |
| 84 |
await scopedComponent.updateComplete; |
| 85 |
const simpleGreeting = scopedComponent?.shadowRoot?.getElementById( |
| 86 |
'greeting' |
| 87 |
) as LitElement; |
| 88 |
// @ts-expect-error: customElements not yet in ShadowRoot type |
| 89 |
const registry = simpleGreeting?.shadowRoot?.customElements; |
| 90 |
|
| 91 |
assert.notExists(registry); |
| 92 |
}); |
| 93 |
|
| 94 |
test(`hosted element should be defined inside the host element registry`, async () => { |
| 95 |
const container = document.createElement('div'); |
| 96 |
container.innerHTML = `<scoped-component></scoped-component>`; |
| 97 |
|
| 98 |
document.body.appendChild(container); |
| 99 |
|
| 100 |
const scopedComponent = container.firstChild as LitElement; |
| 101 |
await scopedComponent.updateComplete; |
| 102 |
const simpleGreeting = scopedComponent?.shadowRoot?.getElementById( |
| 103 |
'greeting' |
| 104 |
) as LitElement; |
| 105 |
|
| 106 |
assert.isTrue(simpleGreeting instanceof SimpleGreeting); |
| 107 |
}); |
| 108 |
|
| 109 |
test(`hosted element should not be defined in the global registry`, async () => { |
| 110 |
const container = document.createElement('div'); |
| 111 |
container.innerHTML = `<simple-greeting id="global-greeting"></simple-greeting>`; |
| 112 |
|
| 113 |
document.body.appendChild(container); |
| 114 |
|
| 115 |
const simpleGreeting = document.getElementById('global-greeting'); |
| 116 |
|
| 117 |
assert.isFalse(simpleGreeting instanceof SimpleGreeting); |
| 118 |
assert.isTrue(simpleGreeting instanceof HTMLElement); |
| 119 |
}); |
| 120 |
|
| 121 |
test(`host element should apply static styles`, async () => { |
| 122 |
const container = document.createElement('div'); |
| 123 |
container.innerHTML = `<scoped-component></scoped-component>`; |
| 124 |
|
| 125 |
document.body.appendChild(container); |
| 126 |
|
| 127 |
const scopedComponent = container.firstChild as LitElement; |
| 128 |
await scopedComponent.updateComplete; |
| 129 |
const simpleGreeting = |
| 130 |
scopedComponent?.shadowRoot?.getElementById('greeting'); |
| 131 |
const {color} = getComputedStyle(simpleGreeting!); |
| 132 |
|
| 133 |
assert.equal(color, 'rgb(255, 0, 0)'); |
| 134 |
}); |
| 135 |
}); |