mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-01 13:20:41 +01:00
fix: handle start=0 stop=0 for topics.getTopicPosts
add more tests
This commit is contained in:
@@ -261,7 +261,7 @@ define('forum/topic/posts', [
|
||||
|
||||
infinitescroll.loadMore('topics.loadMore', {
|
||||
tid: tid,
|
||||
after: after,
|
||||
after: after + (direction > 0 ? 1 : 0),
|
||||
count: config.postsPerPage,
|
||||
direction: direction,
|
||||
topicPostSort: config.topicPostSort,
|
||||
|
||||
@@ -30,9 +30,7 @@ module.exports = function (SocketTopics) {
|
||||
parseInt(data.count, 10) || meta.config.postsPerPage || 20
|
||||
));
|
||||
|
||||
if (data.direction === 1) {
|
||||
start += 1;
|
||||
} else if (data.direction === -1) {
|
||||
if (data.direction === -1) {
|
||||
start -= infScrollPostsPerPage;
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,10 @@ module.exports = function (Topics) {
|
||||
repliesStart -= 1;
|
||||
}
|
||||
}
|
||||
const pids = await posts.getPidsFromSet(set, repliesStart, repliesStop, reverse);
|
||||
let pids = [];
|
||||
if (start !== 0 || stop !== 0) {
|
||||
pids = await posts.getPidsFromSet(set, repliesStart, repliesStop, reverse);
|
||||
}
|
||||
if (!pids.length && !topicOrTid.mainPid) {
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -507,6 +507,42 @@ describe('Topic\'s', () => {
|
||||
const posts = await topics.getTopicPosts(null, `tid:${tid}:posts`, 0, 9, topic.userId, true);
|
||||
assert.deepStrictEqual(posts, []);
|
||||
});
|
||||
|
||||
it('should only return main post', async () => {
|
||||
const topicData = await topics.getTopicData(tid);
|
||||
const postsData = await topics.getTopicPosts(topicData, `tid:${tid}:posts`, 0, 0, topic.userId, false);
|
||||
assert.strictEqual(postsData.length, 1);
|
||||
assert.strictEqual(postsData[0].content, 'main post');
|
||||
});
|
||||
|
||||
it('should only return first reply', async () => {
|
||||
const topicData = await topics.getTopicData(tid);
|
||||
const postsData = await topics.getTopicPosts(topicData, `tid:${tid}:posts`, 1, 1, topic.userId, false);
|
||||
assert.strictEqual(postsData.length, 1);
|
||||
assert.strictEqual(postsData[0].content, 'topic reply 1');
|
||||
});
|
||||
|
||||
it('should return main post and first reply', async () => {
|
||||
const topicData = await topics.getTopicData(tid);
|
||||
const postsData = await topics.getTopicPosts(topicData, `tid:${tid}:posts`, 0, 1, topic.userId, false);
|
||||
assert.strictEqual(postsData.length, 2);
|
||||
assert.strictEqual(postsData[0].content, 'main post');
|
||||
assert.strictEqual(postsData[1].content, 'topic reply 1');
|
||||
});
|
||||
|
||||
it('should return posts in correct order', async () => {
|
||||
const data = await socketTopics.loadMore({ uid: topic.userId }, { tid: tid, after: 20, direction: 1 });
|
||||
assert.strictEqual(data.posts.length, 11);
|
||||
assert.strictEqual(data.posts[0].content, 'topic reply 20');
|
||||
assert.strictEqual(data.posts[1].content, 'topic reply 21');
|
||||
});
|
||||
|
||||
it('should return posts in correct order in reverse direction', async () => {
|
||||
const data = await socketTopics.loadMore({ uid: topic.userId }, { tid: tid, after: 25, direction: -1 });
|
||||
assert.strictEqual(data.posts.length, 20);
|
||||
assert.strictEqual(data.posts[0].content, 'topic reply 5');
|
||||
assert.strictEqual(data.posts[1].content, 'topic reply 6');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user