Merge "Ensure all drafts are moved after comments during sorting"

This commit is contained in:
Dhruv Srivastava
2020-02-04 13:13:40 +00:00
committed by Gerrit Code Review
2 changed files with 39 additions and 6 deletions

View File

@@ -640,17 +640,20 @@
}
}
_sortComments(comments) {
return comments.slice(0).sort((a, b) => {
if (b.__draft && !a.__draft ) { return -1; }
if (a.__draft && !b.__draft ) { return 1; }
return util.parseDate(a.updated) - util.parseDate(b.updated);
});
}
/**
* @param {!Array<!Object>} comments
* @return {!Array<!Object>} Threads for the given comments.
*/
_createThreads(comments) {
const sortedComments = comments.slice(0).sort((a, b) => {
if (b.__draft && !a.__draft ) { return 0; }
if (a.__draft && !b.__draft ) { return 1; }
return util.parseDate(a.updated) - util.parseDate(b.updated);
});
const sortedComments = this._sortComments(comments);
const threads = [];
for (const comment of sortedComments) {
// If the comment is in reply to another comment, find that comment's

View File

@@ -1102,6 +1102,36 @@ limitations under the License.
});
});
test('comments sorting', () => {
const comments = [
{
id: 'new_draft',
message: 'i do not like either of you',
__commentSide: 'left',
__draft: true,
updated: '2015-12-20 15:01:20.396000000',
},
{
id: 'sallys_confession',
message: 'i like you, jack',
updated: '2015-12-23 15:00:20.396000000',
line: 1,
__commentSide: 'left',
}, {
id: 'jacks_reply',
message: 'i like you, too',
updated: '2015-12-24 15:01:20.396000000',
__commentSide: 'left',
line: 1,
in_reply_to: 'sallys_confession',
},
];
const sortedComments = element._sortComments(comments);
assert.equal(sortedComments[0], comments[1]);
assert.equal(sortedComments[1], comments[2]);
assert.equal(sortedComments[2], comments[0]);
});
test('_createThreads', () => {
const comments = [
{