merge
This commit is contained in:
sentsin 2017-09-03 06:48:46 +08:00
commit 58f01788b3
4 changed files with 346 additions and 4 deletions

View File

@ -72,6 +72,13 @@ module.exports = function (config) {
// list of files to exclude
exclude: [],
client: {
mocha: {
// mocha测试超时6秒
timeout: 1000 * 6
}
},
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor

View File

@ -415,7 +415,7 @@
//如果是数字,按大小排序,如果是非数字,按字典序排序
clone.sort(function(o1, o2){
var isNum = /^\d+$/
var isNum = /^-?\d+$/
,v1 = o1[key]
,v2 = o2[key];

View File

@ -4,6 +4,7 @@
*/
/* global layui */
/* eslint-disable max-nested-callbacks, fecs-indent */
var laytpl = layui.laytpl;
@ -226,3 +227,4 @@ describe('laytpl', function () {
});
});
});
/* eslint-enable max-nested-callbacks, fecs-indent */

View File

@ -4,7 +4,7 @@
*/
/* global layui */
/* eslint-disable max-nested-callbacks */
/* eslint-disable max-nested-callbacks, fecs-indent */
var $ = layui.$;
@ -123,7 +123,7 @@ describe('layui', function () {
layui.each([
'layui'
], function (index, value) {
expect(this + '').to.deep.equal(value).and.equal('layui')
expect(this + '').to.deep.equal(value).and.equal('layui');
expect(index).to.equal(0);
done();
});
@ -170,5 +170,338 @@ describe('layui', function () {
expect(flag).to.equal('version');
});
});
describe('layui.img', function () {
var base64 = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
it('success callback', function (done) {
layui.img(base64, function (img) {
expect(img).to.not.undefined;
expect(typeof(img)).to.equal('object', '是img对象');
expect(img.nodeType).to.equal(1, 'img标签节点');
// 在ie11中不通过, 原因目前不明
// expect(img.width).to.equal(1);
// expect(img.height).to.equal(1);
done();
});
});
it('error callback', function (done) {
layui.img('/404/404.gif', function () {}, function (e) {
expect(e).to.not.undefined;
done();
});
});
// 先删除, 因为没有哪个图片是决定不变的
// it('http 200', function (done) {
// layui.img('https://www.baidu.com/img/bd_logo1.png', function (img) {
// expect(img).to.not.undefined;
// done();
// });
// });
it('http 404', function (done) {
layui.img('http://www.404.xx/logo.404.gif', function () {}, function (e) {
expect(e).to.not.undefined;
done();
});
});
it('load complete', function (done) {
layui.img(base64, function () {
layui.img(base64, function (img) {
expect(img).to.not.undefined;
done();
});
});
});
});
it('layui.hint', function () {
expect(layui.hint).to.be.a('function');
expect(layui.hint()).to.be.a('object');
expect(layui.hint().error).to.be.a('function');
});
describe('layui.stope', function () {
it('stopPropagation', function (done) {
layui.stope({
stopPropagation: function (e) {
expect(e).to.be.undefined;
done();
}
});
});
it('cancelBubble', function () {
var event = {};
layui.stope(event);
expect(event.cancelBubble).to.be.true;
});
// ie中不支持
// it('window.event', function () {
// var old = window.event;
// var event = window.event = {};
// layui.stope();
// expect(event.cancelBubble).to.be.true;
// window.event = old;
// });
});
describe('layui.onevent', function () {
it('check params and return value', function () {
expect(layui.onevent).to.be.a('function');
expect(layui.onevent()).to.deep.equal(layui);
expect(layui.onevent([], [], [])).to.deep.equal(layui);
expect(layui.onevent({}, {}, {})).to.deep.equal(layui);
var result = layui.onevent('test-' + Date.now(), 'click', function () {});
expect(result).to.deep.equal(layui);
});
it('bind event', function (done) {
var id = 'test-bind-event';
var data = {
name: 'layui'
};
var result = layui.onevent(id, 'click', function (param) {
expect(result).to.deep.equal(this).and.equal(layui);
expect(param).to.deep.equal(data);
done();
});
layui.event(id, 'click', data);
});
it('coverage of the same name event', function () {
var id = 'test-same-event';
var index = 0;
layui.onevent(id, 'click', function () {
index = 1;
});
layui.onevent(id, 'click', function () {
index = 2;
});
layui.event(id, 'click');
expect(index).to.equal(2);
});
});
describe('layui.event', function () {
it('trigger event', function (done) {
layui.onevent('test-trigger', 'click(*)', function (data) {
expect(data).to.be.true;
done();
});
layui.event('test-trigger', 'click(*)', true);
});
it('trigger multiple', function () {
var index = 0;
var id = 'test-trigger-multiple';
layui.onevent(id, 'nav', function () {
index += 1;
});
layui.event(id, 'nav');
layui.event(id, 'nav');
layui.event(id, 'nav');
expect(index).to.equal(3);
});
// todo多个事件
});
describe('layui.sort', function () {
var numberData = [
{
name: 1
},
{
name: 3
},
{
name: 2
}
];
it('check params and return value', function () {
// 由于没有值参数, 导致 JSON.parse 失败
expect(function () {
layui.sort();
}).to.throw();
expect(layui.sort({})).to.deep.equal({});
expect(layui.sort({
name: 'layui'
})).to.deep.equal({
name: 'layui'
});
expect(layui.sort([{
name: 'layui'
}], 'name')).to.deep.equal([{
name: 'layui'
}]);
expect(layui.sort([{
name: 'layui'
}], 'name', true)).to.deep.equal([{
name: 'layui'
}]);
});
// 测试是否污染原数据
it('clone object', function () {
var clone = layui.sort(numberData, 'name');
// 往clone对象添加
clone.push('layui');
expect(clone).to.have.lengthOf(4);
expect(numberData).to.have.lengthOf(3);
});
it('format value number', function () {
var result = layui.sort([
{
key: '1'
},
{
key: '-1'
},
{
key: 2
},
{
key: 3
}
], 'key');
expect(result).to.deep.equal([
{
key: '-1'
},
{
key: '1'
},
{
key: 2
},
{
key: 3
}
]);
});
it('asc order', function () {
var result = layui.sort(numberData, 'name');
expect(result).to.deep.equal([
{
name: 1
},
{
name: 2
},
{
name: 3
}
]);
});
it('desc order', function () {
var result = layui.sort(numberData, 'name', true);
expect(result).to.deep.equal([
{
name: 3
},
{
name: 2
},
{
name: 1
}
]);
});
it('error data', function () {
var data = [
// null,
{
name: 5
},
{},
[],
'test',
{
name: '3'
}
];
expect(layui.sort(data, 'name')).to.deep.equal([
{},
[],
'test',
{
name: '3'
},
{
name: 5
}
]);
});
});
it('layui.device', function () {
expect(layui.device).to.be.a('function');
expect(layui.device()).to.be.a('object');
expect(layui.device().ie).to.be.not.undefined;
expect(layui.device().ios).to.be.not.undefined;
expect(layui.device().android).to.be.not.undefined;
expect(layui.device().weixin).to.be.a('boolean');
expect(layui.device('weixin').weixin).to.be.false;
expect(layui.device('.*')['.*']).to.be.not.empty;
expect(layui.device('layui.com')['layui.com']).to.be.false;
});
describe('layui.getStyle', function () {
it('real test', function () {
var elem = $('<div />').css({
position: 'fixed',
zIndex: 10
}).appendTo('body').get(0);
expect(layui.getStyle(elem, 'position')).to.equal('fixed');
expect(layui.getStyle(elem, 'z-index')).to.equal('10');
});
it('mock currentStyle', function (done) {
var node = {
currentStyle: {
getPropertyValue: function (name) {
expect(name).to.equal('layui');
done();
}
}
};
layui.getStyle(node, 'layui');
});
});
it('layui.extend', function () {
expect(layui.extend).to.be.a('function');
expect(layui.extend()).to.deep.equal(layui);
expect(layui.extend({
v: 'v',
util: 'util'
})).to.deep.equal(layui);
var id = 'test-extend-' + Date.now();
var data = {};
data[id] = id;
expect(layui.modules[id]).to.be.undefined;
layui.extend(data);
expect(layui.modules[id]).to.be.not.undefined;
expect(layui.modules[id]).to.equal(id);
delete layui.modules[id];
});
});
/* eslint-enable max-nested-callbacks */
/* eslint-enable max-nested-callbacks, fecs-indent */