0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

using promises correctly in tests

This commit is contained in:
Tim Griesser 2013-05-26 19:53:44 -04:00
parent a3d2fb7aa9
commit 3446fe7461
3 changed files with 100 additions and 132 deletions

View file

@ -26,9 +26,7 @@
results.length.should.equal(2); results.length.should.equal(2);
done(); done();
}, function (error) { }, done);
throw error;
});
}); });
it('can read', function (done) { it('can read', function (done) {
@ -41,18 +39,13 @@
firstPost = results.models[0]; firstPost = results.models[0];
posts.read({slug: firstPost.attributes.slug}).then(function (found) { return posts.read({slug: firstPost.attributes.slug});
should.exist(found); }).then(function (found) {
should.exist(found);
found.attributes.title.should.equal(firstPost.attributes.title); found.attributes.title.should.equal(firstPost.attributes.title);
done(); done();
}, function (error) {
throw error;
});
}, function (error) {
throw error;
}); });
}); });
@ -60,25 +53,24 @@
var firstPost; var firstPost;
posts.browse().then(function (results) { posts.browse().then(function (results) {
should.exist(results); should.exist(results);
results.length.should.be.above(0); results.length.should.be.above(0);
firstPost = results.models[0]; firstPost = results.models[0];
posts.edit({id: firstPost.id, title: "new title"}).then(function (edited) { return posts.edit({id: firstPost.id, title: "new title"});
should.exist(edited);
edited.attributes.title.should.equal('new title'); }).then(function (edited) {
done(); should.exist(edited);
}, function (error) {
throw error;
});
}, function (error) { edited.attributes.title.should.equal('new title');
throw error;
}); done();
}, done);
}); });
it('can add', function (done) { it('can add', function (done) {
@ -101,39 +93,35 @@
}); });
it('can delete', function (done) { it('can delete', function (done) {
var firstPostId, var firstPostId;
ids,
hasDeletedId;
posts.browse().then(function (results) { posts.browse().then(function (results) {
should.exist(results); should.exist(results);
results.length.should.be.above(0); results.length.should.be.above(0);
firstPostId = results.models[0].id; firstPostId = results.models[0].id;
posts.destroy(firstPostId).then(function () { return posts.destroy(firstPostId);
posts.browse().then(function (newResults) { }).then(function () {
ids = _.pluck(newResults.models, "id"); return posts.browse();
hasDeletedId = _.any(ids, function (id) { }).then(function (newResults) {
return id === firstPostId; var ids, hasDeletedId;
});
hasDeletedId.should.equal(false); ids = _.pluck(newResults.models, "id");
done(); hasDeletedId = _.any(ids, function (id) {
}, function (error) { return id === firstPostId;
throw error;
});
}, function (error) {
throw error;
}); });
}, function (error) {
throw error; hasDeletedId.should.equal(false);
});
done();
}, done);
}); });
}); });
}()); }());

View file

