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

Fix native toHaveStyleRule to work with object style props #337

Merged
merged 2 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -14,7 +14,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 =
!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 @@ -138,3 +138,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');
});