diff --git a/tests/designSystem/dynamicStyles.test.mjs b/tests/designSystem/dynamicStyles.test.mjs new file mode 100644 index 00000000..7aa060b0 --- /dev/null +++ b/tests/designSystem/dynamicStyles.test.mjs @@ -0,0 +1,276 @@ +import { describe, it, beforeEach, afterEach } from 'node:test'; +import assert from 'node:assert/strict'; +import { JSDOM } from 'jsdom'; +import { + registerScope, + setVariables, + releaseScope, + getScopeAttributeName +} from '../../js/designSystem/dynamicStyles.js'; + +describe('js/designSystem/dynamicStyles.js', () => { + let dom; + let document; + let window; + let originalCSSStyleSheet; + + beforeEach(() => { + dom = new JSDOM('', { + url: 'http://localhost/', + pretendToBeVisual: true, + }); + window = dom.window; + document = window.document; + + originalCSSStyleSheet = globalThis.CSSStyleSheet; + // Polyfill global CSSStyleSheet for constructable stylesheets support if needed + // JSDOM supports it, but we need to expose it globally for the module to detect it + if (!globalThis.CSSStyleSheet) { + globalThis.CSSStyleSheet = window.CSSStyleSheet; + } + }); + + afterEach(() => { + // Clean up + dom = null; + document = null; + window = null; + globalThis.CSSStyleSheet = originalCSSStyleSheet; + }); + + describe('registerScope', () => { + it('should register a scope and return a string ID', () => { + const scopeId = registerScope('test-scope', [], { documentRef: document }); + assert.equal(typeof scopeId, 'string'); + assert.ok(scopeId.startsWith('test-scope')); + }); + + it('should create a style element or use adoptedStyleSheets', () => { + const scopeId = registerScope('test-scope', [], { documentRef: document }); + + // Check for style element fallback or adoptedStyleSheets + const styleElement = document.querySelector('style[data-ds-dynamic="true"]'); + const adopted = document.adoptedStyleSheets && document.adoptedStyleSheets.length > 0; + + assert.ok(styleElement || adopted, 'Should use either