添加测试用例和测试配置

pull/48/head
xuexb 7 years ago
parent f28819e903
commit 1c55deb97c

@ -0,0 +1,126 @@
/**
* @file karma自动化测试配置
* @author fe.xiaowu@gmail.com
*/
/**
* 源文件
*
* @type {Array}
*/
var sourceFileMap = [
'src/layui.js',
'src/lay/modules/jquery.js',
'src/lay/modules/carousel.js',
'src/lay/modules/code.js',
'src/lay/modules/element.js',
'src/lay/modules/flow.js',
'src/lay/modules/form.js',
'src/lay/modules/laydate.js',
'src/lay/modules/layedit.js',
'src/lay/modules/layer.js',
'src/lay/modules/laypage.js',
'src/lay/modules/laytpl.js',
'src/lay/modules/table.js',
'src/lay/modules/tree.js',
'src/lay/modules/upload.js',
'src/lay/modules/util.js',
'src/lay/modules/mobile/zepto.js',
'src/lay/modules/mobile/layer-mobile.js',
'src/lay/modules/mobile/upload-mobile.js',
];
/**
* 测试覆盖率文件, 要忽略 jquery.jszepto.js
*
* @type {Object}
*/
var coverageFileMap = {};
sourceFileMap.filter(function (uri) {
return !/(jquery|zepto)\.js$/.test(uri);
}).forEach(function (uri) {
coverageFileMap[uri] = ['coverage'];
});
module.exports = function(config) {
config.set({
// 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: 下列数组中文件将『逆序载入』
frameworks: ['mocha', 'chai', 'chai-sinon'],
// list of files / patterns to load in the browser
files: sourceFileMap.concat('test/**/*.js'),
// list of files to exclude
exclude: [],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: coverageFileMap,
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: [
'mocha'
// 'coverage'
],
coverageReporter: {
// specify a common output directory
dir: '.',
reporters: [
// { type: 'html', subdir: 'report-html' },
{
type: 'lcov',
subdir: 'coverage'
},
{
type: 'text-summary'
}
]
},
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
// Note: 如果要调试Karma请设置为DEBUG
logLevel: config.LOG_INFO,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: [
'PhantomJS'
],
// enable / disable watching file and executing tests whenever any file changes
// Note: 代码改动自动运行测试需要singleRun为false
autoWatch: false,
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
// 脚本调用请设为 true
singleRun: true
});
};

@ -0,0 +1,227 @@
/**
* @file laytpl - 测试
* @author xuexb <fe.xiaowu@gmail.com>
*/
/* global layui */
var laytpl = layui.laytpl;
describe('laytpl', function () {
it('param is error', function () {
[
[], {},
null,
1,
true
].forEach(function (key) {
expect(laytpl(key)).to.have.string('Laytpl Error');
});
});
it('async render callback', function (done) {
expect(laytpl('').render()).to.have.string('Laytpl Error');
laytpl('{{ d.name }}是一位公猿').render({
name: '贤心'
}, function (result) {
expect(result).to.equal('贤心是一位公猿');
done();
});
});
it('sync result', function () {
var result = laytpl('{{ d.name }}是一位公猿').render({
name: '贤心'
});
expect(result).to.equal('贤心是一位公猿');
});
it('cached', function () {
var compile = laytpl('{{ d.name }}');
expect(compile.render({
name: 1
})).to.equal('1');
expect(compile.render({
name: 2
})).to.equal('2');
});
it('unescape result', function () {
var result = laytpl('{{ d.name }}<div></div>').render({
name: '<em>laytpl</em>'
});
expect(result).to.equal('<em>laytpl</em><div></div>');
});
it('escape result', function () {
var result = laytpl('{{= d.name }}<div></div>').render({
name: '<em>laytpl</em>'
});
expect(result).to.equal('&lt;em&gt;laytpl&lt;/em&gt;<div></div>');
});
describe('typeof result', function () {
it('string', function () {
expect(laytpl('{{ d.name }}').render({
name: 1
})).to.be.a('string');
expect(laytpl([
'{{# ',
' if (true) {',
' return "1";',
' }',
'}}'
].join('')).render({})).to.be.a('string');
});
// 表达式返回boolean
it('boolean', function () {
expect(laytpl([
'{{# ',
' if (true) {',
' return true;',
' }',
'}}'
].join('')).render({})).to.be.a('boolean');
});
it('number', function () {
expect(laytpl([
'{{# ',
' if (true) {',
' return 1;',
' }',
'}}'
].join('')).render({})).to.be.a('number');
});
});
describe('method config', function () {
// reset
afterEach(function () {
laytpl.config({
open: '{{',
close: '}}'
});
});
it('typeof', function () {
expect(laytpl.config).to.be.a('function');
});
it('param is empty', function () {
expect(laytpl.config()).to.be.undefined;
});
it('set open', function () {
laytpl.config({
open: '<%'
});
var result = laytpl([
'<%# var name = "laytpl"; }}',
'你好, <% name }}, <% d.date }}'
].join('')).render({
date: '2017'
});
expect(result).to.equal('你好, laytpl, 2017');
});
it('set open and close', function () {
laytpl.config({
open: '<%',
close: '%>'
});
var result = laytpl([
'<%# var name = "laytpl"; %>',
'你好, <% name %>, <% d.date %>'
].join('')).render({
date: '2017'
});
expect(result).to.equal('你好, laytpl, 2017');
});
});
describe('js expression', function () {
it('var', function () {
var result = laytpl('{{# var type = 1; }}{{ type }}{{ d.name }}').render({
name: 2
});
expect(result).to.equal('12');
});
it('function', function () {
var result = laytpl('{{# var fn = function () {return "ok"}; }}{{ fn() }}').render({});
expect(result).to.equal('ok');
});
it('for', function () {
var result = laytpl([
'{{# ',
' var fn = function () {',
' var num = 0;',
' for (var i = 0; i < 10;i++) {',
' num += 1;',
' }',
' return num',
' };',
'}}',
'{{# ',
' var name = "laytpl";',
'}}',
'你好, {{ name }}, {{ d.name }}, {{ fn() }}'
].join('')).render({
name: 'ok'
});
expect(result).to.equal('你好, laytpl, ok, 10');
});
it('if else', function () {
var result;
result = laytpl([
'{{# ',
' if (true) {',
' return true;',
' }',
' else {',
' return false',
' }',
'}}'
].join('')).render({});
expect(result).to.be.true;
result = laytpl([
'{{# ',
' if (!true) {',
' return true;',
' }',
' else {',
' return false',
' }',
'}}'
].join('')).render({});
expect(result).to.be.false;
});
});
describe('parse error', function () {
it('error var', function () {
var result = laytpl('{{ data.xxoo }}').render({});
expect(result).to.have.string('Can\'t find variable: data');
expect(result).to.have.string('Laytpl Error');
});
it('error expression', function () {
var result = laytpl('{{# var xxoo = ; }}').render({});
expect(result).to.have.string('Laytpl Error');
expect(result).to.have.string('Unexpected token \';\'');
});
});
});
Loading…
Cancel
Save