Skip to content

Commit

Permalink
Fix native toHaveStyleRule to work with object style props (#337)
Browse files Browse the repository at this point in the history
Co-authored-by: Evan Jacobs <[email protected]>
  • Loading branch information
sayav and quantizor authored Nov 21, 2021
1 parent a712c13 commit b01cd89
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/native/toHaveStyleRule.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { matcherTest, buildReturnMessage } = require('../utils');

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

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

Expand Down
10 changes: 10 additions & 0 deletions test/native/toHaveStyleRule.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,13 @@ it('theming', () => {

expect(renderer.create(component).toJSON()).toHaveStyleRule('color', 'mediumseagreen');
});

it('style prop is an object', () => {
const StyledView = styled.TouchableOpacity`
background-color: papayawhip;
`;
const tree = renderer.create(<StyledView />).toJSON();

expect(typeof tree.props.style).toBe('object')
expect(tree).toHaveStyleRule('background-color', 'papayawhip');
});

0 comments on commit b01cd89

Please sign in to comment.