UNPKG

@mixtape/core

Version:

Supercharged fixture library for organizing and generating test data

118 lines 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var extension_1 = require("./extension"); var type_composer_1 = require("./type-composer"); var utils_1 = require("./utils"); var object_builder_1 = require("./object-builder"); /** * Base fixture class. * @implements {FixtureContext} */ var Fixture = /** @class */ (function () { /** * Create a new `Fixture` * @param generator - generator to use when generating numbers * @param extensionDecorators - decorators to apply to builders being added */ function Fixture(generator, extensionDecorators) { this._generator = generator; this._frozenTypes = {}; this._extensions = new extension_1.Extension(); if (extensionDecorators) this._extensions[extension_1.decorators] = extensionDecorators; } Object.defineProperty(Fixture.prototype, "extensions", { /** * Get `Extension` containing all builders used for fixture * @returns `Extension` */ get: function () { return this._extensions; }, enumerable: true, configurable: true }); /** * Add `Extension` to fixture * @param extension - extension to add * @returns `this` */ Fixture.prototype.extend = function (extension) { this._extensions.merge(extension); return this; }; /** * Freeze to type to ensure the same (randomly generated) value is used everytime * @param type - type to freeze * @returns `this` */ Fixture.prototype.freeze = function (type) { if (this._frozenTypes[type]) return this; var value = this.create(type); this._frozenTypes[type] = utils_1.isObject(value) ? Object.freeze(value) : value; return 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` */ Fixture.prototype.use = function (type, value) { this._frozenTypes[type] = value; return this; }; /** * Create single type * @param type - type to create * @returns type */ Fixture.prototype.create = function (type) { var builder = this._extensions.get(type); utils_1.ensure(function () { return builder !== undefined; }, "No builder defined for type or alias '" + type + "'", ReferenceError); if (this._frozenTypes[type]) { return this._frozenTypes[type]; } return builder.build(this); }; /** * Create array of a given type * @param type - type to create * @param size - size of array to create (optional) * @returns `Array` of types */ Fixture.prototype.createMany = function (type, size) { var list = []; size = size ? size : this._generator.generate(); for (var i = 0; i < size; i++) { list.push(this.create(type)); } return list; }; /** * Build type with custom values * @param type - type to build * @returns `TypeComposer` */ Fixture.prototype.build = function (type) { return new type_composer_1.default(type, this, this._generator); }; /** * Create object from a template * @param template - template to use when building object * @returns `ObjectBuilder` */ Fixture.prototype.from = function (template) { return new object_builder_1.default(template, this, this._generator); }; /** * Reset fixture, i.e. clear frozen values */ Fixture.prototype.reset = function () { this._frozenTypes = {}; }; return Fixture; }()); exports.Fixture = Fixture; //# sourceMappingURL=fixture.js.map