Skip to content

Commit ec10ba5

Browse files
committed
Add: toHaveStyle first approach
1 parent 25b87a7 commit ec10ba5

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

packages/dom/src/lib/ElementAssertion.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Assertion, AssertionError } from "@assertive-ts/core";
2+
import {parse} from '@adobe/css-tools'
23

34
export class ElementAssertion<T extends Element> extends Assertion<T> {
45

@@ -146,6 +147,77 @@ export class ElementAssertion<T extends Element> extends Assertion<T> {
146147
return this.actual.className.split(/\s+/).filter(Boolean);
147148
}
148149

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+
149221
/**
150222
* Helper method to assert the presence or absence of class names.
151223
*

0 commit comments

Comments
 (0)