Skip to content

Instantly share code, notes, and snippets.

@Golodhros
Last active December 30, 2021 18:03
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Golodhros/597bca34394ea855bc9b to your computer and use it in GitHub Desktop.
Save Golodhros/597bca34394ea855bc9b to your computer and use it in GitHub Desktop.
JavaScript Object Builder
/**
* @summary Generates a testObjectBuilder example object
* @requires underscore, requirejs/commonjs
*/
var testObjectBuilder = function(require) {
'use strict';
var _ = require('underscore'),
OrderModel = require('./order_model');
/**
* @summary Generates Order Model test Objects
* @constructor
* @param {config} object
* @example
* var orderModel = new OrderModelBuilder()
* .withPaidTicket()
* .withSoldOutState()
* .build();
*
*/
function OrderModelBuilder(config) {
this.Klass = OrderModelBuilder;
this.config = _.defaults(config || {}, {
// Default properties
});
// Categories
this.withPaidTicket = function() {
var attributes = _.extend({}, this.config, {
// Paid order attributes
});
return new this.Klass(attributes);
};
// States
this.withSoldOutState = function() {
var attributes = _.extend({}, this.config, {
// SoldOut order attributes
});
return new this.Klass(attributes);
};
this.build = function() {
return new OrderModel(this.config);
};
}
return {
OrderModelBuilder: OrderModelBuilder
};
};
// Example of use along with a factory function:
function aModel() {
return new testObjectBuilder.OrderModelBuilder();
}
// Within the beforeEach statement
this.model = aModel()
.withPaidTicket()
.withSoldOutState()
.build();
// Here is a basic object builder template:
function NAMEBuilder(config) {
this.Klass = NAMEBuilder;
this.config = _.defaults(config || {}, {
// Default config
});
this.withFEATURE = function() {
var attributes = _.extend({}, this.config, {
// Specific config
});
return new this.Klass(attributes);
};
this.build = function() {
return new NameOfObject(this.config);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment