UNPKG

mockingbird

Version:

Super Simple, Yet Powerful, TypeScript Library for Creating Mocks. Mockingbird allows you to create class mocks like a breeze with a simple yet powerful @Mock decorator

95 lines (94 loc) 2.87 kB
import { Class } from '@mockingbird/common'; import { Keys, Mutations } from './types'; import { MockProducer } from './mock-producer'; export interface MockBuilder<TClass = any> { /** * Sets the faker locale inside the MockBuilder, thus, all the mocks that * will be generated from the builder will be affected from this setting * and will get applied by it * * @example * MockFactory(Bird).setLocale('es').one() * * @param locale {string} * @returns {this} the actual same builder */ setLocale(locale: string): this; /** * Converts the generated mock into a plain object. * When creating multiple mocks (using .many()), then * all the items in the array will be converted * into plain objects. * * @example * MockFactory(Bird).plain().one() * * @returns {MockBuilder} */ plain(): this; /** * Mutates the generated mock(s). * .mutate() method enables to set another value for a specific * property. * * @example * MockFactory(Bird).mutate({ name: 'some-permanent-name' }).one() * * @param mutations {Mutations<TClass>} */ mutate(mutations: Mutations<TClass>): Omit<MockBuilder<TClass>, 'mutate'>; /** * @alias omit * @param keys * @returns {MockBuilder} * @deprecated use .omit() instead */ ignore(...keys: Keys<TClass>): ReturnType<MockBuilder<TClass>['omit']>; /** * Omits properties from the generated mock(s); all the * rest of the properties will remain the same. * Notice that omitting a property that contains a default * value will only ignore the decorator generation. * * @example * MockFactory(Bird).ignore('name').one() * * @param keys * @returns {MockBuilder} */ omit(...keys: Keys<TClass>): this; /** * Picks specific properties to generate a mock from. * Notice that properties that contain default values * in the class will remain there but the decorator won't * apply to those properties. * * @example * MockFactory(Bird).pick('name').one() * * @param keys * @returns {MockBuilder} */ pick(...keys: Keys<TClass>): this; /** * Creates exactly one mock from the target class * * @returns {TClass | Object} */ one(): TClass; /** * Creates many mocks from the target class. * * @param count {number} How many mocks to create * @returns {TClass[] | Object[]} An array of instances of plain objects */ many(count: number): TClass[]; } export declare class MockBuilder<TClass = any> extends MockProducer<TClass> { private isPlain; private mutations; private omitKeys; private pickKeys; constructor(targetClass: Class<TClass>); private clean; }