merge
pull/66/head v2.1.5
sentsin 2017-09-20 00:25:15 +08:00
commit a9f85a840b
7 changed files with 1672 additions and 151 deletions

View File

@ -12,7 +12,6 @@ cache:
directories:
- node_modules
script:
- npm run test:cov
- npm run test:sauce
- npm run test:cov && npm run test:sauce
after_script:
- npm install coveralls && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage

View File

@ -3,6 +3,36 @@
* @author fe.xiaowu@gmail.com
*/
var url = require('url');
/**
* mock一个server供测试使用
*
* @param {Object} req request
* @param {Object} res response
* @param {Function} next 下一路由
*
* @example
* 请求 /api/mock 参数如:
* timeout - 超时时间, 默认 0
* statusCode - 状态码, 默认 200
* response - 响应内容, 默认 {}
* dataType - 响应格式, 默认 json
*/
var httpServer = function (req, res, next) {
if (req.url.indexOf('/api/mock') === -1) {
return next();
}
var data = url.parse(req.url, true).query;
setTimeout(function () {
res.statusCode = data.statusCode || 200;
res.setHeader('content-type', data.contentType || 'json');
res.end(data.response || '{}');
}, data.timeout || 0);
};
/**
* 源文件
*
@ -47,9 +77,6 @@ module.exports = function (config) {
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// Important: 所有插件必须在此声明
plugins: ['karma-*'],
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
// Important: 下列数组中文件将『逆序载入』
@ -136,6 +163,16 @@ module.exports = function (config) {
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
// 脚本调用请设为 true
singleRun: true
singleRun: true,
middleware: ['httpServer'],
plugins: ['karma-*', {
'middleware:httpServer': [
'factory', function () {
return httpServer;
}
]
}]
};
};

View File

@ -59,6 +59,13 @@ var customLaunchers = {
version: '11'
},
sl_edga: {
base: 'SauceLabs',
browserName: 'microsoftedge',
platform: 'Windows 10',
version: '15'
},
sl_firefox: {
base: 'SauceLabs',
browserName: 'firefox',

View File

@ -162,7 +162,7 @@
//中止冒泡
lay.stope = function(e){
e = e || win.event;
e = e || window.event;
e.stopPropagation
? e.stopPropagation()
: e.cancelBubble = true;

193
test/lay/modules/code.js Normal file
View File

@ -0,0 +1,193 @@
/**
* @file code - 测试
* @author xuexb <fe.xiaowu@gmail.com>
*/
/* global layui */
/* eslint-disable max-nested-callbacks, fecs-indent */
var laycode = layui.code;
var $ = layui.$;
/**
* 创建dom元素, 并返回 jquery 对象
*
* @inner
*
* @param {string} html 标签
*
* @return {jQuery}
*/
var createNode = function (html) {
return $(html).addClass('test-node').appendTo('body');
};
describe('code', function () {
// 输出测试节点
beforeEach(function () {
createNode('<div id="test-div"></div>');
});
// 删除节点
afterEach(function () {
$('.test-node').remove();
});
it('css loaded', function () {
expect($('#layuicss-skincodecss').length).to.equal(1, 'css link 节点必须存在');
});
it('default params', function () {
expect(function () {
laycode();
}).to.not.throw();
createNode('<pre class="layui-code"><div class="layui-code-div">123</div></pre>');
laycode();
expect($('.layui-code').hasClass('layui-code-view')).to.equal(true, '元素的样式名必须包含 layui-code-view');
expect($('.layui-code').find('.layui-code-div').length).to.equal(1, '默认没有 encode');
expect($('.layui-code').find('.layui-code-h3 a').length).to.equal(1, '默认有版权元素');
});
it('options.elem', function () {
createNode('<pre class="layui-test"><div>123</div></pre>');
laycode();
expect($('.layui-test').hasClass('layui-code-view')).to.be.false;
laycode({
elem: '.layui-test'
});
expect($('.layui-test').hasClass('layui-code-view')).to.be.true;
});
it('options.title', function () {
createNode('<pre class="layui-code"><div>123</div></pre>');
laycode({
title: 'layui',
// 主要是版权和标题在一个元素内
about: false
});
expect($('.layui-code-h3').text()).to.equal('layui', '判断标题元素');
});
it('options.height', function () {
createNode('<pre class="layui-code"><div>123</div></pre>');
laycode({
height: 100
});
expect($('.layui-code-ol').css('maxHeight')).to.equal('100px', '判断ol元素的最大高');
});
it('options.encode', function () {
createNode('<pre class="layui-code"><div class="layui-code-div">123\'"</div></pre>');
laycode({
encode: true
});
expect($('.layui-code').find('.layui-code-div').length).to.equal(0, 'encode 后元素被转义');
});
it('options.skin', function () {
createNode('<pre class="layui-code"><div class="layui-code-div">123</div></pre>');
laycode({
skin: 'notepad'
});
expect($('.layui-code-notepad').length).to.equal(1, '自定义风格存在');
});
it('options.about', function () {
createNode('<pre class="layui-code"><div class="layui-code-div">123</div></pre>');
laycode({
about: false
});
expect($('.layui-code').find('.layui-code-h3 a').length).to.equal(0, '不输出版权元素');
});
it('attr lay-title', function () {
createNode('<pre class="layui-code" lay-title="layui"><div>123</div></pre>');
laycode({
// 主要是版权和标题在一个元素内
about: false
});
expect($('.layui-code-h3').text()).to.equal('layui', '判断标题元素');
});
it('attr lay-height', function () {
createNode('<pre class="layui-code" lay-height="100px"><div>123</div></pre>');
laycode();
expect($('.layui-code-ol').css('maxHeight')).to.equal('100px', '判断ol元素的最大高');
});
it('attr lay-encode', function () {
createNode('<pre class="layui-code" lay-encode="true"><div class="layui-code-div">123</div></pre>');
laycode();
expect($('.layui-code').find('.layui-code-div').length).to.equal(0, 'encode 后元素被转义');
});
it('attr lay-skin', function () {
createNode('<pre class="layui-code" lay-skin="notepad"><div class="layui-code-div">123</div></pre>');
laycode();
expect($('.layui-code-notepad').length).to.equal(1, '自定义风格存在');
});
it('multiple nested', function () {
createNode([
'<pre class="layui-code">',
'<div class="layui-code-div">123</div>',
'<pre class="layui-code"><div class="layui-code-div">123</div></pre>',
'</pre>'
].join(''));
laycode();
expect($('.layui-code-view').length).to.equal(2, '必须输出2个代码块');
expect($('.layui-code-h3').length).to.equal(2, '必须输出2个标题元素');
});
it('multiple init', function () {
createNode('<pre class="layui-code"><div class="layui-code-div">123</div></pre>');
laycode();
expect($('.layui-code-view').length).to.equal(1);
expect($('.layui-code-h3').length).to.equal(1);
laycode();
expect($('.layui-code-view').length).to.equal(1, '同标签多次调用时 view 层只有一个');
expect($('.layui-code-h3').length).to.equal(2, '多次调用输出多个标题元素');
});
it('multiple line', function () {
var html = [];
for (var i = 0; i < 300; i++) {
html.push('<div class="layui-code-div">layui</div>');
}
createNode('<pre class="layui-code">' + html.join('\n') + '</pre>');
laycode();
expect($('.layui-code-div').length).to.equal(300);
});
});
/* eslint-enable max-nested-callbacks, fecs-indent */

File diff suppressed because it is too large Load Diff

View File

@ -194,7 +194,7 @@ describe('layui', function () {
});
it('error callback', function (done) {
layui.img('/404/404.gif', function () {}, function (e) {
layui.img('/api/mock?statusCode=404', function () {}, function (e) {
expect(e).to.not.undefined;
done();
});