All files / packages/design-system/shared/utils react.js

80% Statements 8/10
83.33% Branches 5/6
100% Functions 5/5
75% Lines 6/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          61x   2x 63x               2x 42x           40x                          
// Copyright (c) 2015-present, salesforce.com, inc. All rights reserved
// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license
 
import React from 'react';
 
export const flatten = items => items.reduce((a, b) => a.concat(b), []);
 
export const flattenElement = element =>
  React.isValidElement(element)
    ? [element].concat(
        flatten(
          React.Children.toArray(element.props.children).map(flattenElement)
        )
      )
    : [element];
 
export const mapElement = (element, fn) =>
  React.isValidElement(element)
    ? fn(
        React.cloneElement(element, {
          children: React.isValidElement(element.props.children)
            ? fn(element.props.children)
            : React.Children.map(element.props.children, element =>
                mapElement(element, fn)
              )
        })
      )
    : element;
 
export const renderWithBetterError = ReactDOM => (element, msg) => {
  try {
    return ReactDOM.renderToStaticMarkup(element);
  } catch (e) {
    throw new Error(`${msg}: ${e}`);
  }
};