|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +// import 'jasmine'; (google3-only) |
| 8 | + |
| 9 | +import {Environment} from '@material/web/testing/environment.js'; |
| 10 | +import {html, render} from 'lit'; |
| 11 | + |
| 12 | +import {hasSlotted} from './has-slotted.js'; |
| 13 | + |
| 14 | +describe('hasSlotted()', () => { |
| 15 | + const env = new Environment(); |
| 16 | + let host: HTMLElement; |
| 17 | + let shadowRoot: ShadowRoot; |
| 18 | + let slot: HTMLSlotElement; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + host = document.createElement('div'); |
| 22 | + shadowRoot = host.attachShadow({mode: 'open'}); |
| 23 | + render(html`<slot ${hasSlotted()} name="test"></slot>`, shadowRoot); |
| 24 | + slot = shadowRoot.querySelector('slot')!; |
| 25 | + env.render(html`${host}`); |
| 26 | + }); |
| 27 | + |
| 28 | + it('throws an error if used on a non-<slot> element', () => { |
| 29 | + expect(() => { |
| 30 | + env.render(html`<div ${hasSlotted()}></div>`); |
| 31 | + }).toThrowError('hasSlotted() must be used on a <slot> element.'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('does not add .has-slotted class when slot has no assigned nodes', async () => { |
| 35 | + expect(slot.classList) |
| 36 | + .withContext('slot classList') |
| 37 | + .not.toContain('has-slotted'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('adds .has-slotted class when slot has assigned nodes', async () => { |
| 41 | + render(html`<span slot="test">Content</span>`, host); |
| 42 | + await env.waitForStability(); |
| 43 | + |
| 44 | + expect(slot.classList) |
| 45 | + .withContext('slot classList') |
| 46 | + .toContain('has-slotted'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('toggles .has-slotted class on slot content change', async () => { |
| 50 | + render(html`<span slot="test">Content</span>`, host); |
| 51 | + await env.waitForStability(); |
| 52 | + |
| 53 | + expect(slot.classList) |
| 54 | + .withContext('slot classList') |
| 55 | + .toContain('has-slotted'); |
| 56 | + |
| 57 | + host.firstElementChild?.remove(); |
| 58 | + await env.waitForStability(); |
| 59 | + |
| 60 | + expect(slot.classList) |
| 61 | + .withContext('slot classList') |
| 62 | + .not.toContain('has-slotted'); |
| 63 | + }); |
| 64 | +}); |
0 commit comments