|
| 1 | +import CssCustomProperties from "./CssCustomProperties"; |
| 2 | + |
| 3 | +describe("CssCustomProperties in jsdom", () => { |
| 4 | + beforeEach(() => { |
| 5 | + const style = document.createElement("style"); |
| 6 | + style.textContent = ` |
| 7 | + :root { --eccgui-color-palette-blue-500: #1c6ecb; } |
| 8 | + .config { --note-yellow: #ffde8f; } |
| 9 | + `; |
| 10 | + document.head.appendChild(style); |
| 11 | + }); |
| 12 | + |
| 13 | + it("reads property names of a style rule without iterating the declaration", () => { |
| 14 | + expect( |
| 15 | + CssCustomProperties.listLocalCssStyleRuleProperties({ |
| 16 | + selectorText: ":root", |
| 17 | + propertyType: "custom", |
| 18 | + }), |
| 19 | + ).toEqual([["--eccgui-color-palette-blue-500", "#1c6ecb"]]); |
| 20 | + }); |
| 21 | + |
| 22 | + it("does not throw for scoped selectors", () => { |
| 23 | + expect(() => |
| 24 | + new CssCustomProperties({ selectorText: ".config", returnObject: false }).customProperties(), |
| 25 | + ).not.toThrow(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe("useComputedStyleFallback", () => { |
| 29 | + beforeEach(() => { |
| 30 | + // the property is not part of any stylesheet, so the CSSOM does not know its name |
| 31 | + const element = document.createElement("div"); |
| 32 | + element.classList.add("without-stylesheet"); |
| 33 | + element.style.setProperty("--only-computed", "#c0ffee"); |
| 34 | + document.body.appendChild(element); |
| 35 | + }); |
| 36 | + |
| 37 | + it("is disabled by default", () => { |
| 38 | + expect( |
| 39 | + new CssCustomProperties({ |
| 40 | + selectorText: ".without-stylesheet", |
| 41 | + }).customProperties(), |
| 42 | + ).toEqual({}); |
| 43 | + }); |
| 44 | + |
| 45 | + it("reads the names from the computed style if the CSSOM does not provide any", () => { |
| 46 | + expect( |
| 47 | + new CssCustomProperties({ |
| 48 | + selectorText: ".without-stylesheet", |
| 49 | + useComputedStyleFallback: true, |
| 50 | + }).customProperties(), |
| 51 | + ).toEqual({ "only-computed": "#c0ffee" }); |
| 52 | + }); |
| 53 | + |
| 54 | + it("is not used if the CSSOM provides names", () => { |
| 55 | + expect( |
| 56 | + new CssCustomProperties({ |
| 57 | + selectorText: ".config", |
| 58 | + useComputedStyleFallback: true, |
| 59 | + }).customProperties(), |
| 60 | + ).toEqual({ "note-yellow": "#ffde8f" }); |
| 61 | + }); |
| 62 | + }); |
| 63 | +}); |
0 commit comments