Skip to content

feat(dom): DOM - toHaveStyle #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft

feat(dom): DOM - toHaveStyle #154

wants to merge 7 commits into from

Conversation

SbsCruz
Copy link

@SbsCruz SbsCruz commented Aug 1, 2025

Added the toHaveStyle matcher.


let expectedStyle = {}
let receivedStyle = {}
let props: string[] = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to refactor these above so that we handle mutability.
Use const instead of let

Comment on lines +181 to +191
Object.entries(css).map(([property, value]) => {
props = [...props, property];

normalizer.style[property] = value;
const normalizedValue = window?.getComputedStyle(normalizer).getPropertyValue(property);

expectedStyle = {
...expectedStyle,
[property]: normalizedValue?.trim(),

};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get this logic in a separate function in our helpers file. This help avoid dupllicated code below.

Comment on lines +203 to +213
normalizer.style[property] = value;
const normalizedValue = window.getComputedStyle(normalizer).getPropertyValue(property);

expectedStyle = {
...expectedStyle,
[property]: normalizedValue.trim(),
};

return expectedStyle;
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove duplicated code by moving it to a function.

Comment on lines +229 to +240
const isSameStyle = !!Object.keys(expectedStyle).length &&
Object.entries(expectedStyle).every(([expectedProp, expectedValue]) => {
const isCustomProperty = expectedProp.startsWith('--')
const spellingVariants = [expectedProp]
expectedProp !== null;

if (!isCustomProperty) spellingVariants.push(expectedProp.toLowerCase())
return spellingVariants.some( searchProp =>
receivedStyle[searchProp] === expectedValue
)
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's extract this in a new function to keep our code readable and simple.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For tests, keep in mind we want to test the the error assertion and its message. Also the inverted error and its message. depending on the test add both assertions

console.log("isSameStyle: ", isSameStyle)
const error = new AssertionError({
actual: this.actual,
message: `Expected the element to have ${JSON.stringify(expectedStyle)} style`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a better messaging here, we can add the element in the string as well.
For example:

Expected

element to have ${JSON.stringify(expectedStyle)} style

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants