| 1 |
/** |
| 2 |
* @license |
| 3 |
* Copyright 2024 Google LLC |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
*/ |
| 6 |
|
| 7 |
import {suite} from 'uvu'; |
| 8 |
// eslint-disable-next-line import/extensions |
| 9 |
import * as assert from 'uvu/assert'; |
| 10 |
import { |
| 11 |
customElements, |
| 12 |
document, |
| 13 |
EventTargetShimMeta, |
| 14 |
HTMLElement, |
| 15 |
} from '@lit-labs/ssr-dom-shim'; |
| 16 |
|
| 17 |
const test = suite('Element Shim'); |
| 18 |
|
| 19 |
test('toggleAttribute adds and removes attributes', () => { |
| 20 |
const shimmedEl = new HTMLElement(); |
| 21 |
assert.ok(shimmedEl.toggleAttribute('potato')); |
| 22 |
assert.ok(shimmedEl.hasAttribute('potato')); |
| 23 |
assert.not.ok(shimmedEl.toggleAttribute('potato')); |
| 24 |
assert.not.ok(shimmedEl.hasAttribute('potato')); |
| 25 |
}); |
| 26 |
|
| 27 |
test('toggleAttribute accepts an optional force parameter', () => { |
| 28 |
const shimmedEl = new HTMLElement(); |
| 29 |
assert.not.ok(shimmedEl.toggleAttribute('potato', false)); |
| 30 |
assert.not.ok(shimmedEl.hasAttribute('potato')); |
| 31 |
assert.ok(shimmedEl.toggleAttribute('potato', true)); |
| 32 |
assert.ok(shimmedEl.hasAttribute('potato')); |
| 33 |
// A no-op, ensuring attribute remains |
| 34 |
assert.ok(shimmedEl.toggleAttribute('potato', true)); |
| 35 |
assert.ok(shimmedEl.hasAttribute('potato')); |
| 36 |
}); |
| 37 |
|
| 38 |
test('toggleAttribute retains a previously set value if force is true', () => { |
| 39 |
const shimmedEl = new HTMLElement(); |
| 40 |
shimmedEl.setAttribute('foo', 'bar'); |
| 41 |
assert.ok(shimmedEl.toggleAttribute('foo', true)); |
| 42 |
assert.equal(shimmedEl.getAttribute('foo'), 'bar'); |
| 43 |
}); |
| 44 |
|
| 45 |
test('setAttribute silently casts values to a string', () => { |
| 46 |
const shimmedEl = new HTMLElement(); |
| 47 |
// It is possible to pass any value to `setAttribute`, and we |
| 48 |
// silently convert it to a string. |
| 49 |
shimmedEl.setAttribute('tomato', {} as unknown as string); |
| 50 |
assert.equal(shimmedEl.getAttribute('tomato'), '[object Object]'); |
| 51 |
}); |
| 52 |
|
| 53 |
test('localName and tagName should be available with constructor from customElements registry', () => { |
| 54 |
const elementName = 'lit-test-registry'; |
| 55 |
customElements.define(elementName, class extends HTMLElement {}); |
| 56 |
const LitTestRegistry = customElements.get(elementName)!; |
| 57 |
|
| 58 |
const shimmedEl = new LitTestRegistry(); |
| 59 |
assert.equal(shimmedEl.localName, elementName); |
| 60 |
assert.equal(shimmedEl.tagName, elementName.toUpperCase()); |
| 61 |
}); |
| 62 |
|
| 63 |
test('localName and tagName should be available when registering with customElements', () => { |
| 64 |
const elementName = 'lit-test-local'; |
| 65 |
class LitTestLocal extends HTMLElement {} |
| 66 |
customElements.define(elementName, LitTestLocal); |
| 67 |
|
| 68 |
const shimmedEl = new LitTestLocal(); |
| 69 |
assert.equal(shimmedEl.localName, elementName); |
| 70 |
assert.equal(shimmedEl.tagName, elementName.toUpperCase()); |
| 71 |
}); |
| 72 |
|
| 73 |
test('localName and tagName usage should return undefined if not registered', () => { |
| 74 |
class LitTestUnregistered extends HTMLElement {} |
| 75 |
|
| 76 |
const shimmedEl = new LitTestUnregistered(); |
| 77 |
assert.equal(shimmedEl.localName, undefined); |
| 78 |
assert.equal(shimmedEl.tagName, undefined); |
| 79 |
}); |
| 80 |
|
| 81 |
test('getRootNode should return document when not inside a shadow DOM', () => { |
| 82 |
assert.equal(new HTMLElement().getRootNode(), document); |
| 83 |
}); |
| 84 |
|
| 85 |
test('getRootNode should return the shadow root of the host', () => { |
| 86 |
const host = new HTMLElement(); |
| 87 |
host.attachShadow({mode: 'open'}); |
| 88 |
const child = new HTMLElement(); |
| 89 |
(child as Partial<EventTargetShimMeta>).__host = host; |
| 90 |
assert.equal(child.getRootNode(), host.shadowRoot); |
| 91 |
assert.equal(child.getRootNode({composed: false}), host.shadowRoot); |
| 92 |
}); |
| 93 |
|
| 94 |
test('getRootNode should return document with composed option', () => { |
| 95 |
const host = new HTMLElement(); |
| 96 |
host.attachShadow({mode: 'open'}); |
| 97 |
const child = new HTMLElement(); |
| 98 |
(child as Partial<EventTargetShimMeta>).__host = host; |
| 99 |
assert.equal(child.getRootNode({composed: true}), document); |
| 100 |
}); |
| 101 |
|
| 102 |
test('getRootNode should return the shadow root of the host with closed mode', () => { |
| 103 |
const host = new HTMLElement(); |
| 104 |
const shadowRoot = host.attachShadow({mode: 'closed'}); |
| 105 |
assert.equal(host.shadowRoot, null); |
| 106 |
const child = new HTMLElement(); |
| 107 |
(child as Partial<EventTargetShimMeta>).__host = host; |
| 108 |
assert.equal(child.getRootNode(), shadowRoot); |
| 109 |
}); |
| 110 |
|
| 111 |
test.run(); |