Skip to content

Commit

Permalink
Tests for CommentService
Browse files Browse the repository at this point in the history
  • Loading branch information
aantonovdevelop committed May 14, 2016
1 parent 8543cb8 commit 34b52d0
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 41 deletions.
41 changes: 0 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,41 +0,0 @@
var factories = require('./factories');

var UserService = require('./services/user-service');
var AuthorizationService = require('./services/authorization-service');
var CommentService = require('./services/comment-service');

var user_service = new UserService(factories.user_factory, factories.account_factory);
var authorization_service = new AuthorizationService(factories.account_factory, factories.token_factory);
var comment_service = new CommentService(factories.comment_factory);

/*
authorization_service.get_token('artem', '112233').then(token => {
console.log(token)
}).catch(err => {
console.log(err)
});
*/

/*
authorization_service.get_user('TzEk8VIipWzpfvoxNGBZq1EZZsYmzzTc').then((user) => {
comment_service.create_comment(user, 'First Nice Comment').then(() => {
comment_service.get_comments().then(console.log).catch(console.log);
}).catch(console.log);
}).catch(console.log);
*/

//user_service.get_users().then((user) => console.log(user[0].comments)).catch(console.log);
authorization_service.get_token('artem', '112233').then(token => {
authorization_service.get_user(token).then((user) => {
comment_service.create_comment(user, 'Six Nice Comment', '5734ea8f33aed5a357dfdd65').then(console.log).catch(console.error);
}).catch(console.error);
});


comment_service.get_comments().then(console.log);

/*
comment_service.get_level('5734d79fa589d0685f5268c2', function (level) {
console.log(level);
});
*/
91 changes: 91 additions & 0 deletions test/comment-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"use strict";

var assert = require('assert');

var CommentService = require('../services/comment-service'),
comment_factory = require('./mocks/comment-factory');

var comment_service = undefined,
maximum_nesting_lvl = 5;

describe('CommentService', function () {
describe('#constructor', function () {
it('Should create CommentService instance', function () {
comment_service = new CommentService(maximum_nesting_lvl, comment_factory);

assert.ok(comment_service instanceof CommentService);
assert.ok(comment_service.model);
});
});

describe('#get_comments', function () {
it('Should get all comments', function (done) {
comment_service.get_comments().then(comments => {
assert.ok(comments instanceof Array);
assert.ok(comments.length === 2);

assert.equal(comments[0].message, "test_message");
assert.equal(comments[1].message, "test_message");

done();
});
});
});

describe('#create_comment', function () {
var user = {
comments: [],
save: function () {
return Promise.resolve();
}
};

it('Should create new user comment', function (done) {
comment_service.nesting_level = maximum_nesting_lvl;

comment_service.create_comment(user, "some message", 1)
.then(done).catch(done);
});

it('Should throw error of nesting level', function (done) {
comment_service.nesting_level = 1;

comment_service.create_comment(user, "some message", 1)
.then(() => done(new Error('Should trow error')))
.catch(error => {
assert.equal(error.message, "Maximum level of nesting");

done();
});
});
});

describe('#nesting_level', function () {

var default_nesting_lvl = 4;

it('Should set nesting level', function () {
comment_service.nesting_level = 3;

assert.equal(comment_service._maximum_nesting_lvl, 3);
});

it('Should set default nesting level because new more than 10', function () {
comment_service.nesting_level = 11;

assert.equal(comment_service._maximum_nesting_lvl, default_nesting_lvl);
});

it('Should set default nesting level because new less than 1', function () {
comment_service.nesting_level = 0;

assert.equal(comment_service._maximum_nesting_lvl, default_nesting_lvl);
});

it('Should return current level of nesting', function () {
comment_service._maximum_nesting_lvl = default_nesting_lvl;

assert.equal(comment_service.nesting_level, default_nesting_lvl);
});
});
});
9 changes: 9 additions & 0 deletions test/mocks/comment-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

var comment_model_mock = require('./models/comment');

module.exports = {
get_model () {
return comment_model_mock;
}
};
34 changes: 34 additions & 0 deletions test/mocks/models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use strict";

var assert = require('assert');

function Comment(schema) {
assert.ok(schema.message);
assert.ok(schema.parent);

this.save = function () {
return Promise.resolve(Object.assign({_id: 1}, schema));
}
}

Comment.get_parent = function (id) {
return Promise.resolve((id < 5) ? ++id : undefined);
};

Comment.get_by_id = function (id) {
return Promise.resolve({_id: id, message: "test_message", parent: ++id});
};

Comment.get_all = function () {
return Promise.resolve([{
_id: 1,
message: "test_message",
parent: undefined
}, {
_id: 2,
message: "test_message",
parent: undefined
}]);
};

module.exports = Comment;

0 comments on commit 34b52d0

Please sign in to comment.