Skip to content

Commit b01cd89

Browse files
a4n1quantizor
andauthored
Fix native toHaveStyleRule to work with object style props (#337)
Co-authored-by: Evan Jacobs <[email protected]>
1 parent a712c13 commit b01cd89

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/native/toHaveStyleRule.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { matcherTest, buildReturnMessage } = require('../utils');
22

3-
function toHaveStyleRule(component, property, expected) {
4-
const styles = component.props.style.filter((x) => x);
3+
function toHaveStyleRule({ props: {style} }, property, expected) {
4+
const styles = Array.isArray(style) ? style.filter(x => x) : style
55

66
/**
77
* Convert style name to camel case (so we can compare)
@@ -12,7 +12,10 @@ function toHaveStyleRule(component, property, expected) {
1212
* Merge all styles into one final style object and search for the desired
1313
* stylename against this object
1414
*/
15-
const mergedStyles = styles.reduce((acc, item) => (Object.assign({}, acc, item)), {});
15+
const mergedStyles =
16+
Array.isArray(styles)
17+
? styles.reduce((acc, item) => (Object.assign({}, acc, item)), {})
18+
: styles
1619
const received = mergedStyles[camelCasedProperty];
1720
const pass = matcherTest(received, expected, this.isNot);
1821

test/native/toHaveStyleRule.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,13 @@ it('theming', () => {
150150

151151
expect(renderer.create(component).toJSON()).toHaveStyleRule('color', 'mediumseagreen');
152152
});
153+
154+
it('style prop is an object', () => {
155+
const StyledView = styled.TouchableOpacity`
156+
background-color: papayawhip;
157+
`;
158+
const tree = renderer.create(<StyledView />).toJSON();
159+
160+
expect(typeof tree.props.style).toBe('object')
161+
expect(tree).toHaveStyleRule('background-color', 'papayawhip');
162+
});

0 commit comments

Comments
 (0)