All files FakerFaker.ts

100% Statements 18/18
100% Branches 5/5
100% Functions 5/5
100% Lines 18/18

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 38 39 40 41 42 431x     1x       5x     5x 12x 16x   16x   6x 3x 3x 4x     3x   6x     10x 10x     16x             5x 5x      
import { fake } from "faker"
import { IFaker } from "./IFaker"
 
export default class FakerFaker implements IFaker {
  schema: string
 
  constructor(schema: string) {
    this.schema = schema
  }
 
  private schemaReducer = (schema: any): any =>
    Object.keys(schema).reduce((acc: any, key: string) => {
      const value = schema[key]
      let generatedFake
      switch (typeof value) {
        case "object":
          if (Array.isArray(value)) {
            const [{ quantity }, s] = value
            generatedFake = [...Array(quantity)].map(() =>
              this.schemaReducer(s)
            )
          } else {
            generatedFake = this.schemaReducer(value)
          }
          break
        case "string":
        default:
          generatedFake = fake(value)
          break
      }
 
      return {
        ...acc,
        [key]: generatedFake,
      }
    }, {})
 
  Fake(): any {
    const schema = JSON.parse(this.schema)
    return this.schemaReducer(schema)
  }
}