showdown/test/unit/showdown.helpers.js

264 lines
7.2 KiB
JavaScript

/**
* Created by Tivie on 27/01/2017.
*/
chai.should();
/*jshint expr: true*/
/*jshint -W053 */
/*jshint -W010 */
/*jshint -W009 */
describe('encodeEmailAddress()', function () {
'use strict';
let encoder = showdown.helper.encodeEmailAddress,
email = 'foobar@example.com',
encodedEmail = encoder(email),
encodedEmail2 = encoder(email);
it('should encode email', function () {
encodedEmail.should.not.equal(email);
});
it('should encode email determinated', function () {
encodedEmail.should.equal(encodedEmail2);
});
it('should decode to original email', function () {
let decodedEmail = encodedEmail.replace(/&#(.+?);/g, function (wm, cc) {
if (cc.charAt(0) === 'x') {
//hex
return String.fromCharCode('0' + cc);
} else {
//dec
return String.fromCharCode(cc);
}
});
decodedEmail.should.equal(email);
});
});
describe('isString()', function () {
'use strict';
let isString = showdown.helper.isString;
it('should return true for new String Object', function () {
isString(new String('some string')).should.be.true;
});
it('should return true for String Object', function () {
isString(String('some string')).should.be.true;
});
it('should return true for string literal', function () {
isString('some string').should.be.true;
});
it('should return false for integers', function () {
isString(5).should.be.false;
});
it('should return false for random objects', function () {
isString({foo: 'bar'}).should.be.false;
});
it('should return false for arrays', function () {
isString(['bar']).should.be.false;
});
});
describe('isFunction()', function () {
'use strict';
let isFunction = showdown.helper.isFunction;
it('should return true for closures', function () {
isFunction(function () {}).should.be.true;
});
it('should return true for defined functions', function () {
function foo () {}
isFunction(foo).should.be.true;
});
it('should return true for function letiables', function () {
let bar = function () {};
isFunction(bar).should.be.true;
});
it('should return false for hash objects', function () {
isFunction({}).should.be.false;
});
it('should return false for objects', function () {
isFunction(new Object ()).should.be.false;
});
it('should return false for string primitives', function () {
isFunction('foo').should.be.false;
});
});
describe('isArray()', function () {
'use strict';
let isArray = showdown.helper.isArray;
it('should return true for short syntax arrays', function () {
isArray([]).should.be.true;
});
it('should return true for array objects', function () {
let myArr = new Array();
isArray(myArr).should.be.true;
});
it('should return false for functions', function () {
isArray(function () {}).should.be.false;
function baz () {}
isArray(baz).should.be.false;
});
it('should return false for objects', function () {
isArray({}).should.be.false;
isArray(new Object ()).should.be.false;
});
it('should return false for strings', function () {
isArray('foo').should.be.false;
isArray(new String('foo')).should.be.false;
});
});
describe('isUndefined()', function () {
'use strict';
let isUndefined = showdown.helper.isUndefined;
it('should return true if nothing is passed', function () {
isUndefined().should.be.true;
});
it('should return true if a letiable is initialized but not defined', function () {
let myVar;
isUndefined(myVar).should.be.true;
});
it('should return false for null', function () {
isUndefined(null).should.be.false;
});
it('should return false for 0', function () {
isUndefined(0).should.be.false;
});
it('should return false for empty string', function () {
isUndefined('').should.be.false;
});
it('should return false for empty booleans false or true', function () {
isUndefined(false).should.be.false;
isUndefined(true).should.be.false;
});
it('should return false for anything not undefined', function () {
isUndefined('foo').should.be.false;
isUndefined(2).should.be.false;
isUndefined({}).should.be.false;
});
});
describe('stdExtName()', function () {
'use strict';
let stdExtName = showdown.helper.stdExtName;
it('should remove certain chars', function () {
let str = 'bla_- \nbla';
//[_?*+\/\\.^-]
stdExtName(str).should.not.match(/[_?*+\/\\.^-]/g);
});
it('should make everything lowercase', function () {
let str = 'BLABLA';
//[_?*+\/\\.^-]
stdExtName(str).should.equal('blabla');
});
});
describe('forEach()', function () {
'use strict';
let forEach = showdown.helper.forEach;
it('should throw an error if first parameter is undefined', function () {
(function () {forEach();}).should.throw('obj param is required');
});
it('should throw an error if second parameter is undefined', function () {
(function () {forEach([]);}).should.throw('callback param is required');
});
it('should throw an error if second parameter is not a function', function () {
(function () {forEach([], 'foo');}).should.throw('callback param must be a function/closure');
});
it('should throw an error if first parameter is not an object or an array', function () {
(function () {forEach('foo', function () {});}).should.throw('obj does not seem to be an array or an iterable object');
});
it('should not throw even if object is empty', function () {
(function () {forEach({}, function () {});}).should.not.throw();
});
it('should iterate array items', function () {
let myArray = ['banana', 'orange', 'grape'];
forEach(myArray, function (val, key, obj) {
key.should.be.a('number');
(key % 1).should.equal(0);
val.should.equal(myArray[key]);
obj.should.equal(myArray);
});
});
it('should iterate over object properties', function () {
let myObj = {foo: 'banana', bar: 'orange', baz: 'grape'};
forEach(myObj, function (val, key, obj) {
myObj.should.have.ownProperty(key);
val.should.equal(myObj[key]);
obj.should.equal(myObj);
});
});
it('should iterate only over object own properties', function () {
let Obj1 = {foo: 'banana'},
myObj = Object.create(Obj1);
myObj.bar = 'orange';
myObj.baz = 'grape';
myObj.should.have.ownProperty('bar');
myObj.should.have.ownProperty('baz');
myObj.should.not.have.ownProperty('foo');
forEach(myObj, function (val, key) {
key.should.not.equal('foo');
});
});
});
describe('matchRecursiveRegExp()', function () {
'use strict';
let rRegExp = showdown.helper.matchRecursiveRegExp;
it('should match nested elements', function () {
let result = rRegExp('<div><div>a</div></div>', '<div\\b[^>]*>', '</div>', 'gim');
result.should.deep.equal([['<div><div>a</div></div>', '<div>a</div>', '<div>', '</div>']]);
});
});
describe('repeat()', function () {
'use strict';
it('work produce the same output as String.prototype.repeat()', function () {
if (typeof String.prototype.repeat !== 'undefined') {
let str = 'foo',
expected = str.repeat(100),
actual = showdown.helper.repeat(str, 100);
expected.should.equal(actual);
}
});
});