All files / generate mock.ts

100% Statements 12/12
100% Branches 4/4
100% Functions 2/2
100% Lines 12/12

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    5x         5x 39x   39x 39x   108x     39x       108x     93x 93x   108x     39x    
import { Blueprint, BlueprintConstantProperties } from '../blueprint/blueprint';
 
import { transform } from './transform';
 
/**
 * Generate a mock from a blueprint
 */
export function generate<T>(blueprint: Blueprint<T>, withDefaults?: BlueprintConstantProperties<T>): T {
  const mock: T = Object.create(null);
 
  const allKeys = Array.from(new Set(Object.keys(blueprint).concat(Object.keys({ ...withDefaults }))));
  allKeys.forEach(key => {
    let value: unknown;
    if (withDefaults) {
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      value = withDefaults[key];
    }
 
    // otherwise, use the generator
    if(value === undefined) {
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      const generator = blueprint[key];
      value = generator();
    }
    Object.assign(mock, { [key]: value });
  });
 
  return transform<T>(mock);
}