|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + |
| 4 | +<head> |
| 5 | + <meta charset="utf-8"> |
| 6 | + <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes"> |
| 7 | + <script src="/components/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script> |
| 8 | + <script src="/components/web-component-tester/browser.js"></script> |
| 9 | +</head> |
| 10 | + |
| 11 | +<body> |
| 12 | + |
| 13 | + <pfelement> |
| 14 | + This is the element content. |
| 15 | + </pfelement> |
| 16 | + |
| 17 | + <script type="module"> |
| 18 | + import PFElement from "../dist/pfelement.js"; |
| 19 | + |
| 20 | + const colors = ["red", "yellow", "blue"]; |
| 21 | + |
| 22 | + class TestElement extends PFElement { |
| 23 | + static get tag() { |
| 24 | + return "pfe-test-element" |
| 25 | + } |
| 26 | + |
| 27 | + static get version() { |
| 28 | + return "1.0"; |
| 29 | + } |
| 30 | + |
| 31 | + get html() { |
| 32 | + return ` |
| 33 | + <style>:host{--context:dark;}</style> |
| 34 | + <div>Test Element</div> |
| 35 | + `; |
| 36 | + } |
| 37 | + |
| 38 | + constructor() { |
| 39 | + super(TestElement, { type: PFElement.PfeTypes.Content }); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + PFElement.create(TestElement); |
| 44 | + |
| 45 | + class TestChildElement extends PFElement { |
| 46 | + static get tag() { |
| 47 | + return "pfe-test-child-element" |
| 48 | + } |
| 49 | + |
| 50 | + get html() { |
| 51 | + return ` |
| 52 | + <div>Test Child Element</div> |
| 53 | + `; |
| 54 | + } |
| 55 | + |
| 56 | + constructor() { |
| 57 | + super(TestChildElement, { type: PFElement.PfeTypes.Content }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + PFElement.create(TestChildElement); |
| 62 | + |
| 63 | + suite('<pfelement>', () => { |
| 64 | + teardown(() => { |
| 65 | + const testElement = document.getElementById("test-element"); |
| 66 | + |
| 67 | + if (!testElement) return; |
| 68 | + |
| 69 | + document.body.removeChild(testElement); |
| 70 | + }); |
| 71 | + |
| 72 | + test("it should default to the provided --context variable in the component's stylesheet", () => { |
| 73 | + const testElement = document.createElement("pfe-test-element"); |
| 74 | + |
| 75 | + testElement.id = "test-element"; |
| 76 | + document.body.appendChild(testElement); |
| 77 | + |
| 78 | + assert.equal(testElement.getAttribute("on"), "dark"); |
| 79 | + }); |
| 80 | + |
| 81 | + test("it should update the on attribute if style with --context is manually added after upgrade", () => { |
| 82 | + const testElement = document.createElement("pfe-test-element"); |
| 83 | + |
| 84 | + testElement.id = "test-element"; |
| 85 | + document.body.appendChild(testElement); |
| 86 | + |
| 87 | + testElement.setAttribute("style", "color: pink; --context: light;"); |
| 88 | + |
| 89 | + assert.equal(testElement.getAttribute("on"), "light"); |
| 90 | + }); |
| 91 | + |
| 92 | + test("it should favor context attribute over the --context variable", () => { |
| 93 | + const testElement = document.createElement("pfe-test-element"); |
| 94 | + |
| 95 | + testElement.id = "test-element"; |
| 96 | + document.body.appendChild(testElement); |
| 97 | + |
| 98 | + testElement.setAttribute("context", "saturated"); |
| 99 | + testElement.setAttribute("style", "color: pink; --context: light;"); |
| 100 | + |
| 101 | + assert.equal(testElement.getAttribute("on"), "saturated"); |
| 102 | + }); |
| 103 | + |
| 104 | + test("it should update the on attribute if context is manually added after upgrade", () => { |
| 105 | + const testElement = document.createElement("pfe-test-element"); |
| 106 | + |
| 107 | + testElement.id = "test-element"; |
| 108 | + document.body.appendChild(testElement); |
| 109 | + |
| 110 | + document.querySelector("#test-element").setAttribute("context", "light"); |
| 111 | + |
| 112 | + assert.equal(testElement.getAttribute("on"), "light"); |
| 113 | + }); |
| 114 | + |
| 115 | + test("it should push down the context to children", done => { |
| 116 | + const testElement = document.createElement("pfe-test-element"); |
| 117 | + const testChildElement = document.createElement("pfe-test-child-element"); |
| 118 | + |
| 119 | + testElement.id = "test-element"; |
| 120 | + testChildElement.id = "test-child-element"; |
| 121 | + |
| 122 | + testElement.appendChild(testChildElement); |
| 123 | + document.body.appendChild(testElement); |
| 124 | + |
| 125 | + flush(() => { |
| 126 | + assert.equal(testChildElement.getAttribute("on"), "dark"); |
| 127 | + done(); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + test("it should push an attribute override context down to the children", done => { |
| 132 | + const testElement = document.createElement("pfe-test-element"); |
| 133 | + const testChildElement = document.createElement("pfe-test-child-element"); |
| 134 | + |
| 135 | + testElement.id = "test-element"; |
| 136 | + testChildElement.id = "test-child-element"; |
| 137 | + |
| 138 | + testElement.appendChild(testChildElement); |
| 139 | + document.body.appendChild(testElement); |
| 140 | + |
| 141 | + // Add the context information |
| 142 | + testElement.setAttribute("context", "light"); |
| 143 | + |
| 144 | + flush(() => { |
| 145 | + assert.equal(testChildElement.getAttribute("on"), "light"); |
| 146 | + done(); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + test("it should push a variable override context down to the children", done => { |
| 151 | + const testElement = document.createElement("pfe-test-element"); |
| 152 | + const testChildElement = document.createElement("pfe-test-child-element"); |
| 153 | + |
| 154 | + testElement.id = "test-element"; |
| 155 | + testChildElement.id = "test-child-element"; |
| 156 | + |
| 157 | + testElement.appendChild(testChildElement); |
| 158 | + document.body.appendChild(testElement); |
| 159 | + |
| 160 | + // Add the context information |
| 161 | + testElement.setAttribute("style", "--context: light;"); |
| 162 | + |
| 163 | + flush(() => { |
| 164 | + assert.equal(testChildElement.getAttribute("on"), "light"); |
| 165 | + done(); |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + test("it should favor child's context if set via variable --context", () => { |
| 170 | + const testElement = document.createElement("pfe-test-element"); |
| 171 | + const testChildElement = document.createElement("pfe-test-child-element"); |
| 172 | + |
| 173 | + testElement.id = "test-element"; |
| 174 | + testChildElement.id = "test-child-element"; |
| 175 | + |
| 176 | + testElement.appendChild(testChildElement); |
| 177 | + document.body.appendChild(testElement); |
| 178 | + |
| 179 | + // Add the context information |
| 180 | + testChildElement.setAttribute("style", "--context: light;"); |
| 181 | + |
| 182 | + assert.equal(testChildElement.getAttribute("on"), "light"); |
| 183 | + }); |
| 184 | + |
| 185 | + test("it should favor child's context if the child has context set", () => { |
| 186 | + const testElement = document.createElement("pfe-test-element"); |
| 187 | + const testChildElement = document.createElement("pfe-test-child-element"); |
| 188 | + |
| 189 | + testElement.id = "test-element"; |
| 190 | + testChildElement.id = "test-child-element"; |
| 191 | + |
| 192 | + testElement.appendChild(testChildElement); |
| 193 | + document.body.appendChild(testElement); |
| 194 | + |
| 195 | + // Add the context information |
| 196 | + testChildElement.setAttribute("context", "light"); |
| 197 | + |
| 198 | + assert.equal(testChildElement.getAttribute("on"), "light"); |
| 199 | + }); |
| 200 | + |
| 201 | + // @TODO Deprecated for 1.0 |
| 202 | + test("it should update the on attribute if style with --theme is manually added after upgrade", () => { |
| 203 | + const testElement = document.createElement("pfe-test-element"); |
| 204 | + |
| 205 | + testElement.id = "test-element"; |
| 206 | + document.body.appendChild(testElement); |
| 207 | + |
| 208 | + assert.equal(testElement.getAttribute("on"), "dark"); |
| 209 | + testElement.setAttribute("style", "color: pink; --theme: light;"); |
| 210 | + assert.equal(testElement.getAttribute("on"), "light"); |
| 211 | + }); |
| 212 | + |
| 213 | + // @TODO Deprecated for 1.0 |
| 214 | + test("it should update the on and context attributes if pfe-theme is manually added after upgrade", () => { |
| 215 | + const testElement = document.createElement("pfe-test-element"); |
| 216 | + |
| 217 | + testElement.id = "test-element-theme"; |
| 218 | + document.body.appendChild(testElement); |
| 219 | + |
| 220 | + document.querySelector("#test-element-theme").setAttribute("pfe-theme", "light"); |
| 221 | + |
| 222 | + assert.equal(testElement.getAttribute("on"), "light"); |
| 223 | + assert.equal(testElement.getAttribute("context"), "light"); |
| 224 | + }); |
| 225 | + |
| 226 | + }); |
| 227 | + </script> |
| 228 | +</body> |
| 229 | + |
| 230 | +</html> |
0 commit comments