|
1 | 1 | import { Assertion, AssertionError } from "@assertive-ts/core"; |
| 2 | +import {parse} from '@adobe/css-tools' |
2 | 3 |
|
3 | 4 | export class ElementAssertion<T extends Element> extends Assertion<T> { |
4 | 5 |
|
@@ -146,6 +147,77 @@ export class ElementAssertion<T extends Element> extends Assertion<T> { |
146 | 147 | return this.actual.className.split(/\s+/).filter(Boolean); |
147 | 148 | } |
148 | 149 |
|
| 150 | + public toHaveStyle(css: Object|string): this { |
| 151 | + const styleTest = document.createElement("div"); |
| 152 | + styleTest.style.color = "red"; |
| 153 | + styleTest.style.display = "flex"; |
| 154 | + if ( |
| 155 | + this.actual instanceof HTMLElement || |
| 156 | + this.actual['ownerDocument'] |
| 157 | + ) { |
| 158 | + |
| 159 | + |
| 160 | + const parsedCSS = typeof css === 'object' |
| 161 | + ? css |
| 162 | + : parse(`selector { ${css} }`, {silent: true}).stylesheet |
| 163 | + |
| 164 | + const window = this.actual.ownerDocument.defaultView; |
| 165 | + |
| 166 | + const computedStyle = window?.getComputedStyle; |
| 167 | + |
| 168 | + const expected = parsedCSS |
| 169 | + const received = computedStyle?.(this.actual); |
| 170 | + console.log(received?.color); |
| 171 | + const expectedRule = expected.rules[0]; |
| 172 | + |
| 173 | + interface StyleDeclaration { |
| 174 | + property: string; |
| 175 | + value: string; |
| 176 | + } |
| 177 | + |
| 178 | + let style = {} |
| 179 | + let props: string[] = [] |
| 180 | + |
| 181 | + expectedRule.declarations.map((declaration: StyleDeclaration) => { |
| 182 | + const property = declaration.property; |
| 183 | + const value = declaration.value; |
| 184 | + |
| 185 | + props = [...props, property]; |
| 186 | + |
| 187 | + style = { |
| 188 | + ...style, |
| 189 | + [property]: value, |
| 190 | + }; |
| 191 | + |
| 192 | + return style |
| 193 | + |
| 194 | + }) |
| 195 | + |
| 196 | + console.log(style); |
| 197 | + console.log(props); |
| 198 | + |
| 199 | + props.map((prop: string) => { |
| 200 | + |
| 201 | + console.log(received?.[prop]); |
| 202 | + }) |
| 203 | + |
| 204 | + |
| 205 | + return this.execute({ |
| 206 | + assertWhen: true, |
| 207 | + error: new AssertionError({ |
| 208 | + actual: this.actual, |
| 209 | + message: "Expected the element to have the specified style", |
| 210 | + }), |
| 211 | + invertedError: new AssertionError({ |
| 212 | + actual: this.actual, |
| 213 | + message: "Expected the element to NOT have the specified style", |
| 214 | + }), |
| 215 | + }); |
| 216 | + } |
| 217 | + return this; |
| 218 | + } |
| 219 | + |
| 220 | + |
149 | 221 | /** |
150 | 222 | * Helper method to assert the presence or absence of class names. |
151 | 223 | * |
|
0 commit comments