From 61d32bdebb9a92f5984354b83b88d7bf3a0f6bfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Mon, 3 Oct 2022 09:40:54 -0400 Subject: [PATCH] fix: category ordering add test --- public/src/admin/manage/categories.js | 2 +- src/categories/update.js | 2 +- test/categories.js | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/public/src/admin/manage/categories.js b/public/src/admin/manage/categories.js index 0c541a1438..adb212905e 100644 --- a/public/src/admin/manage/categories.js +++ b/public/src/admin/manage/categories.js @@ -202,7 +202,7 @@ define('admin/manage/categories', [ // this makes sure order is correct when drag & drop is used on pages > 1 const baseIndex = (ajaxify.data.pagination.currentPage - 1) * ajaxify.data.categoriesPerPage; modified[cid] = { - order: baseIndex + e.newIndex, + order: baseIndex + e.newIndex + 1, }; if (isCategoryUpdate) { diff --git a/src/categories/update.js b/src/categories/update.js index 20b14361fb..d4be83edb8 100644 --- a/src/categories/update.js +++ b/src/categories/update.js @@ -108,7 +108,7 @@ module.exports = function (Categories) { if (currentIndex === -1) { throw new Error('[[error:no-category]]'); } - // moves cid to index order-1 in the array + // moves cid to index order - 1 in the array if (childrenCids.length > 1) { childrenCids.splice(Math.max(0, order - 1), 0, childrenCids.splice(currentIndex, 1)[0]); } diff --git a/test/categories.js b/test/categories.js index 98d15c28bc..cd28ee33ef 100644 --- a/test/categories.js +++ b/test/categories.js @@ -421,6 +421,22 @@ describe('Categories', () => { assert.equal(data.icon, updateData[cid].icon); }); + it('should properly order categories', async () => { + const p1 = await Categories.create({ name: 'p1', description: 'd', parentCid: 0, order: 1 }); + const c1 = await Categories.create({ name: 'c1', description: 'd1', parentCid: p1.cid, order: 1 }); + const c2 = await Categories.create({ name: 'c2', description: 'd2', parentCid: p1.cid, order: 2 }); + const c3 = await Categories.create({ name: 'c3', description: 'd3', parentCid: p1.cid, order: 3 }); + // move c1 to second place + await apiCategories.update({ uid: adminUid }, { [c1.cid]: { order: 2 } }); + let cids = await db.getSortedSetRange(`cid:${p1.cid}:children`, 0, -1); + assert.deepStrictEqual(cids.map(Number), [c2.cid, c1.cid, c3.cid]); + + // move c3 to front + await apiCategories.update({ uid: adminUid }, { [c3.cid]: { order: 1 } }); + cids = await db.getSortedSetRange(`cid:${p1.cid}:children`, 0, -1); + assert.deepStrictEqual(cids.map(Number), [c3.cid, c2.cid, c1.cid]); + }); + it('should not remove category from parent if parent is set again to same category', async () => { const parentCat = await Categories.create({ name: 'parent', description: 'poor parent' }); const updateData = {};