software-mansion / react-native-reanimated

React Native's Animated library reimplemented
https://docs.swmansion.com/react-native-reanimated/
MIT License
9.09k stars 1.32k forks source link

Fix `getAnimatedStyle` error when called with component that doesn't have animated styles #6746

Open AcostaB opened 17 hours ago

AcostaB commented 17 hours ago

isEmpty will return false if it is passed in undefined, instead of throwing an error.

Summary

If getAnimatedStyle is passed in a component that doesn't have animated styles, it throws an error saying "Cannot convert undefined or null to object". This happens because jestUtils.ts has an isEmpty function that calls Object.keys with an argument that can potentially be undefined. The arg can potentially be undefined because it comes from const jestAnimatedStyleValue = component.props.jestAnimatedStyle?.value;

This issue was introduced in version 3.16.0.

Test plan

Can be simply tested on a browser console. Just run:

(obj => Object.keys(obj).length === 0)(undefined) // throws error

(obj => !obj || Object.keys(obj).length === 0)(undefined) // returns true as expected

(obj => !obj || Object.keys(obj).length === 0)({test: 'something'}) // returns false as expected