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

Strip styled components referenced in css by not used in the render #350

Open
robert-hurst-cmd opened this issue Dec 16, 2020 · 1 comment

Comments

@robert-hurst-cmd
Copy link
Contributor

robert-hurst-cmd commented Dec 16, 2020

So I originally thought this issue was related to the renaming of child components but after some digging, I noticed this was due to some logic in our button component that would leave some styled-components completely unrendered. These components however are referenced in the CSS of the parent component. Since these children are never in the final dom tree, jest-styled-components cannot get their class names, but because they are referenced in the CSS of the parent component, they show up in the final CSS.

Here's a reproduction case:

const BlueText = styled.div`
  color: blue;
`;
const RedText = styled.div`
  color: red;
`;

const FancyText = styled.div`
  font-size: 16px;

  ${BlueText} {
    text-align: right;
  }
  ${RedText} {
    text-align: center;
  }
`;

const component = (
  <FancyText>
    <BlueText>A container that has styles for an unused child</BlueText>
  </FancyText>
);

And here's the snap:

.c2 {
  color: blue;
}

.c0 {
  font-size: 16px;
}

.c0 .c1 {
  text-align: right;
}

.c0 .sc-fzXfLX {
  text-align: center;
}

<div
  className="c0"
>
  <div
    className="c1 c2"
  >
    A container that has styles for an unused child
  </div>
</div>

As you can see we have a rule referenced for sc-fzXfLX -- the id of the RedText component above. The main issue here is that if the id of RedText changes it will cause the snapshot to fail even if the CSS and HTML is functionally the same.

During this process, I've created a PR to fix this issue here.

@robert-hurst-cmd robert-hurst-cmd changed the title Styled components referenced in css by not used in the render should be stripped Strip styled components referenced in css by not used in the render Dec 16, 2020
@stonebk
Copy link

stonebk commented Jul 9, 2021

Looks like this was fixed in jest-styled-components@7.0.5 🎉

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

2 participants