mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
test(util) test/common/util.js -> common/util.spec.js
This commit is contained in:
parent
ea08b92e8b
commit
19763bb84d
1 changed files with 3 additions and 5 deletions
124
common/util.spec.js
Normal file
124
common/util.spec.js
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
'use strict';
|
||||
|
||||
const test = require('supertape');
|
||||
const {reRequire} = require('mock-require');
|
||||
const Util = require('./util');
|
||||
const {
|
||||
findObjByNameInArr,
|
||||
getRegExp,
|
||||
escapeRegExp,
|
||||
} = Util;
|
||||
|
||||
test('getExt: no extension', (t) => {
|
||||
const EXT = '';
|
||||
const name = 'file-withot-extension';
|
||||
const ext = Util.getExt(name);
|
||||
|
||||
t.equal(ext, EXT, 'should return "" when extension is none');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('getExt: return extension', (t) => {
|
||||
const EXT = '.png';
|
||||
const name = 'picture.png';
|
||||
const ext = Util.getExt(name);
|
||||
|
||||
t.equal(ext, EXT, 'should return ".png" in files "picture.png"');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('util: getExt: no name', (t) => {
|
||||
const ext = Util.getExt();
|
||||
|
||||
t.equal(ext, '', 'should return empty string');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('util: findObjByNameInArr: no array', (t) => {
|
||||
t.throws(findObjByNameInArr, /array should be array!/, 'should throw when no array');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('util: findObjByNameInArr: no name', (t) => {
|
||||
const fn = () => findObjByNameInArr([]);
|
||||
t.throws(fn, /name should be string!/, 'should throw when no name');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('util: findObjByNameInArr: object', (t) => {
|
||||
const name = 'hello';
|
||||
const obj = {
|
||||
name,
|
||||
};
|
||||
|
||||
const array = [
|
||||
obj,
|
||||
];
|
||||
|
||||
const result = findObjByNameInArr(array, name);
|
||||
|
||||
t.equal(result, obj, 'should return obj');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('util: findObjByNameInArr: array', (t) => {
|
||||
const name = 'hello';
|
||||
const data = 'abc';
|
||||
const item = {
|
||||
name,
|
||||
data,
|
||||
};
|
||||
|
||||
const obj = {
|
||||
name: 'world',
|
||||
};
|
||||
|
||||
const array = [
|
||||
name, [
|
||||
obj,
|
||||
item,
|
||||
],
|
||||
];
|
||||
|
||||
const result = findObjByNameInArr(array, name);
|
||||
|
||||
t.equal(result, data, 'should return data');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('util: getRegExp', (t) => {
|
||||
const reg = getRegExp('hel?o.*');
|
||||
|
||||
t.deepEqual(reg, RegExp('^hel.?o\\..*$'), 'should return regexp');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('util: getRegExp: no', (t) => {
|
||||
const reg = getRegExp('');
|
||||
|
||||
t.deepEqual(reg, RegExp('^.*$'), 'should return regexp');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('util: escapeRegExp: no str', (t) => {
|
||||
t.equal(escapeRegExp(1), 1, 'should equal');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('util: escapeRegExp', (t) => {
|
||||
t.equal(escapeRegExp('#hello'), '\\#hello', 'should equal');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('util: scope', (t) => {
|
||||
global.window = {};
|
||||
|
||||
reRequire('./util');
|
||||
|
||||
t.pass('should set window in scope');
|
||||
|
||||
delete global.window;
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue