All files / packages/react-native-web/jest createSerializer.js

96% Statements 24/25
94.44% Branches 17/18
100% Functions 6/6
96% Lines 24/25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 5743x       109x   95x 202x 202x 1x   202x       95x 37x 37x 37x           95x     95x 95x 66x 65x 77x     1x       95x     14x       1459x       30x     43x     43x  
const React = require('react');
 
function createSerializer(styleSheet) {
  function flattenNodeStyles(node) {
    if (node && node.props) {
      // check for React elements in any props
      const nextProps = Object.keys(node.props).reduce((acc, curr) => {
        const value = node.props[curr];
        if (React.isValidElement(value)) {
          acc[curr] = flattenNodeStyles(value);
        }
        return acc;
      }, {});
 
      // flatten styles and avoid empty objects in snapshots
      if (node.props.style) {
        const style = styleSheet.flatten(node.props.style);
        Eif (Object.keys(style).length > 0) {
          nextProps.style = style;
        } else {
          delete nextProps.style;
        }
      }
 
      const args = [node, nextProps];
 
      // recurse over children too
      const children = node.children || node.props.children;
      if (children) {
        if (Array.isArray(children)) {
          children.forEach(child => {
            args.push(flattenNodeStyles(child));
          });
        } else {
          args.push(flattenNodeStyles(children));
        }
      }
 
      return React.cloneElement.apply(React.cloneElement, args);
    }
 
    return node;
  }
 
  function test(value) {
    return !!value && value.$$typeof === Symbol.for('react.test.json');
  }
 
  function print(value, serializer) {
    return serializer(flattenNodeStyles(value));
  }
 
  return { test, print };
}
 
module.exports = createSerializer;