@ -27,9 +27,7 @@
results.length.should.be.above(0); results.length.should.be.above(0);
done(); done();
}, function (error) { }, done);
throw error;
});
}); });
it('can read', function (done) { it('can read', function (done) {
@ -43,20 +41,17 @@
firstSetting = results.models[0]; firstSetting = results.models[0];
settings.read(firstSetting.attributes.key).then(function (found) { return settings.read(firstSetting.attributes.key);
should.exist(found); }).then(function (found) {
found.attributes.value.should.equal(firstSetting.attributes.value); should.exist(found);
done(); found.attributes.value.should.equal(firstSetting.attributes.value);
}, function (error) {
throw error;
});
}, function (error) { done();
throw error;
}); }, done);
}); });
it('can edit single', function (done) { it('can edit single', function (done) {
@ -71,29 +66,26 @@
firstPost = results.models[0]; firstPost = results.models[0];
// The edit method has been modified to take an object of // The edit method has been modified to take an object of
// key/value pairs // key/value pairs
toEdit[firstPost.attributes.key] = "new value"; toEdit[firstPost.attributes.key] = "new value";
settings.edit(toEdit).then(function (edited) { return settings.edit(toEdit);
should.exist(edited); }).then(function (edited) {
edited.length.should.equal(1); should.exist(edited);
edited = edited[0]; edited.length.should.equal(1);
edited.attributes.key.should.equal(firstPost.attributes.key); edited = edited[0];
edited.attributes.value.should.equal('new value');
done(); edited.attributes.key.should.equal(firstPost.attributes.key);
}, function (error) { edited.attributes.value.should.equal('new value');
throw error;
});
}, function (error) { done();
throw error;
}); }, done);
}); });
it('can edit multiple', function (done) { it('can edit multiple', function (done) {
@ -111,34 +103,32 @@
firstPost = results.models[0]; firstPost = results.models[0];
secondPost = results.models[1]; secondPost = results.models[1];
// The edit method has been modified to take an object of // The edit method has been modified to take an object of
// key/value pairs // key/value pairs
toEdit[firstPost.attributes.key] = "new value1"; toEdit[firstPost.attributes.key] = "new value1";
toEdit[secondPost.attributes.key] = "new value2"; toEdit[secondPost.attributes.key] = "new value2";
settings.edit(toEdit).then(function (edited) { return settings.edit(toEdit);
should.exist(edited); }).then(function (edited) {
edited.length.should.equal(2); should.exist(edited);
editedPost = edited[0]; edited.length.should.equal(2);
editedPost.attributes.key.should.equal(firstPost.attributes.key); editedPost = edited[0];
editedPost.attributes.value.should.equal('new value1');
editedPost = edited[1]; editedPost.attributes.key.should.equal(firstPost.attributes.key);
editedPost.attributes.value.should.equal('new value1');
editedPost.attributes.key.should.equal(secondPost.attributes.key); editedPost = edited[1];
editedPost.attributes.value.should.equal('new value2');
done(); editedPost.attributes.key.should.equal(secondPost.attributes.key);
}, function (error) { editedPost.attributes.value.should.equal('new value2');
throw error;
}); done();
}, function (error) {
throw error; }, done);
});
}); });
it('can add', function (done) { it('can add', function (done) {
@ -155,15 +145,11 @@
createdSetting.attributes.value.should.equal(newSetting.value, "value is correct"); createdSetting.attributes.value.should.equal(newSetting.value, "value is correct");
done(); done();
}, function (error) { }, done);
throw error;
});
}); });
it('can delete', function (done) { it('can delete', function (done) {
var firstSettingId, var firstSettingId;
ids,
hasDeletedId;
settings.browse().then(function (results) { settings.browse().then(function (results) {
@ -173,28 +159,27 @@
firstSettingId = results.models[0].id; firstSettingId = results.models[0].id;
settings.destroy(firstSettingId).then(function () { return settings.destroy(firstSettingId);
settings.browse().then(function (newResults) { }).then(function () {
ids = _.pluck(newResults.models, "id"); return settings.browse();
hasDeletedId = _.any(ids, function (id) { }).then(function (newResults) {
return id === firstSettingId;
});
hasDeletedId.should.equal(false); var ids, hasDeletedId;
done(); ids = _.pluck(newResults.models, "id");
}, function (error) {
throw error; hasDeletedId = _.any(ids, function (id) {
}); return id === firstSettingId;
}, function (error) {
throw error;
}); });
}, function (error) {
throw error; hasDeletedId.should.equal(false);
});
done();
}, done);
}); });
}); });
}()); }());

View file

@ -103,9 +103,7 @@
}); });
it('can delete', function (done) { it('can delete', function (done) {
var firstUserId, var firstUserId;
ids,
hasDeletedId;
users.browse().then(function (results) { users.browse().then(function (results) {
@ -115,33 +113,30 @@
firstUserId = results.models[0].id; firstUserId = results.models[0].id;
users.destroy(firstUserId).then(function () { return users.destroy(firstUserId);
users.browse().then(function (newResults) { }).then(function () {
if (newResults.length < 1) { return users.browse();
// Bug out if we only had one user and deleted it.
return done();
}
ids = _.pluck(newResults.models, "id"); }).then(function (newResults) {
var ids, hasDeletedId;
hasDeletedId = _.any(ids, function (id) { if (newResults.length < 1) {
return id === firstUserId; // Bug out if we only had one user and deleted it.
}); return done();
}
hasDeletedId.should.equal(false); ids = _.pluck(newResults.models, "id");
done(); hasDeletedId = _.any(ids, function (id) {
}, function (error) { return id === firstUserId;
throw error;
});
}, function (error) {
throw error;
}); });
}, function (error) {
throw error; hasDeletedId.should.equal(false);
});
done();
}, done);
}); });
}); });