Skip to content

Node.js based forum software built for the modern web

test: add missing tests

Barış Soner Uşaklı404a87740882
diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js
index 6b91bba943..bf0dc70322 100644
--- a/src/controllers/helpers.js
+++ b/src/controllers/helpers.js
@@ -242,7 +242,7 @@ helpers.buildTitle = function (pageTitle) {
helpers.getCategories = async function (set, uid, privilege, selectedCid) {
const cids = await categories.getCidsByPrivilege(set, uid, privilege);
- return await getCategoryData(cids, uid, selectedCid, privilege);
+ return await getCategoryData(cids, uid, selectedCid, Object.values(categories.watchStates), privilege);
};
helpers.getCategoriesByStates = async function (uid, selectedCid, states, privilege = 'topics:read') {
@@ -251,44 +251,23 @@ helpers.getCategoriesByStates = async function (uid, selectedCid, states, privil
};
async function getCategoryData(cids, uid, selectedCid, states, privilege) {
- if (selectedCid && !Array.isArray(selectedCid)) {
- selectedCid = [selectedCid];
- }
- selectedCid = selectedCid && selectedCid.map(String);
-
- const visibleCategories = await helpers.getVisibleCategories({
- cids, uid, states, privilege, showLinks: false,
- });
+ const [visibleCategories, selectData] = await Promise.all([
+ helpers.getVisibleCategories({
+ cids, uid, states, privilege, showLinks: false,
+ }),
+ helpers.getSelectedCategory(selectedCid),
+ ]);
const categoriesData = categories.buildForSelectCategories(visibleCategories, ['disabledClass']);
- let selectedCategory = [];
- const selectedCids = [];
categoriesData.forEach((category) => {
- category.selected = selectedCid ? selectedCid.includes(String(category.cid)) : false;
- if (category.selected) {
- selectedCategory.push(category);
- selectedCids.push(category.cid);
- }
+ category.selected = selectData.selectedCids.includes(category.cid);
});
- selectedCids.sort((a, b) => a - b);
-
- if (selectedCategory.length > 1) {
- selectedCategory = {
- icon: 'fa-plus',
- name: '[[unread:multiple-categories-selected]]',
- bgColor: '#ddd',
- };
- } else if (selectedCategory.length === 1) {
- selectedCategory = selectedCategory[0];
- } else {
- selectedCategory = null;
- }
-
+ selectData.selectedCids.sort((a, b) => a - b);
return {
categories: categoriesData,
- selectedCategory: selectedCategory,
- selectedCids: selectedCids,
+ selectedCategory: selectData.selectedCategory,
+ selectedCids: selectData.selectedCids,
};
}
@@ -347,26 +326,26 @@ helpers.getVisibleCategories = async function (params) {
});
};
-helpers.getSelectedCategory = async function (cid) {
- if (cid && !Array.isArray(cid)) {
- cid = [cid];
+helpers.getSelectedCategory = async function (cids) {
+ if (cids && !Array.isArray(cids)) {
+ cids = [cids];
}
- cid = cid && cid.map(cid => parseInt(cid, 10));
- let selectedCategories = await categories.getCategoriesData(cid);
-
+ cids = cids && cids.map(cid => parseInt(cid, 10));
+ let selectedCategories = await categories.getCategoriesData(cids);
+ const selectedCids = selectedCategories.map(c => c && c.cid).filter(Boolean);
if (selectedCategories.length > 1) {
selectedCategories = {
icon: 'fa-plus',
name: '[[unread:multiple-categories-selected]]',
bgColor: '#ddd',
};
- } else if (selectedCategories.length === 1) {
+ } else if (selectedCategories.length === 1 && selectedCategories[0]) {
selectedCategories = selectedCategories[0];
} else {
selectedCategories = null;
}
return {
- selectedCids: cid || [],
+ selectedCids: selectedCids,
selectedCategory: selectedCategories,
};
};
diff --git a/test/controllers.js b/test/controllers.js
index 077ef196b5..cdb6828c97 100644
--- a/test/controllers.js
+++ b/test/controllers.js
@@ -2218,6 +2218,29 @@ describe('Controllers', () => {
},
], done);
});
+
+ it('should load categories', async () => {
+ const helpers = require('../src/controllers/helpers');
+ const data = await helpers.getCategories('cid:0:children', 1, 'topics:read', 0);
+ assert(data.categories.length > 0);
+ assert.strictEqual(data.selectedCategory, null);
+ assert.deepStrictEqual(data.selectedCids, []);
+ });
+
+ it('should load categories by states', async () => {
+ const helpers = require('../src/controllers/helpers');
+ const data = await helpers.getCategoriesByStates(1, 1, Object.values(categories.watchStates), 'topics:read');
+ assert.deepStrictEqual(data.selectedCategory.cid, 1);
+ assert.deepStrictEqual(data.selectedCids, [1]);
+ });
+
+ it('should load categories by states', async () => {
+ const helpers = require('../src/controllers/helpers');
+ const data = await helpers.getCategoriesByStates(1, 0, [categories.watchStates.ignoring], 'topics:read');
+ assert(data.categories.length === 0);
+ assert.deepStrictEqual(data.selectedCategory, null);
+ assert.deepStrictEqual(data.selectedCids, []);
+ });
});
describe('unread', () => {