From 2fd2accf8c05a6a70ce856a0ab08a27de0704205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 15 Jun 2018 15:23:27 -0400 Subject: [PATCH] add tests for top topics fix popular page displaying 18 topics per page --- src/categories/topics.js | 2 +- src/topics/popular.js | 2 +- src/topics/recent.js | 2 +- src/topics/suggested.js | 8 +------- src/topics/top.js | 2 +- src/topics/unread.js | 6 +----- test/topics.js | 16 ++++++++++++++++ 7 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/categories/topics.js b/src/categories/topics.js index a56545cfa9..3b7038fbe0 100644 --- a/src/categories/topics.js +++ b/src/categories/topics.js @@ -57,7 +57,7 @@ module.exports = function (Categories) { function (results, next) { var totalPinnedCount = results.pinnedTids.length; - pinnedTids = results.pinnedTids.slice(data.start, data.stop === -1 ? undefined : data.stop + 1); + pinnedTids = results.pinnedTids.slice(data.start, data.stop !== -1 ? data.stop + 1 : undefined); var pinnedCount = pinnedTids.length; diff --git a/src/topics/popular.js b/src/topics/popular.js index 99855b17d3..95bed19613 100644 --- a/src/topics/popular.js +++ b/src/topics/popular.js @@ -53,7 +53,7 @@ module.exports = function (Topics) { function (topics, next) { tids = topics.filter(function (topic) { return topic && parseInt(topic.deleted, 10) !== 1; - }).sort(sortPopular).slice(start, stop !== -1 ? stop - 1 : undefined).map(function (topic) { + }).sort(sortPopular).slice(start, stop !== -1 ? stop + 1 : undefined).map(function (topic) { return topic.tid; }); privileges.topics.filterTids('read', tids, uid, next); diff --git a/src/topics/recent.js b/src/topics/recent.js index 5b717d9639..6fea81f972 100644 --- a/src/topics/recent.js +++ b/src/topics/recent.js @@ -41,7 +41,7 @@ module.exports = function (Topics) { }, function (tids, next) { recentTopics.topicCount = tids.length; - tids = tids.slice(start, stop + 1); + tids = tids.slice(start, stop !== -1 ? stop + 1 : undefined); Topics.getTopicsByTids(tids, uid, next); }, function (topicData, next) { diff --git a/src/topics/suggested.js b/src/topics/suggested.js index 187af3706a..bb450a759d 100644 --- a/src/topics/suggested.js +++ b/src/topics/suggested.js @@ -35,13 +35,7 @@ module.exports = function (Topics) { } }, function (categoryTids, next) { - tids = _.uniq(tids.concat(categoryTids)); - if (stop === -1) { - tids = tids.slice(start); - } else { - tids = tids.slice(start, stop + 1); - } - + tids = _.uniq(tids.concat(categoryTids)).slice(start, stop !== -1 ? stop + 1 : undefined); Topics.getTopicsByTids(tids, uid, next); }, ], callback); diff --git a/src/topics/top.js b/src/topics/top.js index b4f10b9340..2d86cc8eb3 100644 --- a/src/topics/top.js +++ b/src/topics/top.js @@ -33,7 +33,7 @@ module.exports = function (Topics) { }, function (tids, next) { topTopics.topicCount = tids.length; - tids = tids.slice(start, stop + 1); + tids = tids.slice(start, stop !== -1 ? stop + 1 : undefined); Topics.getTopicsByTids(tids, uid, next); }, function (topicData, next) { diff --git a/src/topics/unread.js b/src/topics/unread.js index 7c068cfc40..86d3cac56a 100644 --- a/src/topics/unread.js +++ b/src/topics/unread.js @@ -42,11 +42,7 @@ module.exports = function (Topics) { return next(null, []); } - if (params.stop === -1) { - tids = tids.slice(params.start); - } else { - tids = tids.slice(params.start, params.stop + 1); - } + tids = tids.slice(params.start, params.stop !== -1 ? params.stop + 1 : undefined); Topics.getTopicsByTids(tids, params.uid, next); }, diff --git a/test/topics.js b/test/topics.js index 4dec7a37c4..af7d22f76e 100644 --- a/test/topics.js +++ b/test/topics.js @@ -1823,4 +1823,20 @@ describe('Topic\'s', function () { ], done); }); }); + + describe('top topics', function () { + it('should get top topics in category', function (done) { + var filters = ['', 'watched', 'unreplied', 'new']; + async.map(filters, function (filter, next) { + topics.getTopTopics(topic.categoryId, topic.userId, 0, -1, filter, next); + }, function (err, data) { + assert.ifError(err); + assert(data); + data.forEach(function (filterTopics) { + assert(Array.isArray(filterTopics.topics)); + }); + done(); + }); + }); + }); });