All files / app/assets/javascripts/vue_shared/components/lib/utils props_utils.js

100% Statements 11/11
87.5% Branches 14/16
100% Functions 3/3
100% Lines 11/11

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                      1x 9x 14x 15x 15x 3x         12x 1x           11x 9x       10x    
/**
 * Return the union of the given components' props options. Required props take
 * precendence over non-required props of the same name.
 *
 * This makes two assumptions:
 *  - All given components define their props in verbose object format.
 *  - The components all agree on the `type` of a common prop.
 *
 * @param {object[]} components The components to derive the union from.
 * @returns {object} The union of the props of the given components.
 */
export const propsUnion = (components) =>
  components.reduce((acc, component) => {
    Object.entries(component.props ?? {}).forEach(([propName, propOptions]) => {
      Eif (process.env.NODE_ENV !== 'production') {
        if (typeof propOptions !== 'object' || !('type' in propOptions)) {
          throw new Error(
            `Cannot create props union: expected verbose prop options for prop "${propName}"`,
          );
        }
 
        if (propName in acc && acc[propName]?.type !== propOptions?.type) {
          throw new Error(
            `Cannot create props union: incompatible prop types for prop "${propName}"`,
          );
        }
      }
 
      if (!(propName in acc) || propOptions.required) {
        acc[propName] = propOptions;
      }
    });
 
    return acc;
  }, {});