Skip to content

Commit 59fcc1b

Browse files
committed
Support promise for TestDataBuilder.prototype.buildTo
1 parent 1d5fca7 commit 59fcc1b

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

lib/test-data-builder.js

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var extend = require('util')._extend;
2-
var async = require('async');
1+
const extend = require('util')._extend;
2+
const Promise = require('bluebird');
33

44
module.exports = exports = TestDataBuilder;
55

@@ -46,7 +46,7 @@ function TestDataBuilder() {
4646
* for required properties not listed.
4747
* @return TestDataBuilder (fluent interface)
4848
*/
49-
TestDataBuilder.prototype.define = function(name, Model, properties) {
49+
TestDataBuilder.prototype.define = function (name, Model, properties) {
5050
this._definitions.push({
5151
name: name,
5252
model: Model,
@@ -61,7 +61,7 @@ TestDataBuilder.prototype.define = function(name, Model, properties) {
6161
* is the name passed to `define()` and {property} is the name of
6262
* the property to use.
6363
*/
64-
TestDataBuilder.ref = function(path) {
64+
TestDataBuilder.ref = function (path) {
6565
return new Reference(path);
6666
};
6767

@@ -70,36 +70,31 @@ TestDataBuilder.ref = function(path) {
7070
* the supplied context object.
7171
* @param {Object.<string, Object>} context The context to object to populate.
7272
* @param {function(Error)} callback Callback.
73+
* @return {Promise}
7374
*/
74-
TestDataBuilder.prototype.buildTo = function(context, callback) {
75+
TestDataBuilder.prototype.buildTo = function (context, callback) {
7576
this._context = context;
76-
async.eachSeries(
77-
this._definitions,
78-
this._buildObject.bind(this),
79-
callback);
77+
return Promise.mapSeries(this._definitions, this._buildObject.bind(this)).nodeify(callback);
8078
};
8179

82-
TestDataBuilder.prototype._buildObject = function(definition, callback) {
80+
TestDataBuilder.prototype._buildObject = function (definition, callback) {
8381
var defaultValues = this._gatherDefaultPropertyValues(definition.model);
8482
var values = extend(defaultValues, definition.properties || {});
8583
var resolvedValues = this._resolveValues(values);
86-
87-
definition.model.create(resolvedValues, function(err, result) {
88-
if (err) {
89-
console.error(
90-
'Cannot build object %j - %s\nDetails: %j',
91-
definition,
92-
err.message,
93-
err.details);
94-
} else {
95-
this._context[definition.name] = result;
96-
}
97-
98-
callback(err);
99-
}.bind(this));
84+
var that = this;
85+
86+
return Promise.resolve(definition.model.create(resolvedValues)).then(function (result) {
87+
that._context[definition.name] = result;
88+
}).catch(function (err) {
89+
console.error(
90+
'Cannot build object %j - %s\nDetails: %j',
91+
definition,
92+
err.message,
93+
err.details);
94+
}).nodeify(callback);
10095
};
10196

102-
TestDataBuilder.prototype._resolveValues = function(values) {
97+
TestDataBuilder.prototype._resolveValues = function (values) {
10398
var result = {};
10499
for (var key in values) {
105100
var val = values[key];
@@ -112,7 +107,7 @@ TestDataBuilder.prototype._resolveValues = function(values) {
112107
};
113108

114109
var valueCounter = 0;
115-
TestDataBuilder.prototype._gatherDefaultPropertyValues = function(Model) {
110+
TestDataBuilder.prototype._gatherDefaultPropertyValues = function (Model) {
116111
var result = {};
117112
Model.forEachProperty(function createDefaultPropertyValue(name) {
118113
var prop = Model.definition.properties[name];
@@ -163,11 +158,11 @@ function Reference(path) {
163158
this._path = path;
164159
}
165160

166-
Reference.prototype.resolveFromContext = function(context) {
161+
Reference.prototype.resolveFromContext = function (context) {
167162
var elements = this._path.split('.');
168163

169164
var result = elements.reduce(
170-
function(obj, prop) {
165+
function (obj, prop) {
171166
return obj[prop];
172167
},
173168
context

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"author": "Ritchie Martori",
1414
"dependencies": {
15-
"async": "^2.0.1",
15+
"bluebird": "^3.4.6",
1616
"chai": "^3.5.0",
1717
"supertest": "^2.0.0"
1818
},

0 commit comments

Comments
 (0)