1
0
mirror of synced 2025-12-07 22:18:17 +08:00

Dev: Test

This commit is contained in:
surunzi
2017-11-05 14:35:39 +08:00
parent 6b99b991e3
commit 3219e26eb1
8 changed files with 448 additions and 378 deletions

View File

@@ -22,7 +22,8 @@ var coverage = util.reduce(util.keys(remappedJson), function (result, source)
{
return source.match(/src.*\.js$/) &&
source.indexOf('node_modules') < 0 &&
source.indexOf('modernizr') < 0;
source.indexOf('modernizr') < 0 &&
source.indexOf('util') < 0;
}
return result;

View File

@@ -1,13 +1,14 @@
var tool = eruda.get('console'),
describe('console', function ()
{
var tool = eruda.get('console'),
$tool = $('.eruda-console');
beforeEach(function ()
{
tool.clear();
});
beforeEach(function ()
{
eruda.show('console');
tool.clear();
});
describe('log', function ()
{
it('string', function ()
{
var text = '<span>This is a log</span>';
@@ -18,7 +19,7 @@ describe('log', function ()
it('clear', function ()
{
expect($tool.find('.eruda-log')).toHaveLength(0);
expect($tool.find('.eruda-logs li')).toHaveLength(0);
});
it('recognize url', function ()
@@ -33,6 +34,7 @@ describe('log', function ()
tool.log(obj);
expect($tool.find('.eruda-log')).toContainText('Object { a: 1 }');
$tool.find('.eruda-log').click();
});
it('html', function ()
@@ -70,165 +72,210 @@ describe('log', function ()
tool.count('test');
expect($tool.find('.eruda-html')).toContainText('test: 2');
});
});
describe('substitution', function ()
{
it('number', function ()
describe('substitution', function ()
{
tool.log('Eruda is %d', 1.2, 'year old');
expect($tool.find('.eruda-log')).toContainText('Eruda is 1 year old');
tool.log('%i', 1.2, 'year old');
expect($tool.find('.eruda-log')).toContainText('1 year old');
tool.log('%f', 1.2, 'year old');
expect($tool.find('.eruda-log')).toContainText('1.2 year old');
});
it('string', function ()
{
tool.log('My name is %s', 'eruda');
expect($tool.find('.eruda-log')).toContainText('My name is eruda');
});
it('object', function ()
{
tool.log('Object is %O', {a: 1});
expect($tool.find('.eruda-log')).toContainText('Object is { a: 1 }');
tool.log('Dom is %o', document.createElement('script'));
expect($tool.find('.eruda-log')).toContainText('Dom is <script></script>');
});
it('style', function ()
{
tool.log('%cblue%cgreen', 'color:blue', 'color:green');
expect($tool.find('.eruda-log')).toContainHtml('<span style="color:blue">blue</span><span style="color:green">green</span>');
});
it('Repeat log', function ()
{
for (var i = 0; i < 10; i++) tool.log(1);
var $log = $tool.find('.eruda-log-item');
expect($log).toHaveLength(1);
expect($log.find('.eruda-count')).toContainText('10');
});
});
describe('table', function ()
{
it('wrong args', function ()
{
tool.table('test');
expect($tool.find('.eruda-table')).not.toContainElement('table');
});
it('sort keys', function ()
{
tool.table([{a: 1}, {d: 2, a: 2}, {c: 1}]);
expect($tool.find('.eruda-table thead tr')).toContainHtml('<th>(index)</th><th>a</th><th>c</th><th>d</th>');
});
it('basic', function ()
{
tool.table([{test: 1}, {test: 2, test2: 3}]);
expect($tool.find('.eruda-table tbody tr')).toHaveLength(2);
expect($tool.find('.eruda-table thead th')).toHaveLength(3);
});
it('filter', function ()
{
tool.table([{test: 1}, {test: 2, test2: 3}], 'test');
expect($tool.find('.eruda-table thead th')).toHaveLength(2);
});
});
describe('filter', function ()
{
// Test case from https://github.com/liriliri/eruda/issues/14
it('function', function ()
{
tool.filter(function (log)
it('number', function ()
{
return log.type !== 'error';
tool.log('Eruda is %d', 1.2, 'year old');
expect($tool.find('.eruda-log')).toContainText('Eruda is 1 year old');
tool.log('%i', 1.2, 'year old');
expect($tool.find('.eruda-log')).toContainText('1 year old');
tool.log('%f', 1.2, 'year old');
expect($tool.find('.eruda-log')).toContainText('1.2 year old');
});
var obj = {};
Object.defineProperty(obj, 'a', {
get: function ()
it('string', function ()
{
tool.log('My name is %s', 'eruda');
expect($tool.find('.eruda-log')).toContainText('My name is eruda');
});
it('object', function ()
{
tool.log('Object is %O', {a: 1});
expect($tool.find('.eruda-log')).toContainText('Object is { a: 1 }');
tool.log('Dom is %o', document.createElement('script'));
expect($tool.find('.eruda-log')).toContainText('Dom is <script></script>');
});
it('style', function ()
{
tool.log('%cblue%cgreen', 'color:blue', 'color:green');
expect($tool.find('.eruda-log')).toContainHtml('<span style="color:blue">blue</span><span style="color:green">green</span>');
});
it('Repeat log', function ()
{
for (var i = 0; i < 10; i++) tool.log(1);
var $log = $tool.find('.eruda-log-item');
expect($log).toHaveLength(1);
expect($log.find('.eruda-count')).toContainText('10');
});
});
describe('table', function ()
{
it('wrong args', function ()
{
tool.table('test');
expect($tool.find('.eruda-table')).not.toContainElement('table');
});
it('sort keys', function ()
{
tool.table([{a: 1}, {d: 2, a: 2}, {c: 1}]);
expect($tool.find('.eruda-table thead tr')).toContainHtml('<th>(index)</th><th>a</th><th>c</th><th>d</th>');
});
it('basic', function ()
{
tool.table([{test: 1}, {test: 2, test2: 3}]);
expect($tool.find('.eruda-table tbody tr')).toHaveLength(2);
expect($tool.find('.eruda-table thead th')).toHaveLength(3);
});
it('filter', function ()
{
tool.table([{test: 1}, {test: 2, test2: 3}], 'test');
expect($tool.find('.eruda-table thead th')).toHaveLength(2);
});
});
describe('filter', function ()
{
// Test case from https://github.com/liriliri/eruda/issues/14
it('function', function ()
{
tool.filter(function (log)
{
tool.error('deprecated');
return log.type !== 'error';
});
return 1;
}
var obj = {};
Object.defineProperty(obj, 'a', {
get: function ()
{
tool.error('deprecated');
return 1;
}
});
tool.log(obj);
expect($tool.find('.eruda-logs li').length).toEqual(1);
tool.filter('all');
});
tool.log(obj);
expect($tool.find('.eruda-logs li').length).toEqual(1);
tool.filter('all');
it('all info error warn log', function ()
{
tool.log('log').info('info').error('error').warn('warn').debug('debug');
expect($tool.find('.eruda-log-item')).toHaveLength(5);
tool.filter('info');
expect($tool.find('.eruda-log-item')).toHaveLength(1);
expect($tool.find('.eruda-info')).toHaveLength(1);
tool.filter('error');
expect($tool.find('.eruda-log-item')).toHaveLength(1);
expect($tool.find('.eruda-error')).toHaveLength(1);
tool.filter('warn');
expect($tool.find('.eruda-log-item')).toHaveLength(1);
expect($tool.find('.eruda-warn')).toHaveLength(1);
tool.filter('debug');
expect($tool.find('.eruda-log-item')).toHaveLength(1);
expect($tool.find('.eruda-debug')).toHaveLength(1);
tool.filter('all');
});
it('regex', function ()
{
tool.log('test').log('test2');
expect($tool.find('.eruda-log-item')).toHaveLength(2);
tool.filter(/test2/);
expect($tool.find('.eruda-log-item')).toHaveLength(1);
expect($tool.find('.eruda-log')).toContainText('test2');
tool.filter('all');
});
});
it('all info error warn log', function ()
describe('config', function ()
{
tool.log('log').info('info').error('error').warn('warn').debug('debug');
expect($tool.find('.eruda-log-item')).toHaveLength(5);
var config = tool.config;
tool.filter('info');
expect($tool.find('.eruda-log-item')).toHaveLength(1);
expect($tool.find('.eruda-info')).toHaveLength(1);
it('max number', function ()
{
config.set('maxLogNum', '10');
for (var i = 0; i < 20; i++) tool.log(i);
expect($tool.find('.eruda-log-item')).toHaveLength(10);
});
tool.filter('error');
expect($tool.find('.eruda-log-item')).toHaveLength(1);
expect($tool.find('.eruda-error')).toHaveLength(1);
it('override console', function ()
{
config.set('overrideConsole', true);
console.log('test');
expect($tool.find('.eruda-log-item')).toContainText('test');
});
tool.filter('warn');
expect($tool.find('.eruda-log-item')).toHaveLength(1);
expect($tool.find('.eruda-warn')).toHaveLength(1);
tool.filter('debug');
expect($tool.find('.eruda-log-item')).toHaveLength(1);
expect($tool.find('.eruda-debug')).toHaveLength(1);
tool.filter('all');
it('display extra info', function ()
{
config.set('displayExtraInfo', true);
tool.log('test');
expect($tool.find('.eruda-logs li')).toContainElement('.eruda-header');
});
});
it('regex', function ()
describe('ui', function ()
{
tool.log('test').log('test2');
expect($tool.find('.eruda-log-item')).toHaveLength(2);
it('clear', function ()
{
tool.log('test');
$('.eruda-clear-console').click();
expect($tool.find('.eruda-logs li')).toHaveLength(0);
});
tool.filter(/test2/);
expect($tool.find('.eruda-log-item')).toHaveLength(1);
expect($tool.find('.eruda-log')).toContainText('test2');
it('filter', function ()
{
tool.log('test');
tool.warn('test');
expect($tool.find('.eruda-logs li')).toHaveLength(2);
$('.eruda-filter[data-filter="warn"]').click();
expect($tool.find('.eruda-logs li')).toHaveLength(1);
$('.eruda-filter[data-filter="all"]').click();
});
tool.filter('all');
it('help', function ()
{
$tool.find('.eruda-help').click();
expect($tool.find('.eruda-html')).toHaveLength(1);
});
});
describe('execute', function ()
{
it('js', function ()
{
$tool.find('textarea').val('1+2');
$('.eruda-execute').click();
expect($tool.find('.eruda-output')).toContainText('3');
});
it('filter', function ()
{
tool.log('test');
tool.log('eruda');
expect($tool.find('.eruda-logs li')).toHaveLength(2);
$tool.find('textarea').val('/eruda');
$('.eruda-execute').click();
expect($tool.find('.eruda-logs li')).toHaveLength(1);
});
});
});
describe('config', function ()
{
var config = tool.config;
it('max number', function ()
{
config.set('maxLogNum', '10');
for (var i = 0; i < 20; i++) tool.log(i);
expect($tool.find('.eruda-log-item')).toHaveLength(10);
});
it('override console', function ()
{
config.set('overrideConsole', true);
console.log('test');
expect($tool.find('.eruda-log-item')).toContainText('test');
});
it('display extra info', function ()
{
config.set('displayExtraInfo', true);
tool.log('test');
expect($tool.find('.eruda-logs li')).toContainElement('.eruda-header');
});
});

View File

@@ -1,12 +1,20 @@
var tool = eruda.get('elements'),
$tool = $('.eruda-elements');
describe('api', function ()
describe('elements', function ()
{
it('set element', function ()
var tool = eruda.get('elements'),
$tool = $('.eruda-elements');
beforeEach(function ()
{
tool.set(document.body);
expect($tool.find('.eruda-parent')).toContainText('html');
expect($tool.find('.eruda-breadcrumb')).toContainText('body');
eruda.show('elements');
});
});
describe('api', function ()
{
it('set element', function ()
{
tool.set(document.body);
expect($tool.find('.eruda-parent')).toContainText('html');
expect($tool.find('.eruda-breadcrumb')).toContainText('body');
});
});
});

View File

@@ -1,83 +1,86 @@
describe('init', function ()
describe('devTools', function ()
{
it('destroy', function ()
describe('init', function ()
{
eruda.destroy();
expect($('#eruda')).toHaveLength(0);
});
it('init', function ()
{
var container = document.createElement('div');
container.id = 'eruda';
document.body.appendChild(container);
eruda.init({
container: container,
tool: []
});
var $eruda = $('#eruda');
expect($eruda.find('.eruda-dev-tools')).toHaveLength(1);
});
});
describe('tool', function ()
{
it('add', function ()
{
eruda.add({
name: 'test',
init: function ($el)
{
this._$el = $el;
$el.html('Test Plugin');
}
});
expect($('.eruda-test')).toContainText('Test Plugin');
});
it('show', function ()
{
var $tool = $('.eruda-test');
expect($tool).toBeHidden();
eruda.show('test');
expect($tool).toHaveCss({display: 'block'});
});
it('remove', function ()
{
eruda.remove('test');
expect($('.eruda-test')).toHaveLength(0);
});
});
describe('display', function ()
{
it('show', function ()
{
eruda.show();
expect($('.eruda-dev-tools')).toHaveCss({display: 'block'});
});
it('hide', function (done)
{
eruda.hide();
setTimeout(function ()
it('destroy', function ()
{
expect($('.eruda-dev-tools')).toBeHidden();
done();
}, 500);
eruda.destroy();
expect($('#eruda')).toHaveLength(0);
});
it('init', function ()
{
var container = document.createElement('div');
container.id = 'eruda';
document.body.appendChild(container);
eruda.init({
container: container,
tool: []
});
var $eruda = $('#eruda');
expect($eruda.find('.eruda-dev-tools')).toHaveLength(1);
});
});
describe('tool', function ()
{
it('add', function ()
{
eruda.add({
name: 'test',
init: function ($el)
{
this._$el = $el;
$el.html('Test Plugin');
}
});
expect($('.eruda-test')).toContainText('Test Plugin');
});
it('show', function ()
{
var $tool = $('.eruda-test');
expect($tool).toBeHidden();
eruda.show('test');
expect($tool).toHaveCss({display: 'block'});
});
it('remove', function ()
{
eruda.remove('test');
expect($('.eruda-test')).toHaveLength(0);
});
});
describe('display', function ()
{
it('show', function ()
{
eruda.show();
expect($('.eruda-dev-tools')).toHaveCss({display: 'block'});
});
it('hide', function (done)
{
eruda.hide();
setTimeout(function ()
{
expect($('.eruda-dev-tools')).toBeHidden();
done();
}, 500);
});
});
describe('scale', function ()
{
it('get', function ()
{
eruda.scale(1);
expect(eruda.scale()).toBe(1);
});
});
});
describe('scale', function ()
{
it('get', function ()
{
eruda.scale(1);
expect(eruda.scale()).toBe(1);
});
});

View File

@@ -1,42 +1,45 @@
var tool = eruda.get('info'),
$tool = $('.eruda-info');
describe('default', function ()
describe('info', function ()
{
it('location', function ()
var tool = eruda.get('info'),
$tool = $('.eruda-info');
describe('default', function ()
{
expect($tool.find('.eruda-content').eq(0)).toContainText(location.href);
it('location', function ()
{
expect($tool.find('.eruda-content').eq(0)).toContainText(location.href);
});
it('user agent', function ()
{
expect($tool.find('.eruda-content').eq(1)).toContainText(navigator.userAgent);
});
it('device', function ()
{
expect($tool.find('.eruda-content').eq(2)).toContainText(window.innerWidth);
});
});
it('user agent', function ()
describe('basic', function ()
{
expect($tool.find('.eruda-content').eq(1)).toContainText(navigator.userAgent);
});
it('clear', function ()
{
tool.clear();
expect($tool.find('li')).toHaveLength(0);
});
it('device', function ()
{
expect($tool.find('.eruda-content').eq(2)).toContainText(window.innerWidth);
});
});
it('add', function ()
{
tool.add('test', 'eruda');
expect($tool.find('.eruda-title')).toContainText('test');
expect($tool.find('.eruda-content')).toContainText('eruda');
});
describe('basic', function ()
{
it('clear', function ()
{
tool.clear();
expect($tool.find('li')).toHaveLength(0);
it('remove', function ()
{
tool.remove('test');
expect($tool.find('li')).toHaveLength(0);
});
});
it('add', function ()
{
tool.add('test', 'eruda');
expect($tool.find('.eruda-title')).toContainText('test');
expect($tool.find('.eruda-content')).toContainText('eruda');
});
it('remove', function ()
{
tool.remove('test');
expect($tool.find('li')).toHaveLength(0);
});
});
});

View File

@@ -1,75 +1,78 @@
var tool = eruda.get('settings'),
$tool = $('.eruda-settings');
var cfg = eruda.config.create('eruda-test');
cfg.set({
testSwitch: false,
testSelect: '1',
testRange: 1,
testColor: '#000'
});
beforeEach(function ()
describe('settings', function ()
{
tool.clear();
});
var tool = eruda.get('settings'),
$tool = $('.eruda-settings');
describe('switch', function ()
{
it('ui', function ()
{
var text = 'Test Switch';
tool.switch(cfg, 'testSwitch', text);
expect($tool.find('.eruda-switch')).toContainText(text);
var cfg = eruda.config.create('eruda-test');
cfg.set({
testSwitch: false,
testSelect: '1',
testRange: 1,
testColor: '#000'
});
});
describe('separator', function ()
{
it('ui', function ()
beforeEach(function ()
{
tool.separator();
expect($tool.find('.eruda-separator').length).toEqual(1);
tool.clear();
});
});
describe('select', function ()
{
it('ui', function ()
describe('switch', function ()
{
var text = 'Test Select';
it('ui', function ()
{
var text = 'Test Switch';
tool.select(cfg, 'testSelect', text, ['1', '2', '3']);
var $el = $tool.find('.eruda-select');
expect($el.find('ul li').length).toEqual(3);
expect($el.find('.eruda-head')).toContainText(text);
expect($el.find('.eruda-val')).toContainText('1');
tool.switch(cfg, 'testSwitch', text);
expect($tool.find('.eruda-switch')).toContainText(text);
});
});
});
describe('range', function ()
{
it('ui', function ()
describe('separator', function ()
{
var text = 'Test Range';
tool.range(cfg, 'testRange', text, {min: 0, max: 1, step: 0.1});
var $el = $tool.find('.eruda-range');
expect($el.find('.eruda-head')).toContainText(text);
expect($el.find('input').length).toEqual(1);
it('ui', function ()
{
tool.separator();
expect($tool.find('.eruda-separator').length).toEqual(1);
});
});
});
describe('color', function ()
{
it('ui', function ()
describe('select', function ()
{
var text = 'Test Color';
it('ui', function ()
{
var text = 'Test Select';
tool.color(cfg, 'testColor', text, ['#000', '#fff']);
var $el = $tool.find('.eruda-color');
expect($el.find('.eruda-head')).toContainText(text);
expect($el.find('ul li').length).toEqual(2);
tool.select(cfg, 'testSelect', text, ['1', '2', '3']);
var $el = $tool.find('.eruda-select');
expect($el.find('ul li').length).toEqual(3);
expect($el.find('.eruda-head')).toContainText(text);
expect($el.find('.eruda-val')).toContainText('1');
});
});
describe('range', function ()
{
it('ui', function ()
{
var text = 'Test Range';
tool.range(cfg, 'testRange', text, {min: 0, max: 1, step: 0.1});
var $el = $tool.find('.eruda-range');
expect($el.find('.eruda-head')).toContainText(text);
expect($el.find('input').length).toEqual(1);
});
});
describe('color', function ()
{
it('ui', function ()
{
var text = 'Test Color';
tool.color(cfg, 'testColor', text, ['#000', '#fff']);
var $el = $tool.find('.eruda-color');
expect($el.find('.eruda-head')).toContainText(text);
expect($el.find('ul li').length).toEqual(2);
});
});
});

View File

@@ -1,51 +1,53 @@
var tool = eruda.get('snippets'),
$tool = $('.eruda-snippets');
describe('default', function ()
describe('snippets', function ()
{
it('border all', function ()
{
expect($tool.find('.eruda-name').eq(0)).toContainText('Border All');
});
var tool = eruda.get('snippets'),
$tool = $('.eruda-snippets');
it('refresh page', function ()
describe('default', function ()
{
expect($tool.find('.eruda-name').eq(1)).toContainText('Refresh Page');
});
it('search text', function ()
{
expect($tool.find('.eruda-name').eq(2)).toContainText('Search Text');
});
it('edit page', function ()
{
expect($tool.find('.eruda-name').eq(3)).toContainText('Edit Page');
});
});
describe('basic', function ()
{
it('clear', function ()
{
tool.clear();
expect($tool.find('.eruda-name')).toHaveLength(0);
});
it('add', function ()
{
tool.add('Test', function ()
it('border all', function ()
{
console.log('eruda');
}, 'This is the description');
expect($tool.find('.eruda-name')).toContainText('Test');
expect($tool.find('.eruda-description')).toContainText('This is the description');
expect($tool.find('.eruda-name').eq(0)).toContainText('Border All');
});
it('refresh page', function ()
{
expect($tool.find('.eruda-name').eq(1)).toContainText('Refresh Page');
});
it('search text', function ()
{
expect($tool.find('.eruda-name').eq(2)).toContainText('Search Text');
});
it('edit page', function ()
{
expect($tool.find('.eruda-name').eq(3)).toContainText('Edit Page');
});
});
it('remove', function ()
describe('basic', function ()
{
tool.remove('Test');
expect($tool.find('.eruda-name')).toHaveLength(0);
});
});
it('clear', function ()
{
tool.clear();
expect($tool.find('.eruda-name')).toHaveLength(0);
});
it('add', function ()
{
tool.add('Test', function ()
{
console.log('eruda');
}, 'This is the description');
expect($tool.find('.eruda-name')).toContainText('Test');
expect($tool.find('.eruda-description')).toContainText('This is the description');
});
it('remove', function ()
{
tool.remove('Test');
expect($tool.find('.eruda-name')).toHaveLength(0);
});
});
});

View File

@@ -1,11 +1,14 @@
var tool = eruda.get('sources'),
$tool = $('.eruda-sources');
describe('highlight code', function ()
describe('sources', function ()
{
it('js', function ()
var tool = eruda.get('sources'),
$tool = $('.eruda-sources');
describe('highlight code', function ()
{
tool.set('js', '/* test */');
expect($tool.find('.eruda-content')).toContainHtml('<span style="color:#63a35c;">/* test */</span>');
it('js', function ()
{
tool.set('js', '/* test */');
expect($tool.find('.eruda-content')).toContainHtml('<span style="color:#63a35c;">/* test */</span>');
});
});
});
});