Skip to content
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

Add better support for various modifiers #120

Merged
merged 4 commits into from
Feb 24, 2018

Conversation

Joelkang
Copy link
Contributor

@Joelkang Joelkang commented Feb 11, 2018

There are 2 commits in this PR as discussed in this issue #64

  1. Updates the existing modifier syntax to remove the need to add a starting space. Also adds support for more complex modifiers
  2. Updates the modifier syntax to accept child Styled Components by passing in an array of strings and styled components, interpolating them into a final string modifier.

I'm open to discussion about the 2nd one, since that's a pretty ugly looking array. Im not sure how else to support nested components without requiring testers to know about the styledComponentId property. If we're otherwise ok with this modification, I can go ahead and update the docs as well.

An alternative I've been thinking of could be something like:

expect(wrapper.find(Text)).toHaveStyleRule('color', 'purple', {
  context: Link,
  modifier: '> & span',
})

I haven't really thought in much detail how deeper nested components can be handled in this way (it might just be available out of the box depending on the implementation)

Joel Kang added 2 commits February 10, 2018 22:31
Also removes the need for the starting space on element selector modifiers.

fixes styled-components#64
Augments the modifier syntax to allow nested component rules such as
```
const Child = styled.div`
  color: blue;
`;

const Parent = styled.div`
  display: flex;

  ${Child} {
    flex: 1;
  }
`;

fixes styled-components#64
@chrissinclair
Copy link

Heya! This would be very helpful for a project I'm working on at the moment - any word of when it'll get merged & published?

R.e. 2) - agree that the array isn't the prettiest. Given that styled-components has already taken the approach of template literals, could we not do something similar?

expect(wrapper.find(Text)).toHaveStyleRule('color', 'purple', {
  modifier: select`> ${Link} span`,
});

Even if all select does is swap that string to be the array from the current implementation? I suppose taking it a step further, you could get it to generate that whole object:

expect(wrapper.find(Text)).toHaveStyleRule('color', 'purple', select`> ${Link} span`);

Either way, being able to get that array version merged in would be awesome! Thanks :)

@MicheleBertoli
Copy link
Member

Thank you very much for this PR, @Joelkang.
I love how the code is simple, well tested, and it works as expected.
The only comment/nit I have is to avoid mutating the modifier argument.

I also agree the array syntax is not optimal in terms of DX, would you mind experimenting with the tagged template literal solution suggested by @chrissinclair (thanks!)?

We should be able to reuse css helper, like:

css`> ${Text} span`.join('')

// => > .sc-gZMcBi span

Please let me know if you need any help.

@Joelkang
Copy link
Contributor Author

@MicheleBertoli herp derp I dont know why it didnt occur to me to use the css helper which already does the interpolations. I was hoping to be able to have it inside toHaveStyleRule instead of requiring consumers/testers to use it manually, but it doesnt seem like you can use a pass a template literal to a tag (which makes sense). Seems like an okay tradeoff for more complex modifiers though.

@MicheleBertoli MicheleBertoli merged commit 3963eff into styled-components:master Feb 24, 2018
@MicheleBertoli
Copy link
Member

🙌thank you very much @Joelkang

kevcenteno pushed a commit to kevcenteno/jest-styled-components that referenced this pull request Mar 20, 2018
* feat(modifiers): add support for sass-style selectors

Also removes the need for the starting space on element selector modifiers.

fixes styled-components#64

* feat(toHaveStyleRule): add support for component modifiers

Augments the modifier syntax to allow nested component rules such as
```
const Child = styled.div`
  color: blue;
`;

const Parent = styled.div`
  display: flex;

  ${Child} {
    flex: 1;
  }
`;

fixes styled-components#64

* fix(hasStyleRule) use SC css instead for component modifiers

* docs(toHaveStyleRule): add docs for testing nested styled components
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.

3 participants