mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-27 00:51:16 +01:00
test: missing utils tests
This commit is contained in:
@@ -9,7 +9,7 @@ const utils = { ...require('./utils.common') };
|
|||||||
utils.getLanguage = function () {
|
utils.getLanguage = function () {
|
||||||
let lang = 'en-GB';
|
let lang = 'en-GB';
|
||||||
if (typeof window === 'object' && window.config && window.utils) {
|
if (typeof window === 'object' && window.config && window.utils) {
|
||||||
lang = utils.params().lang || config.userLang || config.defaultLang || 'en-GB';
|
lang = window.utils.params().lang || window.config.userLang || window.config.defaultLang || 'en-GB';
|
||||||
}
|
}
|
||||||
return lang;
|
return lang;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
const validator = require('validator');
|
||||||
const { JSDOM } = require('jsdom');
|
const { JSDOM } = require('jsdom');
|
||||||
const slugify = require('../src/slugify');
|
const slugify = require('../src/slugify');
|
||||||
const db = require('./mocks/databasemock');
|
const db = require('./mocks/databasemock');
|
||||||
@@ -34,6 +35,7 @@ describe('Utility Methods', () => {
|
|||||||
);
|
);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should strip HTML tags', (done) => {
|
it('should strip HTML tags', (done) => {
|
||||||
assert.strictEqual(utils.stripHTMLTags('<p>just <b>some</b> text</p>'), 'just some text');
|
assert.strictEqual(utils.stripHTMLTags('<p>just <b>some</b> text</p>'), 'just some text');
|
||||||
assert.strictEqual(utils.stripHTMLTags('<p>just <b>some</b> text</p>', ['p']), 'just <b>some</b> text');
|
assert.strictEqual(utils.stripHTMLTags('<p>just <b>some</b> text</p>', ['p']), 'just <b>some</b> text');
|
||||||
@@ -133,6 +135,15 @@ describe('Utility Methods', () => {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should get language key', () => {
|
||||||
|
assert.strictEqual(utils.getLanguage(), 'en-GB');
|
||||||
|
global.window.utils = {};
|
||||||
|
global.window.config = { userLang: 'tr' };
|
||||||
|
assert.strictEqual(utils.getLanguage(), 'tr');
|
||||||
|
global.window.config = { defaultLang: 'de' };
|
||||||
|
assert.strictEqual(utils.getLanguage(), 'de');
|
||||||
|
});
|
||||||
|
|
||||||
it('should return true if string has language key', (done) => {
|
it('should return true if string has language key', (done) => {
|
||||||
assert.equal(utils.hasLanguageKey('some text [[topic:title]] and [[user:reputaiton]]'), true);
|
assert.equal(utils.hasLanguageKey('some text [[topic:title]] and [[user:reputaiton]]'), true);
|
||||||
done();
|
done();
|
||||||
@@ -143,6 +154,42 @@ describe('Utility Methods', () => {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return bootstrap env', () => {
|
||||||
|
assert.strictEqual(utils.findBootstrapEnvironment(), 'xs');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should check if mobile', () => {
|
||||||
|
assert.strictEqual(utils.isMobile(), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should check password validity', () => {
|
||||||
|
global.ajaxify = {
|
||||||
|
data: {
|
||||||
|
minimumPasswordStrength: 1,
|
||||||
|
minimumPasswordLength: 6,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const zxcvbn = require('zxcvbn');
|
||||||
|
|
||||||
|
function check(pwd, expectedError) {
|
||||||
|
try {
|
||||||
|
utils.assertPasswordValidity(pwd, zxcvbn);
|
||||||
|
assert(false);
|
||||||
|
} catch (err) {
|
||||||
|
assert.strictEqual(err.message, expectedError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
check('123456', '[[user:weak_password]]');
|
||||||
|
check('', '[[user:change_password_error]]');
|
||||||
|
check('asd', '[[reset_password:password_too_short]]');
|
||||||
|
check(new Array(513).fill('a').join(''), '[[error:password-too-long]]');
|
||||||
|
utils.assertPasswordValidity('Yzsh31j!a', zxcvbn);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should generate UUID', () => {
|
||||||
|
assert(validator.isUUID(utils.generateUUID()));
|
||||||
|
});
|
||||||
|
|
||||||
it('should shallow merge two objects', (done) => {
|
it('should shallow merge two objects', (done) => {
|
||||||
const a = { foo: 1, cat1: 'ginger' };
|
const a = { foo: 1, cat1: 'ginger' };
|
||||||
const b = { baz: 2, cat2: 'phoebe' };
|
const b = { baz: 2, cat2: 'phoebe' };
|
||||||
|
|||||||
Reference in New Issue
Block a user