-
Notifications
You must be signed in to change notification settings - Fork 412
feature: toHaveStyle custom matcher #12
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
Conversation
src/to-have-style.js
Outdated
import {matcherHint} from 'jest-matcher-utils' | ||
import {checkHtmlElement, getMessage} from './utils' | ||
|
||
function parseCSS(css) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might consider using the css
module for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I will. It's on the to-do list above. This was just a first draft, a proof-of-concept.
Codecov Report
@@ Coverage Diff @@
## master #12 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 6 7 +1
Lines 41 67 +26
Branches 12 15 +3
=====================================
+ Hits 41 67 +26
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to be soooo cool!
In the future maybe we could consider toHaveOnlyStyles
, or maybe we could call this method toHaveStylesSubset
or something.
) | ||
} | ||
|
||
function printoutStyles(styles) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could use css.stringify
to ensure that this handles things like media queries and stuff properly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about it, but there's a problem with this. The css
library seems to only be able to handle the syntax of css to the stylesheet level, that is, property: value;
pairs listed inside a selector { ... }
. And here I'm handling only plain css property/value pairs (well, sets of property/value pairs really) abstracted away from a selector. I do not even think media queries apply.
For instance, I'd have to convert what getComputedStyles
give me, which is, again, a set of plain css property/value pairs, and manually build the AST that the css
lib would have created out of those rules with a fake css selector wrapping them, just to make css.stringify
be able to process it.
I saw the point of using css
to parse because even though I had to fake wrapping the plain css props in a selector, it gives me syntax error handling, which is amazing for the pretty error messages when expectations in tests fail. But after all this reasoning do you still think is worth using css.stringify
@kentcdodds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Do we even need to handle media queries here? Maybe I'm missing out some potential great use case for this, but I don't see it yet)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, I don't think we need to use css.stringify
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is amazing! Thanks for working on this!
It would be very cool in the future to support objects in addition to a string of CSS 👍
I'll let you go ahead and merge this considering this is your project and all. 👍 I'm just a bystander 😉 |
I considered working on that in this PR, but I'll leave it for now, so we can have something usable sooner.
I think I never thanked you for this huge opportunity to work on and maintain an OSS project, almost all by myself, so here it goes: Thank you! (though I still don't think of it as mine, never would have worked on this if it weren't for your and @antoaravinth who led the way, thanks again!) |
Ooops, I think I messed up @kentcdodds. I think the commit was supposed to start with What am I supposed to do now? Something like what you did here? You see why I still need you around? 😄 |
Haha, no worries. Yep, that's exactly what the manual_releases.md file is for 👍 |
There was an issue with a minor release, so this manual-releases.md change is to release a new minor version. Reference: #12
🎉 This PR is included in version 1.2.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Is this feature implemented? If it isn't I would love to take a look at @gnapse job and try to figure out what can be done in order to solve the problem. By the way, huge thanks to you @kentcdodds for inspiring other people to contribute and learn doing OSS :) |
Not sure what's the logic there. The fact that it asserts that the element has some styles does not imply that those have to be all the styles it's got. It's only that those are the style rules you want to assert. The same happens with Unless I'm misunderstanding? |
Hi @gnapse 👋, Thanks for the response, if that's the case, yes, I think that the statement is a bit redundant. Actually I realized that the thing I was trying to test wasn't working as I expected, long story short: I was having issues with the Have a nice day. |
What:
A new
.toHaveStyle
custom matcher, as proposed in #6.Closes #6
Checklist:
parseCSS
with a more robust alternative (like this)