Skip to content

Commit dd0da1b

Browse files
support shallow rendering when nesting styled components (#306) (#309)
1 parent 38c7ccf commit dd0da1b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/toHaveStyleRule.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ const shouldDive = node =>
55

66
const isTagWithClassName = node =>
77
node.exists() && node.prop("className") && typeof node.type() === "string";
8+
9+
const hasClassName = node =>
10+
node.length > 0
11+
&& typeof node.props === "function"
12+
&& node.props("className")
13+
&& node.props("className").className;
814

915
const getClassNames = received => {
1016
let className;
1117

1218
if (received) {
1319
if (received.$$typeof === Symbol.for("react.test.json")) {
1420
className = received.props.className || received.props.class;
21+
} else if (hasClassName(received)) {
22+
className = received.props("className").className;
1523
} else if (typeof received.exists === "function" && received.exists()) {
1624
const tree = shouldDive(received) ? received.dive() : received;
1725
const components = tree.findWhere(isTagWithClassName);

test/toHaveStyleRule.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,19 @@ it("nested", () => {
479479
toHaveStyleRule(<MyComponent />, "background", "papayawhip");
480480
});
481481

482+
it("nested with styling", () => {
483+
const Wrapper = styled.section`
484+
background: papayawhip;
485+
`;
486+
487+
const MyComponent = (props) => <Wrapper {...props} />;
488+
const MyStyledComponent = styled(MyComponent)`
489+
color: red;
490+
`;
491+
492+
toHaveStyleRule(<MyStyledComponent/>, "color", "red");
493+
});
494+
482495
it("empty children", () => {
483496
const Wrapper = styled.section`
484497
padding: 4em;

0 commit comments

Comments
 (0)