Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.