diff --git a/index.js b/index.js index 67e57b3..e69de29 100644 --- a/index.js +++ b/index.js @@ -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); -}); -*/ diff --git a/test/comment-service.js b/test/comment-service.js new file mode 100644 index 0000000..29306e6 --- /dev/null +++ b/test/comment-service.js @@ -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); + }); + }); +}); \ No newline at end of file diff --git a/test/mocks/comment-factory.js b/test/mocks/comment-factory.js new file mode 100644 index 0000000..24ea54e --- /dev/null +++ b/test/mocks/comment-factory.js @@ -0,0 +1,9 @@ +"use strict"; + +var comment_model_mock = require('./models/comment'); + +module.exports = { + get_model () { + return comment_model_mock; + } +}; \ No newline at end of file diff --git a/test/mocks/models/comment.js b/test/mocks/models/comment.js new file mode 100644 index 0000000..4f6e991 --- /dev/null +++ b/test/mocks/models/comment.js @@ -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; \ No newline at end of file