From e047b72c87f5fe61163dd016a176556c05085866 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 19 Aug 2020 09:04:24 -0400 Subject: [PATCH] feat: additional tests for #8569 --- test/flags.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/test/flags.js b/test/flags.js index e975933c6308..b0f6fcac1fd9 100644 --- a/test/flags.js +++ b/test/flags.js @@ -286,6 +286,66 @@ describe('Flags', function () { }); }); }); + + describe('(with sort)', () => { + before(async () => { + // Create a second flag to test sorting + const post = await Topics.reply({ + tid: 1, + uid: uid1, + content: 'this is a reply -- flag me', + }); + await Flags.create('post', post.pid, adminUid, 'another flag'); + await Flags.create('post', 1, uid3, 'additional flag report'); + }); + + it('should return sorted flags latest first if no sort is passed in', async () => { + const payload = await Flags.list({ + uid: adminUid, + }); + + assert(payload.flags.every((cur, idx) => { + if (idx === payload.flags.length - 1) { + return true; + } + + const next = payload.flags[idx + 1]; + return parseInt(cur.datetime, 10) > parseInt(next.datetime, 10); + })); + }); + + it('should return sorted flags oldest first if "oldest" sort is passed in', async () => { + const payload = await Flags.list({ + uid: adminUid, + sort: 'oldest', + }); + + assert(payload.flags.every((cur, idx) => { + if (idx === payload.flags.length - 1) { + return true; + } + + const next = payload.flags[idx + 1]; + return parseInt(cur.datetime, 10) < parseInt(next.datetime, 10); + })); + }); + + it('should return flags with more reports first if "reports" sort is passed in', async () => { + const payload = await Flags.list({ + uid: adminUid, + sort: 'reports', + }); + + assert(payload.flags.every((cur, idx) => { + if (idx === payload.flags.length - 1) { + return true; + } + + const next = payload.flags[idx + 1]; + return parseInt(cur.heat, 10) >= parseInt(next.heat, 10); + })); + }); + }); }); describe('.update()', function () {