import { afterEach, describe, expect, it, vi } from 'vitest'; import { applyShadowStyles, createShadowStyle, ensureGlobalStyle } from '../shadow-styles'; describe('createShadowStyle', () => { afterEach(() => { vi.unstubAllGlobals(); }); it('returns a CSSStyleSheet when constructable stylesheets are available', () => { const result = createShadowStyle('div { color: red; }'); expect(result).toBeInstanceOf(CSSStyleSheet); }); it('returns raw CSS string when CSSStyleSheet is unavailable', () => { vi.stubGlobal('CSSStyleSheet', undefined); const css = 'div { color: red; }'; const result = createShadowStyle(css); expect(result).toBe(css); }); }); describe('applyShadowStyles', () => { function createHost(): HTMLElement { const host = document.createElement('div'); host.attachShadow({ mode: 'open' }); document.body.appendChild(host); return host; } afterEach(() => { vi.restoreAllMocks(); document.body.innerHTML = ''; }); it('uses adoptedStyleSheets when supported and all styles are constructable', () => { const host = createHost(); const shadowRoot = host.shadowRoot!; const sheet = new CSSStyleSheet(); sheet.replaceSync('div { color: red; }'); let assigned: CSSStyleSheet[] = []; Object.defineProperty(shadowRoot, 'adoptedStyleSheets', { get: () => assigned, set: (value: CSSStyleSheet[]) => { assigned = value; }, configurable: true, }); applyShadowStyles(shadowRoot, [sheet]); expect(assigned).toContain(sheet); expect(shadowRoot.querySelectorAll('style').length).toBe(0); }); it('falls back to