Skip to content

Commit 6efcdef

Browse files
committed
Refactor: toHaveStyle class
1 parent c66b17f commit 6efcdef

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

packages/dom/src/lib/ElementAssertion.ts

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,40 +167,65 @@ export class ElementAssertion<T extends Element> extends Assertion<T> {
167167

168168
const expected = parsedCSS
169169
const received = computedStyle?.(this.actual);
170-
console.log(received?.color);
171170
const expectedRule = expected.rules[0];
172171

173172
interface StyleDeclaration {
174173
property: string;
175174
value: string;
176175
}
177176

178-
let style = {}
177+
let expectedStyle = {}
178+
let receivedStyle = {}
179179
let props: string[] = []
180180

181+
const normalizer = document.createElement("div");
182+
document.body.appendChild(normalizer);
183+
184+
181185
expectedRule.declarations.map((declaration: StyleDeclaration) => {
182186
const property = declaration.property;
183187
const value = declaration.value;
184-
188+
185189
props = [...props, property];
186-
187-
style = {
188-
...style,
189-
[property]: value,
190+
191+
normalizer.style[property] = value;
192+
const normalizedValue = window.getComputedStyle(normalizer).getPropertyValue(property);
193+
194+
expectedStyle = {
195+
...expectedStyle,
196+
[property]: normalizedValue.trim(),
190197
};
191-
192-
return style
193198

194-
})
199+
return expectedStyle;
200+
});
201+
202+
document.body.removeChild(normalizer);
195203

196-
console.log(style);
197-
console.log(props);
204+
205+
console.log("expected style: ",expectedStyle);
198206

199207
props.map((prop: string) => {
200-
201-
console.log(received?.[prop]);
208+
receivedStyle = {
209+
...receivedStyle,
210+
[prop]: received?.getPropertyValue(prop).trim(),
211+
};
202212
})
203213

214+
console.log("received style: ", receivedStyle);
215+
216+
const isSameStyle = !!Object.keys(expectedStyle).length &&
217+
Object.entries(expectedStyle).every(([expectedProp, expectedValue]) => {
218+
const isCustomProperty = expectedProp.startsWith('--')
219+
const spellingVariants = [expectedProp]
220+
expectedProp !== null;
221+
222+
if (!isCustomProperty) spellingVariants.push(expectedProp.toLowerCase())
223+
return spellingVariants.some( searchProp =>
224+
receivedStyle[searchProp] === expectedValue
225+
)
226+
})
227+
228+
console.log("isSameStyle: ", isSameStyle)
204229

205230
return this.execute({
206231
assertWhen: true,

0 commit comments

Comments
 (0)