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

support shallow rendering when nesting styled components (#306) #309

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/toHaveStyleRule.js
Expand Up @@ -5,13 +5,21 @@ const shouldDive = node =>

const isTagWithClassName = node =>
node.exists() && node.prop("className") && typeof node.type() === "string";

const hasClassName = node =>
node.length > 0
&& typeof node.props === "function"
&& node.props("className")
&& node.props("className").className;

const getClassNames = received => {
let className;

if (received) {
if (received.$$typeof === Symbol.for("react.test.json")) {
className = received.props.className || received.props.class;
} else if (hasClassName(received)) {
className = received.props("className").className;
} else if (typeof received.exists === "function" && received.exists()) {
const tree = shouldDive(received) ? received.dive() : received;
const components = tree.findWhere(isTagWithClassName);
Expand Down
13 changes: 13 additions & 0 deletions test/toHaveStyleRule.spec.js
Expand Up @@ -479,6 +479,19 @@ it("nested", () => {
toHaveStyleRule(<MyComponent />, "background", "papayawhip");
});

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

const MyComponent = (props) => <Wrapper {...props} />;
const MyStyledComponent = styled(MyComponent)`
color: red;
`;

toHaveStyleRule(<MyStyledComponent/>, "color", "red");
});

it("empty children", () => {
const Wrapper = styled.section`
padding: 4em;
Expand Down