Skip to content

Commit f8e6f79

Browse files
author
Camilo QS
authored
Fix get styled className from children components (#313)
* Fix get styled className from children components Fix issue introduced in #309, the .find is broken when try to get/check styles in children components. * add additional test case
1 parent 8c50db8 commit f8e6f79

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/toHaveStyleRule.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ const shouldDive = node =>
55

66
const isTagWithClassName = node =>
77
node.exists() && node.prop("className") && typeof node.type() === "string";
8-
8+
9+
const isStyledClass = className =>
10+
/^(\w+(-|_))?sc-/.test(className);
11+
912
const hasClassName = node =>
10-
node.length > 0
11-
&& typeof node.props === "function"
12-
&& node.props("className")
13-
&& node.props("className").className;
13+
node.length > 0
14+
&& typeof node.props === "function"
15+
&& node.prop("className")
16+
&& isStyledClass(node.prop("className"))
1417

1518
const getClassNames = received => {
1619
let className;
@@ -19,7 +22,7 @@ const getClassNames = received => {
1922
if (received.$$typeof === Symbol.for("react.test.json")) {
2023
className = received.props.className || received.props.class;
2124
} else if (hasClassName(received)) {
22-
className = received.props("className").className;
25+
className = received.prop("className");
2326
} else if (typeof received.exists === "function" && received.exists()) {
2427
const tree = shouldDive(received) ? received.dive() : received;
2528
const components = tree.findWhere(isTagWithClassName);
@@ -76,7 +79,7 @@ const getModifiedClassName = (className, staticClassName, modifier = "") => {
7679
};
7780

7881
const hasClassNames = (classNames, selectors, options) => {
79-
const staticClassNames = classNames.filter(x => /^(\w+(-|_))?sc-/.test(x));
82+
const staticClassNames = classNames.filter(x => isStyledClass(x));
8083

8184
return classNames.some(className =>
8285
staticClassNames.some(staticClassName =>

test/toHaveStyleRule.spec.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,13 +483,23 @@ it("nested with styling", () => {
483483
const Wrapper = styled.section`
484484
background: papayawhip;
485485
`;
486-
486+
const Children = styled.span`
487+
background: gray;
488+
`;
487489
const MyComponent = (props) => <Wrapper {...props} />;
488490
const MyStyledComponent = styled(MyComponent)`
489491
color: red;
490492
`;
491-
492-
toHaveStyleRule(<MyStyledComponent/>, "color", "red");
493+
const ParentComponent = (props) => (
494+
<MyStyledComponent {...props}>
495+
<Children className="test-class" />
496+
</MyStyledComponent>
497+
);
498+
499+
toHaveStyleRule(<MyStyledComponent />, "color", "red");
500+
toHaveStyleRule(<MyStyledComponent className="test-class" />, "color", "red");
501+
expect(shallow(<ParentComponent/>).find(Children)).toHaveStyleRule("background", "gray");
502+
expect(mount(<ParentComponent/>).find(Children)).toHaveStyleRule("background", "gray");
493503
});
494504

495505
it("empty children", () => {

0 commit comments

Comments
 (0)