test: add missing tests

isekai-main
Barış Soner Uşaklı 3 years ago
parent 2bdb4906ec
commit 404a877408

@ -242,7 +242,7 @@ helpers.buildTitle = function (pageTitle) {
helpers.getCategories = async function (set, uid, privilege, selectedCid) { helpers.getCategories = async function (set, uid, privilege, selectedCid) {
const cids = await categories.getCidsByPrivilege(set, uid, privilege); 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') { 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) { async function getCategoryData(cids, uid, selectedCid, states, privilege) {
if (selectedCid && !Array.isArray(selectedCid)) { const [visibleCategories, selectData] = await Promise.all([
selectedCid = [selectedCid]; helpers.getVisibleCategories({
} cids, uid, states, privilege, showLinks: false,
selectedCid = selectedCid && selectedCid.map(String); }),
helpers.getSelectedCategory(selectedCid),
const visibleCategories = await helpers.getVisibleCategories({ ]);
cids, uid, states, privilege, showLinks: false,
});
const categoriesData = categories.buildForSelectCategories(visibleCategories, ['disabledClass']); const categoriesData = categories.buildForSelectCategories(visibleCategories, ['disabledClass']);
let selectedCategory = [];
const selectedCids = [];
categoriesData.forEach((category) => { categoriesData.forEach((category) => {
category.selected = selectedCid ? selectedCid.includes(String(category.cid)) : false; category.selected = selectData.selectedCids.includes(category.cid);
if (category.selected) {
selectedCategory.push(category);
selectedCids.push(category.cid);
}
}); });
selectedCids.sort((a, b) => a - b); selectData.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;
}
return { return {
categories: categoriesData, categories: categoriesData,
selectedCategory: selectedCategory, selectedCategory: selectData.selectedCategory,
selectedCids: selectedCids, selectedCids: selectData.selectedCids,
}; };
} }
@ -347,26 +326,26 @@ helpers.getVisibleCategories = async function (params) {
}); });
}; };
helpers.getSelectedCategory = async function (cid) { helpers.getSelectedCategory = async function (cids) {
if (cid && !Array.isArray(cid)) { if (cids && !Array.isArray(cids)) {
cid = [cid]; cids = [cids];
} }
cid = cid && cid.map(cid => parseInt(cid, 10)); cids = cids && cids.map(cid => parseInt(cid, 10));
let selectedCategories = await categories.getCategoriesData(cid); let selectedCategories = await categories.getCategoriesData(cids);
const selectedCids = selectedCategories.map(c => c && c.cid).filter(Boolean);
if (selectedCategories.length > 1) { if (selectedCategories.length > 1) {
selectedCategories = { selectedCategories = {
icon: 'fa-plus', icon: 'fa-plus',
name: '[[unread:multiple-categories-selected]]', name: '[[unread:multiple-categories-selected]]',
bgColor: '#ddd', bgColor: '#ddd',
}; };
} else if (selectedCategories.length === 1) { } else if (selectedCategories.length === 1 && selectedCategories[0]) {
selectedCategories = selectedCategories[0]; selectedCategories = selectedCategories[0];
} else { } else {
selectedCategories = null; selectedCategories = null;
} }
return { return {
selectedCids: cid || [], selectedCids: selectedCids,
selectedCategory: selectedCategories, selectedCategory: selectedCategories,
}; };
}; };

@ -2218,6 +2218,29 @@ describe('Controllers', () => {
}, },
], done); ], 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', () => { describe('unread', () => {

Loading…
Cancel
Save