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

v7.0.0 No styles found when shallow rendering nested styled-components #306

Open
functionalDev opened this issue Feb 19, 2020 · 6 comments

Comments

@functionalDev
Copy link
Contributor

functionalDev commented Feb 19, 2020

When upgrading to styled-components v5 and jest-styled-components v7, we had multiple components missing styling rules in tests when shallow rendering and checking with toHaveStyleRule.

I came to the conclusion that it happens, when a styled-component has another styled-component as the root element in the mark up.
Example Test:

import { shallow } from "enzyme";

it("nested with styling", () => {
  const WrapperComponent = styled.section`
    background: papayawhip;
  `;

  const MyComponent = ({className}) => <WrapperComponent className={className} />;
  const MyStyledComponent = styled(MyComponent)`
    color: red;
  `;

  const wrapper = shallow(
    <MyStyledComponent >{sampleText}</MyStyledComponent >,
  );
  expect(wrapper).toHaveStyleRule('color', 'red');
});

In my opinion this is a very common thing when using styled-components and should be supported by this library.

Something that is probably obvious, but just to be sure I point it out: There is no test for that case in toHaveStyleRule.spec.js and it fails when I add it.

@dumbNickname
Copy link

dumbNickname commented Feb 26, 2020

We have exactly the same problem - any news here? We are aware of the cheap solution -> switch tests to use mount instead of shallow to make them green, but we would definitely like to avoid taking this path as it increases time needed by tests significantly.

@kennethnym
Copy link

kennethnym commented Mar 8, 2020

The native version of toHaveStyleRule only tries to find style prop in the top-most level of a given styled-components like this:

component.props.styles

However, Enzyme will only render the first level and not any further, so style prop will be undefined.

If we dive three times into the given styled-components, the underlying components will be exposed, thus exposing the style prop:

component.dive().dive().dive().debug()
// <View style={{...}}
component.dive().dive().dive().prop('style')
// works

functionalDev pushed a commit to functionalDev/jest-styled-components that referenced this issue Mar 9, 2020
functionalDev added a commit to functionalDev/jest-styled-components that referenced this issue Mar 9, 2020
@functionalDev
Copy link
Contributor Author

@MrCreeper1008
thx for the explaination, but I don't quite get it. If I understand properly, then I am testing the top-most level of the given styled component and not some deeper level.

I created a pull request which fixes the issue for me. The cause seems to be that the className is not retrieved properly for the case describes above. I also added a test for the use case to toHaveStyleRule.spec.js.

@kennethnym
Copy link

It works differently for react native. I forgot to put that

@quantizor
Copy link
Contributor

v7.0.1 might help

@Tokimon
Copy link

Tokimon commented Jun 24, 2020

I have posted a possible reason here:
#297 (comment)

And I currently use v7.0.2

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

No branches or pull requests

5 participants