Skip to content

Node.js based forum software built for the modern web

test: add helper tests

Barış Soner Uşaklı95b42f830a33
diff --git a/public/src/modules/helpers.common.js b/public/src/modules/helpers.common.js
index 32cda3b7ac..ae7493aedc 100644
--- a/public/src/modules/helpers.common.js
+++ b/public/src/modules/helpers.common.js
@@ -383,7 +383,7 @@ module.exports = function (utils, Benchpress, relative_path) {
</li>`;
});
- return html;
+ return html.join('');
}
function register() {
diff --git a/test/template-helpers.js b/test/template-helpers.js
index 76346dbf13..8e95e66c8c 100644
--- a/test/template-helpers.js
+++ b/test/template-helpers.js
@@ -52,6 +52,17 @@ describe('helpers', () => {
done();
});
+ it('should return true if route is visible', (done) => {
+ const flag = helpers.displayMenuItem({
+ navigation: [{ route: '/recent' }],
+ user: {
+ privileges: {},
+ },
+ }, 0);
+ assert(flag);
+ done();
+ });
+
it('should stringify object', (done) => {
const str = helpers.stringify({ a: 'herp < derp > and & quote "' });
assert.equal(str, '{&quot;a&quot;:&quot;herp &lt; derp &gt; and &amp; quote \\&quot;&quot;}');
@@ -64,7 +75,57 @@ describe('helpers', () => {
done();
});
+ it('should build category icon', (done) => {
+ assert.strictEqual(
+ helpers.buildCategoryIcon({
+ bgColor: '#ff0000',
+ color: '#00ff00',
+ backgroundImage: '/assets/uploads/image.png',
+ imageClass: 'auto',
+ }, 16, 'rounded-circle'),
+ '<span class="icon d-inline-flex justify-content-center align-items-center align-middle rounded-circle" style="background-color: #ff0000; border-color: #ff0000!important; color: #00ff00; background-image: url(/assets/uploads/image.png); background-size: auto; width:16; height: 16; font-size: 8px;"></span>'
+ );
+ assert.strictEqual(
+ helpers.buildCategoryIcon({
+ bgColor: '#ff0000',
+ color: '#00ff00',
+ backgroundImage: '/assets/uploads/image.png',
+ imageClass: 'auto',
+ icon: 'fa-book',
+ }, 16, 'rounded-circle'),
+ '<span class="icon d-inline-flex justify-content-center align-items-center align-middle rounded-circle" style="background-color: #ff0000; border-color: #ff0000!important; color: #00ff00; background-image: url(/assets/uploads/image.png); background-size: auto; width:16; height: 16; font-size: 8px;"><i class="fa fa-fw fa-book"></i></span>'
+ );
+ done();
+ });
+
+ it('should build category label', (done) => {
+ assert.strictEqual(
+ helpers.buildCategoryLabel({
+ bgColor: '#ff0000',
+ color: '#00ff00',
+ backgroundImage: '/assets/uploads/image.png',
+ imageClass: 'auto',
+ name: 'Category 1',
+ }, 'a', ''),
+ `<a href="/category/undefined" class="badge px-1 text-truncate text-decoration-none " style="color: #00ff00;background-color: #ff0000;border-color: #ff0000!important; max-width: 70vw;">\n\t\t\t\n\t\t\tCategory 1\n\t\t</a>`
+ );
+ assert.strictEqual(
+ helpers.buildCategoryLabel({
+ bgColor: '#ff0000',
+ color: '#00ff00',
+ backgroundImage: '/assets/uploads/image.png',
+ imageClass: 'auto',
+ name: 'Category 1',
+ icon: 'fa-book',
+ }, 'span', 'rounded-1'),
+ `<span class="badge px-1 text-truncate text-decoration-none rounded-1" style="color: #00ff00;background-color: #ff0000;border-color: #ff0000!important; max-width: 70vw;">\n\t\t\t<i class="fa fa-fw fa-book"></i>\n\t\t\tCategory 1\n\t\t</span>`,
+ );
+ done();
+ });
+
it('should return empty string if category is falsy', (done) => {
+ assert.equal(helpers.buildCategoryIcon(null), '');
+ assert.equal(helpers.buildCategoryLabel(null), '');
assert.equal(helpers.generateCategoryBackground(null), '');
done();
});
@@ -251,4 +312,34 @@ describe('helpers', () => {
assert.equal(html, '<i class="fa fa-fw fa-question-circle"></i><i class="fa fa-fw fa-question-circle"></i>');
done();
});
+
+ it('should generate replied to or wrote based on toPid', (done) => {
+ const now = Date.now();
+ const iso = new Date().toISOString();
+ let post = { pid: 2, toPid: 1, timestamp: now, timestampISO: iso, parent: { displayname: 'baris' } };
+ let str = helpers.generateWroteReplied(post, 1);
+ assert.strictEqual(str, `[[topic:replied-to-user-ago, 1, /post/1, baris, /post/2, ${iso}]]`);
+
+ post = { pid: 2, toPid: 1, timestamp: now, timestampISO: iso, parent: { displayname: 'baris' } };
+ str = helpers.generateWroteReplied(post, -1);
+ assert.strictEqual(str, `[[topic:replied-to-user-on, 1, /post/1, baris, /post/2, ${iso}]]`);
+
+ post = { pid: 2, timestamp: now, timestampISO: iso, parent: { displayname: 'baris' } };
+ str = helpers.generateWroteReplied(post, 1);
+ assert.strictEqual(str, `[[topic:wrote-ago, /post/2, ${iso}]]`);
+
+ str = helpers.generateWroteReplied(post, -1);
+ assert.strictEqual(str, `[[topic:wrote-on, /post/2, ${iso}]]`);
+
+ done();
+ });
+
+ it('should generate placeholder wave', (done) => {
+ const items = [2, 'divider', 3];
+ const str = helpers.generatePlaceholderWave(items);
+ assert(str.includes('dropdown-divider'));
+ assert(str.includes('col-2'));
+ assert(str.includes('col-3'));
+ done();
+ });
});