js-web-tools
Version:
Tools for Javascript develpers
80 lines (78 loc) • 3.11 kB
JavaScript
;
var _utils = require("../utils");
describe('routeUtils', function () {
it('should get null is search is undefined or empty string', function () {
expect((0, _utils.parseQuery)()).toEqual(null);
expect((0, _utils.parseQuery)('')).toEqual(null);
});
it('should get query object', function () {
expect((0, _utils.parseQuery)('http://www.mydomain.com/subPath?a=av&b=bv&c=123')).toMatchObject({
a: 'av',
b: 'bv',
c: '123'
});
});
it('should not get query object exclude property', function () {
var query = (0, _utils.parseQuery)('http://www.mydomain.com/subPath?a=&b=bv&c');
expect(query.c).toEqual(undefined);
});
});
describe('common utils', function () {
it('test isObject', function () {
expect((0, _utils.isObject)(function () {})).toEqual(true);
expect((0, _utils.isObject)(null)).toEqual(false);
expect((0, _utils.isObject)('str')).toEqual(false);
expect((0, _utils.isObject)([])).toEqual(true);
expect((0, _utils.isObject)(/^abc*/ig)).toEqual(true);
expect((0, _utils.isObject)(false)).toEqual(false);
});
});
describe('Stirng Utils', function () {
describe('isString', function () {
it('should get true if source is string type', function () {
expect((0, _utils.isString)('hello')).toEqual(true);
expect((0, _utils.isString)(new String('hello'))).toEqual(true);
});
it('should get false if source is not string type', function () {
expect((0, _utils.isString)('hello')).toEqual(true);
expect((0, _utils.isString)(new String('hello'))).toEqual(true);
});
});
describe('trim string blanks', function () {
it('should trim string blanks', function () {
expect((0, _utils.trim)(' hello ')).toEqual('hello');
});
it('should trim string blanks', function () {
var str = new String(' hello ');
expect((0, _utils.trim)(str)).toEqual('hello');
});
it('should get source when no blanks to trim', function () {
expect((0, _utils.trim)('hello')).toEqual('hello');
});
it('should get source when source type is not string type', function () {
expect((0, _utils.trim)(123)).toEqual(123);
expect((0, _utils.trim)(true)).toEqual(true);
expect((0, _utils.trim)(false)).toEqual(false);
expect((0, _utils.trim)(['hello'])).toMatchObject(['hello']);
});
it('should use Regexp to trim blanks', function () {
var str = new String(' blanks ');
Object.defineProperty(str, 'trim', {
value: null
});
expect((0, _utils.trim)(str)).toEqual('blanks');
});
});
describe('toNumber', function () {
it('should get number', function () {
expect((0, _utils.toNumber)('12px')).toEqual(12);
expect((0, _utils.toNumber)(24)).toEqual(24);
});
it('should get number 0 when source type is not string or number', function () {
expect((0, _utils.toNumber)(true)).toEqual(0);
expect((0, _utils.toNumber)({})).toEqual(0);
expect((0, _utils.toNumber)([])).toEqual(0);
expect((0, _utils.toNumber)('abc90')).toEqual(0);
});
});
});