From 5e3946a6be0649ba7188f7805eeca21fe7e4b390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 12 May 2017 20:50:01 -0400 Subject: [PATCH] teaser tests --- src/topics/teaser.js | 32 +++++++------ test/topics.js | 112 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 14 deletions(-) diff --git a/src/topics/teaser.js b/src/topics/teaser.js index f864e3e23d..d62dc2fe72 100644 --- a/src/topics/teaser.js +++ b/src/topics/teaser.js @@ -1,9 +1,9 @@ - 'use strict'; var async = require('async'); var S = require('string'); +var winston = require('winston'); var meta = require('../meta'); var user = require('../user'); @@ -14,6 +14,7 @@ var utils = require('../utils'); module.exports = function (Topics) { Topics.getTeasers = function (topics, uid, callback) { if (typeof uid === 'function') { + winston.warn('[Topics.getTeasers] this usage is deprecated please provide uid'); callback = uid; uid = 0; } @@ -108,6 +109,7 @@ module.exports = function (Topics) { Topics.getTeasersByTids = function (tids, uid, callback) { if (typeof uid === 'function') { + winston.warn('[Topics.getTeasersByTids] this usage is deprecated please provide uid'); callback = uid; uid = 0; } @@ -116,7 +118,7 @@ module.exports = function (Topics) { } async.waterfall([ function (next) { - Topics.getTopicsFields(tids, ['tid', 'postcount', 'teaserPid'], next); + Topics.getTopicsFields(tids, ['tid', 'postcount', 'teaserPid', 'mainPid'], next); }, function (topics, next) { Topics.getTeasers(topics, uid, next); @@ -126,6 +128,7 @@ module.exports = function (Topics) { Topics.getTeaser = function (tid, uid, callback) { if (typeof uid === 'function') { + winston.warn('[Topics.getTeaser] this usage is deprecated please provide uid'); callback = uid; uid = 0; } @@ -135,17 +138,18 @@ module.exports = function (Topics) { }; Topics.updateTeaser = function (tid, callback) { - Topics.getLatestUndeletedReply(tid, function (err, pid) { - if (err) { - return callback(err); - } - - pid = pid || null; - if (pid) { - Topics.setTopicField(tid, 'teaserPid', pid, callback); - } else { - Topics.deleteTopicField(tid, 'teaserPid', callback); - } - }); + async.waterfall([ + function (next) { + Topics.getLatestUndeletedReply(tid, next); + }, + function (pid, next) { + pid = pid || null; + if (pid) { + Topics.setTopicField(tid, 'teaserPid', pid, next); + } else { + Topics.deleteTopicField(tid, 'teaserPid', next); + } + }, + ], callback); }; }; diff --git a/test/topics.js b/test/topics.js index 6782f1041d..c362ca0824 100644 --- a/test/topics.js +++ b/test/topics.js @@ -8,6 +8,7 @@ var nconf = require('nconf'); var db = require('./mocks/databasemock'); var topics = require('../src/topics'); var categories = require('../src/categories'); +var meta = require('../src/meta'); var User = require('../src/user'); var groups = require('../src/groups'); var helpers = require('./helpers'); @@ -1524,6 +1525,117 @@ describe('Topic\'s', function () { }); }); + describe('teasers', function () { + var topic1; + var topic2; + before(function (done) { + async.series([ + function (next) { + topics.post({ uid: adminUid, title: 'topic 1', content: 'content 1', cid: categoryObj.cid }, next); + }, + function (next) { + topics.post({ uid: adminUid, title: 'topic 2', content: 'content 2', cid: categoryObj.cid }, next); + }, + ], function (err, results) { + assert.ifError(err); + topic1 = results[0]; + topic2 = results[1]; + done(); + }); + }); + + after(function (done) { + meta.config.teaserPost = ''; + done(); + }); + + + it('should return empty array if first param is empty', function (done) { + topics.getTeasers([], function (err, teasers) { + assert.ifError(err); + assert.equal(0, teasers.length); + done(); + }); + }); + + it('should get teasers with 2 params', function (done) { + topics.getTeasers([topic1.topicData, topic2.topicData], function (err, teasers) { + assert.ifError(err); + assert.deepEqual([undefined, undefined], teasers); + done(); + }); + }); + + it('should get teasers with first posts', function (done) { + meta.config.teaserPost = 'first'; + topics.getTeasers([topic1.topicData, topic2.topicData], function (err, teasers) { + assert.ifError(err); + assert.equal(2, teasers.length); + assert(teasers[0]); + assert(teasers[1]); + assert(teasers[0].tid, topic1.topicData.tid); + assert(teasers[0].content, 'content 1'); + assert(teasers[0].user.username, 'admin'); + done(); + }); + }); + + it('should get teasers even if one topic is falsy', function (done) { + topics.getTeasers([null, topic2.topicData], function (err, teasers) { + assert.ifError(err); + assert.equal(2, teasers.length); + assert.equal(undefined, teasers[0]); + assert(teasers[1]); + assert(teasers[1].tid, topic2.topicData.tid); + assert(teasers[1].content, 'content 2'); + assert(teasers[1].user.username, 'admin'); + done(); + }); + }); + + it('should get teasers with first posts', function (done) { + meta.config.teaserPost = 'last-post'; + topics.reply({ uid: adminUid, content: 'reply 1 content', tid: topic1.topicData.tid }, function (err, result) { + assert.ifError(err); + topic1.topicData.teaserPid = result.pid; + topics.getTeasers([topic1.topicData, topic2.topicData], function (err, teasers) { + assert.ifError(err); + assert(teasers[0]); + assert(teasers[1]); + assert(teasers[0].tid, topic1.topicData.tid); + assert(teasers[0].content, 'reply 1 content'); + done(); + }); + }); + }); + + it('should get teasers by tids', function (done) { + topics.getTeasersByTids([topic2.topicData.tid, topic1.topicData.tid], function (err, teasers) { + assert.ifError(err); + assert(2, teasers.length); + assert.equal(teasers[1].content, 'reply 1 content'); + done(); + }); + }); + + it('should return empty array ', function (done) { + topics.getTeasersByTids([], function (err, teasers) { + assert.ifError(err); + assert.equal(0, teasers.length); + done(); + }); + }); + + it('should get teaser by tid', function (done) { + topics.getTeaser(topic2.topicData.tid, function (err, teaser) { + assert.ifError(err); + assert(teaser); + assert.equal(teaser.content, 'content 2'); + done(); + }); + }); + }); + after(function (done) { db.emptydb(done); });