UNPKG

@mixtape/core

Version:

Supercharged fixture library for organizing and generating test data

107 lines (106 loc) 3.35 kB
import { Extension } from './extension'; import TypeComposer from './type-composer'; import { ValueGenerator } from './generators/value-generator'; import ObjectBuilder from './object-builder'; import { TypeBuilder } from './builder'; /** * Base fixture class. * @implements {FixtureContext} */ export declare class Fixture implements FixtureContext { private _frozenTypes; private readonly _generator; private readonly _extensions; /** * Create a new `Fixture` * @param generator - generator to use when generating numbers * @param extensionDecorators - decorators to apply to builders being added */ constructor(generator: ValueGenerator<number>, extensionDecorators?: Array<(new (decoratee: TypeBuilder<any>) => TypeBuilder<any>)>); /** * Get `Extension` containing all builders used for fixture * @returns `Extension` */ readonly extensions: Extension; /** * Add `Extension` to fixture * @param extension - extension to add * @returns `this` */ extend(extension: Extension): this; /** * Freeze to type to ensure the same (randomly generated) value is used everytime * @param type - type to freeze * @returns `this` */ freeze(type: string): this; /** * Set specific value to use when generating type * @param type - the targeted type * @param value - value to use for the targeted type * @returns `this` */ use<T>(type: string, value: T): this; /** * Create single type * @param type - type to create * @returns type */ create<T>(type: string): T; /** * Create array of a given type * @param type - type to create * @param size - size of array to create (optional) * @returns `Array` of types */ createMany<T>(type: string, size?: number): T[]; /** * Build type with custom values * @param type - type to build * @returns `TypeComposer` */ build<T extends object>(type: string): TypeComposer<T>; /** * Create object from a template * @param template - template to use when building object * @returns `ObjectBuilder` */ from<T extends object>(template: T): ObjectBuilder<T>; /** * Reset fixture, i.e. clear frozen values */ reset(): void; } /** * Interface for a fixture context. * A fixture context is usually injected into different classes/function to give easy access * to data creation and to ensure functionality like 'freeze' and 'use' is working. * @interface */ export interface FixtureContext { /** * Create single type * @param type - type to create * @returns type */ create<T>(type: string): T; /** * Create array of a given type * @param type - type to create * @param size - size of array to create (optional) * @returns `Array` of types */ createMany<T>(type: string, size?: number): T[]; /** * Build type with custom values * @param type - type to build * @returns `TypeComposer` */ build<T extends object>(type: string): TypeComposer<T>; /** * Create object from a template * @param template - template to use for building object * @returns `ObjectBuilder` */ from<T extends object>(template: T): ObjectBuilder<T>; }