-
-
Notifications
You must be signed in to change notification settings - Fork 143
Test style rules with media queries options #56
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
Test style rules with media queries options #56
Conversation
🙌 This is awesome, thanks @yanawaro.
Simple and effective 😻 |
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.
As I said, I really love how you implemented this functionality (and I'm happy to merge this as it is).
I added a couple of comments/questions just to get your feedback.
Thank you very much.
src/toHaveStyleRule.js
Outdated
const getRules = (ast, classNames, options) => { | ||
let rules = ast.stylesheet.rules | ||
if (options.media) { | ||
rules = rules |
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.
Two questions for you:
- What do you think about moving this into a separate function?
Something like:
const rules = options.media ? getMediaRules(ast, options.media) : ast.stylesheet.rules
- What if we make this function even more generic?
const getAtRules = (ast, options) => Object.keys(options).map(option =>
ast.stylesheet.rules.filter(rule => rule.type === option && rule[option] === options[option])
.map(rule => rule.rules)
.reduce((acc, rules) => acc.concat(rules), [])
)
It would support @supports
for free :)
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.
Yep, both looks good 👍 . I've been thinking in this way before too. If it could dynamically use the options keys for filtering rules. But at first i wasn't sure if handling others than @media
are the same. If number 2 is apply, is number 1 gonna look like this?
const rules = Object.keys(options).length ? getAtRules(ast, options.media) : ast.stylesheet.rules
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'd say:
const rules = options ? getAtRules(ast, options) : ast.stylesheet.rules
If you like it.
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.
Because I already set the default to an empty object, it would always be true. 😱
function toHaveStyleRule(received, property, value, options = {}) {
or change its default to false
I'm okay, or remove the default 😂, if you like.
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.
Oh, you are right - I missed that.
I think we can avoid setting a default value, just (received, property, value, options)
would work 👍
src/toHaveStyleRule.js
Outdated
rules = rules | ||
.filter(rule => rule.type === 'media' && rule.media === options.media) | ||
.map(rule => rule.rules) | ||
.reduce((a, b) => a.concat(b), []) |
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.
What about acc, rules
instead of a, b
?
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.
No problem. Yeah, variable names should be meaningful with the context. 😂
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.
👍
@MicheleBertoli It's been quiet, are you waiting for me to submit changes? Sorry if you are, then I misunderstood that you wrote
|
Oh yeah @yanawaro, my feedback was non-mandatory but since you agreed it'd be awesome if you could submit the changes. |
@MicheleBertoli Ok, so what do you suggest for submitting changes?
|
Anything that works for you is fine @yanawaro. |
@MicheleBertoli There,
|
41c7c27
to
396abfb
Compare
@MicheleBertoli
As to #52
Enhancements to support media queries rules is here. I tried to use less code as possible 😀. Don't know if you have better patterns on checking the options (I don't know if you like using objects to define functions to handle or else 😂). For now it looks clean, but if other more options (like finding rules for
:hover
as mentioned further in the issue is implemented.Also using options as mention earlier like
{ media: { 'max-width': '768px' } }
may not be easy. It requires more work, I read the css parser docs and it doesn't parse the media syntax 😂. So maybe right now just use simple strings to match media rules, because it could easily support multiple rules too like(min-width: 480px) and (max-width: 920px)
If its ok, then I should add some documentation 👍