From b83fc256138e70ef98bc75d6ae26f1c889a4b7ea Mon Sep 17 00:00:00 2001 From: Sebastian Gierlinger Date: Mon, 24 Mar 2014 14:49:23 +0100 Subject: [PATCH] Fix delete button closes #2492 - added when.all() to wait until all posts are deleted before deleting tags - added a test --- core/server/models/index.js | 17 ++++---- core/test/integration/api/api_db_spec.js | 53 ++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 core/test/integration/api/api_db_spec.js diff --git a/core/server/models/index.js b/core/server/models/index.js index 16de3246ef..86554b0bb5 100644 --- a/core/server/models/index.js +++ b/core/server/models/index.js @@ -1,5 +1,6 @@ var migrations = require('../data/migration'), - _ = require('lodash'); + _ = require('lodash'), + when = require('when'); module.exports = { Post: require('./post').Post, @@ -20,14 +21,14 @@ module.exports = { var self = this; return self.Post.browse().then(function (posts) { - _.each(posts.toJSON(), function (post) { - self.Post.destroy(post.id); - }); + return when.all(_.map(posts.toJSON(), function (post) { + return self.Post.destroy(post.id); + })); }).then(function () { - self.Tag.browse().then(function (tags) { - _.each(tags.toJSON(), function (tag) { - self.Tag.destroy(tag.id); - }); + return self.Tag.browse().then(function (tags) { + return when.all(_.map(tags.toJSON(), function (tag) { + return self.Tag.destroy(tag.id); + })); }); }); } diff --git a/core/test/integration/api/api_db_spec.js b/core/test/integration/api/api_db_spec.js new file mode 100644 index 0000000000..cb3c5a9753 --- /dev/null +++ b/core/test/integration/api/api_db_spec.js @@ -0,0 +1,53 @@ +/*globals describe, before, beforeEach, afterEach, it */ +var testUtils = require('../../utils'), + should = require('should'), + + // Stuff we are testing + DataGenerator = require('../../utils/fixtures/data-generator'), + dbAPI = require('../../../server/api/db'); + TagsAPI = require('../../../server/api/tags'); + PostAPI = require('../../../server/api/posts'); + +describe('DB API', function () { + + before(function (done) { + testUtils.clearData().then(function () { + done(); + }, done); + }); + + beforeEach(function (done) { + testUtils.initData() + .then(function () { + return testUtils.insertDefaultFixtures(); + }) + .then(function () { + done(); + }, done); + }); + + afterEach(function (done) { + testUtils.clearData().then(function () { + done(); + }, done); + }); + + it('delete all content', function (done) { + + dbAPI.deleteAllContent().then(function (result){ + should.exist(result.message); + result.message.should.equal('Successfully deleted all content from your blog.') + }).then(function () { + TagsAPI.browse().then(function (results) { + should.exist(results); + results.length.should.equal(0); + }); + }).then(function () { + PostAPI.browse().then(function (results) { + should.exist(results); + results.posts.length.should.equal(0); + done(); + }); + }).then(null, done); + }); +}); \ No newline at end of file