1
0
mirror of synced 2025-12-11 08:58:19 +08:00

Add: Jasmine test

This commit is contained in:
surunzi
2016-08-16 18:56:34 +08:00
parent 89f63948d0
commit ec5fe27797
12 changed files with 382 additions and 35 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
/.idea/
/node_modules/
/test/lib/
eruda*
/script/icomoon/

View File

@@ -140,7 +140,7 @@ $css('#test', 'color'); // -> #fff
## $data
Data manipulation TODO
Wrapper of $attr, adds data- prefix to keys.
```javascript
$data('#test', 'attr1', 'eustia');
@@ -159,10 +159,39 @@ event.on('#test', 'click', function ()
## $insert
Insert html on different position. TODO
Insert html on different position.
### before
Insert content before elements.
### after
Insert content after elements.
### prepend
Insert content to the beginning of elements.
### append
Insert content to the end of elements.
|Name |Type |Desc |
|-------|--------------------|----------------------|
|element|string array element|Elements to manipulate|
|content|string |Html strings |
```javascript
$insert.append('#test', '<div>test</div>');
// <div id="test"><div class="mark"></div></div>
$insert.before('#test', '<div>eris</div>');
// -> <div>eris</div><div id="test"><div class="mark"></div></div>
$insert.after('#test', '<div>eris</div>');
// -> <div id="test"><div class="mark"></div></div><div>eris</div>
$insert.prepend('#test', '<div>eris</div>');
// -> <div id="test"><div>eris</div><div class="mark"></div></div>
$insert.append('#test', '<div>eris</div>');
// -> <div id="test"><div class="mark"></div><div>eris</div></div>
```
## $offset
@@ -183,15 +212,19 @@ Element property html, text, val getter and setter.
### html
Get the HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.
Get the HTML contents of the first element in the set of matched elements or
set the HTML contents of every matched element.
### text
Get the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements.
Get the combined text contents of each element in the set of matched
elements, including their descendants, or set the text contents of the
matched elements.
### val
Get the current value of the first element in the set of matched elements or set the value of every matched element.
Get the current value of the first element in the set of matched elements or
set the value of every matched element.
```javascript
$property.html('#test', 'eris');
@@ -219,7 +252,7 @@ Convert value into an array, if it's a string, do querySelector.
|value |element array string|Value to convert |
|return|array |Array of elements|
```
```javascript
$safeEls('.test'); // -> Array of elements with test class
```
@@ -283,20 +316,81 @@ Student.is(a); // -> true
## Emitter
TODO
Event emitter class which provides observer pattern.
### on
Bind event.
### off
Unbind event.
### once
Bind event that trigger once.
|Name |Type |Desc |
|--------|--------|--------------|
|event |string |Event name |
|listener|function|Event listener|
### emit
Emit event.
|Name |Type |Desc |
|-------|------|----------------------------|
|event |string|Event name |
|...args|* |Arguments passed to listener|
### mixin
[static] Mixin object class methods.
|Name|Type |Desc |
|----|------|---------------|
|obj |object|Object to mixin|
```javascript
var event = new Emitter();
event.on('test', function () { console.log('test') });
event.emit('test'); // Logs out 'test'.
Emitter.mixin({});
```
## Select
Simple wrapper of querySelectorAll to make dom selection easier.
### Constructor
### constructor
|Name |Type |Desc |
|--------|------|-------------------|
|selector|string|Dom selector string|
### find
Get desdendants of current matched elements.
|Name |Type |Desc |
|--------|------|-------------------|
|selector|string|Dom selector string|
### each
Iterate over matched elements.
|Name|Type |Desc |
|----|--------|------------------------------------|
|fn |function|Function to execute for each element|
```javascript
var test = new Select('#test');
var $test = new Select('#test');
$test.find('.test').each(function (idx, element)
{
// Manipulate dom nodes
});
```
## allKeys
@@ -316,6 +410,23 @@ obj.one = 1;
allKeys(obj) // -> ['zero', 'one']
```
## before
Create a function that invokes less than n times.
|Name |Type |Desc |
|------|--------|------------------------------------------------|
|n |number |Number of calls at which fn is no longer invoked|
|fn |function|Function to restrict |
|return|function|New restricted function |
Subsequent calls to the created function return the result of the last fn invocation.
```javascript
$(element).on('click', before(5, function() {}));
// -> allow function to be call 4 times at last.
```
## camelCase
Convert string to "camelCase".
@@ -634,6 +745,8 @@ Check if value is array-like.
|value |* |Value to check |
|return|boolean|True if value is array like|
> Function returns false.
```javascript
isArrLike('test'); // -> true
isArrLike(document.body.children); // -> true;
@@ -840,6 +953,24 @@ last([1, 2]); // -> 2
Inject script tag into page with given src value. TODO
## lpad
Pad string on the left side if it's shorter than length.
|Name |Type |Desc |
|-------|------|----------------------|
|str |string|String to pad |
|len |number|Padding length |
|[chars]|string|String used as padding|
|return |string|Resulted string |
```javascript
lpad('a', 5); // -> ' a'
lpad('a', 5, '-'); // -> '----a'
lpad('abc', 3, '-'); // -> 'abc'
lpad('abc', 5, 'ab'); // -> 'ababc'
```
## ltrim
Remove chars or white-spaces from beginning of string.
@@ -904,6 +1035,22 @@ Alias of Object.prototype.toString.
objToStr(5); // -> '[object Number]'
```
## once
Create a function that invokes once.
|Name |Type |Desc |
|------|--------|-----------------------|
|fn |function|Function to restrict |
|return|function|New restricted function|
```javascript
function init() {};
var initOnce = once(init);
initOnce();
initOnce(); // -> init is invoked once
```
## optimizeCb
TODO
@@ -912,10 +1059,56 @@ TODO
No documentation.
## partial
Partially apply a function by filling in given arguments.
|Name |Type |Desc |
|--------|--------|----------------------------------------|
|fn |function|Function to partially apply arguments to|
|partials|...* |Arguments to be partially applied |
|return |function|New partially applied function |
```javascript
var sub5 = partial(function (a, b) { return b - a }, 5);
sub(20); // -> 15
```
## pxToNum
No documentation.
## repeat
Repeat string n-times.
|Name |Type |Desc |
|------|------|----------------|
|str |string|String to repeat|
|n |number|Repeat times |
|return|string|Repeated string |
```javascript
repeat('a', 3); // -> 'aaa'
repeat('ab', 2); // -> 'abab'
repeat('*', 0); // -> ''
```
## restArgs
This accumulates the arguments passed into an array, after a given index.
|Name |Type |Desc |
|----------|--------|---------------------------------------|
|function |function|Function that needs rest parameters |
|startIndex|number |The start index to accumulates |
|return |function|Generated function with rest parameters|
```javascript
var paramArr = _.restArgs(function (rest) { return rest });
paramArr(1, 2, 3, 4); // -> [1, 2, 3, 4]
```
## rtrim
Remove chars or white-spaces from end of string.
@@ -938,7 +1131,17 @@ Create callback based on input value. TODO
## slice
TODO
Create slice of source array or array-like object.
|Name |Type |Desc |
|------------------|------|--------------------------|
|array |array |Array to slice |
|[start=0] |number|Start position |
|[end=array.length]|number|End position, not included|
```javascript
slice([1, 2, 3, 4], 1, 2); // -> [2]
```
## some
@@ -990,6 +1193,19 @@ Check if string starts with the given target string.
startWith('ab', 'a'); // -> true
```
## stripHtmlTag
Strip html tags from a string.
|Name |Type |Desc |
|------|------|---------------|
|str |string|String to strip|
|return|string|Resulted string|
```javascript
stripHtmlTag('<p>Hello</p>'); // -> 'Hello'
```
## toArr
Convert value to an array.

View File

@@ -1,11 +1,12 @@
{
"name": "eruda",
"version": "1.0.7",
"version": "1.0.8",
"description": "Console for Mobile Browsers",
"main": "eruda.js",
"scripts": {
"release": "webpack && webpack -p --config script/release.js",
"dev": "webpack-dev-server",
"cpTestLib": "bash script/cpTestLib.sh",
"utilDoc": "eustia doc src/lib/util.js -f md -o doc/Util_Api.md -t \"Eruda Util Documentation\""
},
"repository": {
@@ -33,6 +34,9 @@
"draggabilly": "^2.1.0",
"handlebars": "^4.0.5",
"handlebars-loader": "^1.1.4",
"jasmine-core": "^2.4.1",
"jasmine-jquery": "^2.1.1",
"jquery": "^3.1.0",
"js-beautify": "^1.6.2",
"json-loader": "^0.5.4",
"modernizr": "^3.3.1",

4
script/cpTestLib.sh Normal file
View File

@@ -0,0 +1,4 @@
mkdir -p test/lib
cp node_modules/jasmine-core/lib/jasmine-core/{jasmine.css,jasmine.js,jasmine-html.js,boot.js} test/lib/
cp node_modules/jasmine-jquery/lib/jasmine-jquery.js test/lib/
cp node_modules/jquery/dist/jquery.js test/lib/

View File

@@ -106,9 +106,15 @@ export default class Console extends Tool
}
_exposeLog()
{
let log = this._log;
let log = this._log,
methods = ['filter'].concat(CONSOLE_METHOD);
CONSOLE_METHOD.forEach(name => this[name] = (...args) => log[name](...args));
methods.forEach(name => this[name] = (...args) =>
{
log[name](...args);
return this;
});
}
_bindEvent(parent)
{

View File

@@ -298,11 +298,13 @@ export default class Log extends util.Emitter
if (filter === 'all') return logs;
var isRegexp = util.isRegExp(filter);
var isRegexp = util.isRegExp(filter),
isFn = util.isFn(filter);
return logs.filter(val =>
{
if (isRegexp) return filter.test(val.val);
if (isFn) return filter(val);
if (isRegexp) return filter.test(util.stripHtmlTag(val.val));
return val.ignoreFilter || val.type === filter;
});
@@ -337,8 +339,11 @@ var evalJs = jsInput =>
function errToStr(err)
{
var lines = err.stack.split('\n'),
msg = `${err.message || lines[0]}<br/>`,
stack = `<div class="eruda-stack">${lines.slice(1).join('<br/>')}</div>`;
msg = `${err.message || lines[0]}<br/>`;
lines = lines.filter(val => val.indexOf('eruda') < 0);
var stack = `<div class="eruda-stack">${lines.slice(1).join('<br/>')}</div>`;
return msg + stack.replace(regJsUrl, match => `<a href="${match}" target="_blank">${match}</a>`);
}

View File

@@ -3295,6 +3295,32 @@ module.exports = (function ()
return exports;
})();
/* ------------------------------ stripHtmlTag ------------------------------ */
var stripHtmlTag = _.stripHtmlTag = (function ()
{
/* Strip html tags from a string.
*
* |Name |Type |Desc |
* |------|------|---------------|
* |str |string|String to strip|
* |return|string|Resulted string|
*
* ```javascript
* stripHtmlTag('<p>Hello</p>'); // -> 'Hello'
* ```
*/
var regHtmlTag = /<[^>]*>/g;
function exports(str)
{
return str.replace(regHtmlTag, '');
}
return exports;
})();
/* ------------------------------ toStr ------------------------------ */
var toStr = _.toStr = (function ()

28
test/boot.js Normal file
View File

@@ -0,0 +1,28 @@
function boot(name)
{
// Need a little delay to make sure width and height of webpack dev server iframe are initialized.
setTimeout(function ()
{
eruda.init({tool: name});
eruda.show().get().config.set('displaySize', '50%');
loadJs('lib/boot', function ()
{
loadJs('lib/jasmine-jquery', function ()
{
// This is needed to trigger jasmine initialization.
loadJs(name, function ()
{
window.onload();
});
});
});
}, 500);
}
function loadJs(src, cb)
{
var script = document.createElement('script');
script.src = src + '.js';
script.onload = cb;
document.body.appendChild(script);
}

16
test/console.html Normal file
View File

@@ -0,0 +1,16 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Console</title>
<link rel="stylesheet" href="lib/jasmine.css">
<script src="lib/jquery.js"></script>
<script src="lib/jasmine.js"></script>
<script src="lib/jasmine-html.js"></script>
<script src="assets/eruda.js"></script>
<script src="boot.js"></script>
</head>
<body>
<script>boot('console');</script>
</body>
</html>

45
test/console.js Normal file
View File

@@ -0,0 +1,45 @@
var tool = eruda.get('console'),
$tool = $('.eruda-console');
describe('log', function ()
{
it('string', function ()
{
var text = 'This is a log';
tool.clear().log(text);
expect($tool.find('.eruda-log')).toContainText(text);
});
it('basic object', function ()
{
var obj = {a: 1};
tool.clear().log(obj);
expect($tool.find('.eruda-log')).toContainText('Object {"a":1}');
});
});
describe('filter', function ()
{
// Test case from https://github.com/liriliri/eruda/issues/14
it('function', function ()
{
tool.clear().filter(function (log)
{
return !(log.type === 'error' && /deprecated(.|\n)*stringify/.test(log.src.stack));
});
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);
});
});

View File

@@ -1,9 +0,0 @@
setTimeout(function ()
{
test();
}, 5000);
function test()
{
console.log('haha');
}

View File

@@ -5,10 +5,14 @@
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Eruda</title>
<link rel="stylesheet" href="style.css">
<script src="effect.js"></script>
<script src="assets/eruda.js"></script>
</head>
<body>
<div id="test-list">
<ul>
<li></li>
</ul>
</div>
<div id="test-element" class="border super-long1 super-long2 super-long-class-name" style="color:red">
<!-- This is a comment -->
<div class="child-one" style="background-image: url(http://7xn2zy.com1.z0.glb.clouddn.com/blog_elf.jpg); color: rgb(255, 80, 0);">One</div>
@@ -73,13 +77,14 @@
}
</script>
<script>
var el = document.createElement('div');
document.body.appendChild(el);
setTimeout(function () {
eruda.init({container: el});
init();
eruda.get('console').log('test');
}, 1000);
(function () {
var el = document.createElement('div');
document.body.appendChild(el);
setTimeout(function ()
{
eruda.init({container: el});
}, 1000);
})();
</script>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js"></script>
<script>FastClick.attach(document.body);</script>-->