Skip to content

Commit

Permalink
fix: toBeVisible edge case for style values (#1640)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski authored Jul 26, 2024
1 parent 2f4568a commit be31579
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/matchers/__tests__/to-be-visible.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,12 @@ test('toBeVisible() on non-React elements', () => {
Received has value: true"
`);
});

test('toBeVisible() does not throw on invalid style', () => {
// @ts-expect-error: intentionally passing invalid style to
// trigger StyleSheet.flatten() returning undefined.
render(<View testID="view" style={0} />);

const view = screen.getByTestId('view');
expect(view).toBeVisible();
});
5 changes: 2 additions & 3 deletions src/matchers/to-be-visible.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ function isElementVisible(
}

function isHiddenForStyles(element: ReactTestInstance) {
const style = element.props.style ?? {};
const { display, opacity } = StyleSheet.flatten(style);
return display === 'none' || opacity === 0;
const flatStyle = StyleSheet.flatten(element.props.style);
return flatStyle?.display === 'none' || flatStyle?.opacity === 0;
}

0 comments on commit be31579

Please sign in to comment.