Skip to content

Commit

Permalink
fixed: localStorage is null. add unit test script. add editorconfig.
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed Jun 7, 2016
1 parent 65e361c commit 790259d
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 16 deletions.
25 changes: 25 additions & 0 deletions .editorconfig
@@ -0,0 +1,25 @@
# editorconfig.org
root = true

# Unix-style newlines with a newline ending every file
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{html,js,ts,css,less,scss,xml,json}]
indent_style = space
indent_size = 2

[*.yml]
indent_style = space
indent_size = 2

[jdists]
indent_style = space
indent_size = 2
4 changes: 2 additions & 2 deletions dist/vconsole.min.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions package.json
Expand Up @@ -5,7 +5,8 @@
"homepage": "https://github.com/WechatFE/vConsole",
"main": "dist/vconsole.min.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha",
"dist": "webpack && npm test"
},
"keywords": [
"console",
Expand All @@ -29,7 +30,10 @@
"less": "^2.5.3",
"less-loader": "^2.2.3",
"style-loader": "^0.13.1",
"webpack": "~1.12.11"
"webpack": "~1.12.11",
"jsdom": "^9.2.1",
"mocha": "^2.5.3",
"chai": "^3.5.0"
},
"author": "WechatFE Team",
"license": "MIT"
Expand Down
18 changes: 12 additions & 6 deletions src/lib/tool.js
Expand Up @@ -63,12 +63,12 @@ export function isObject(value) {
||
// if it isn't a primitive value, then it is a common object
(
!isNumber(value) &&
!isString(value) &&
!isArray(value) &&
!isNull(value) &&
!isFunction(value) &&
!isUndefined(value) &&
!isNumber(value) &&
!isString(value) &&
!isArray(value) &&
!isNull(value) &&
!isFunction(value) &&
!isUndefined(value) &&
!isSymbol(value)
)
);
Expand Down Expand Up @@ -185,10 +185,16 @@ export function JSONStringify(obj) {
* localStorage methods
*/
export function setStorage(key, value) {
if (!window.localStorage) {
return;
}
key = 'vConsole_' + key;
localStorage.setItem(key, value);
}
export function getStorage(key) {
if (!window.localStorage) {
return;
}
key = 'vConsole_' + key;
return localStorage.getItem(key);
}
4 changes: 2 additions & 2 deletions test/ajax.html
Expand Up @@ -7,8 +7,8 @@
<link href="../example/lib/weui.min.css" rel="stylesheet"/>
<link href="../example/lib/demo.css" rel="stylesheet"/>

<script src="../example//lib/zepto.min.js"></script>
<script src="../example//lib/zepto.touch.min.js"></script>
<script src="../example/lib/zepto.min.js"></script>
<script src="../example/lib/zepto.touch.min.js"></script>

<script src="../dist/vconsole.min.js"></script>

Expand Down
4 changes: 2 additions & 2 deletions test/log.html
Expand Up @@ -7,8 +7,8 @@
<link href="../example/lib/weui.min.css" rel="stylesheet"/>
<link href="../example/lib/demo.css" rel="stylesheet"/>

<script src="../example//lib/zepto.min.js"></script>
<script src="../example//lib/zepto.touch.min.js"></script>
<script src="../example/lib/zepto.min.js"></script>
<script src="../example/lib/zepto.touch.min.js"></script>

<script src="../dist/vconsole.min.js"></script>
</head>
Expand Down
4 changes: 2 additions & 2 deletions test/plugin.html
Expand Up @@ -7,8 +7,8 @@
<link href="../example/lib/weui.min.css" rel="stylesheet"/>
<link href="../example/lib/demo.css" rel="stylesheet"/>

<script src="../example//lib/zepto.min.js"></script>
<script src="../example//lib/zepto.touch.min.js"></script>
<script src="../example/lib/zepto.min.js"></script>
<script src="../example/lib/zepto.touch.min.js"></script>

<script src="../dist/vconsole.min.js"></script>
</head>
Expand Down
130 changes: 130 additions & 0 deletions test/test.js
@@ -0,0 +1,130 @@
describe("vConsole", function() {

var assert = require('chai').assert;
var util = require('util');
var jsdom = require('jsdom');

it('touch event', function(done) {
jsdom.env('test/log.html', function(err, window) {
var document = window.document;

window.localStorage = {
getItem: function (key) {
return this[key];
},
setItem: function (key, value) {
this[key] = String(value);
}
};
global.localStorage = window.localStorage;

var vcSwitch = document.querySelector('.vc-switch');

var eventTouchstart = document.createEvent('Event');
eventTouchstart.initEvent('touchstart', true, true);
var point = { x: 10, y: 10 };
eventTouchstart.touches = [{
identifier: Math.random(),
pageX: point.x,
pageY: point.y,
screenX: point.x,
screenY: point.y,
clientX: point.x,
clientY: point.y
}];
vcSwitch.dispatchEvent(eventTouchstart);

var eventTouchmove = document.createEvent('Event');
eventTouchmove.initEvent('touchmove', true, true);
var point = { x: 12, y: 12 };
eventTouchmove.touches = [{
identifier: Math.random(),
pageX: point.x,
pageY: point.y,
screenX: point.x,
screenY: point.y,
clientX: point.x,
clientY: point.y
}];
vcSwitch.dispatchEvent(eventTouchmove);

var eventTouchend = document.createEvent('Event');
eventTouchend.initEvent('touchend', true, true);
vcSwitch.dispatchEvent(eventTouchend);
setTimeout(function () {
assert.equal(localStorage.vConsole_switch_x, '8');

global.localStorage = null;
window.localStorage = null;
done();
}, 100);
}, {
features: {
FetchExternalResources: ["link", "script"]
}
});
});

it('log.html', function(done) {
jsdom.env('test/log.html', function(err, window) {
var document = window.document;

assert.equal(document.querySelector('#__vconsole') !== null, true);

document.querySelector('.weui_btn.weui_btn_default:nth-of-type(1)').click(); // formattedLog
assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-item .vc-item-content').innerHTML, ' formattedLog() Start');

document.querySelector('.vc-toolbar .vc-tool.vc-tool-default:nth-of-type(1)').click(); // clear
assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-item .vc-item-content'), null);

document.querySelector('.weui_btn.weui_btn_default:nth-of-type(2)').click(); // normalObject
assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-item .vc-item-content').innerHTML, ' normalObject() Start');

document.querySelector('.vc-toolbar .vc-tool.vc-tool-default:nth-of-type(1)').click(); // clear
assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-item .vc-item-content'), null);

done();
}, {
features: {
FetchExternalResources: ["link", "script"]
}
});
});

it('plugin.html', function(done) {
jsdom.env('test/plugin.html', function(err, window) {
var document = window.document;
assert.equal(document.querySelector('#__vconsole') !== null, true);

document.querySelector('.page a:nth-of-type(1)').click(); // newTab
assert.equal(document.querySelector('.vc-tabbar .vc-tab:nth-of-type(4)').innerHTML, 'Tab1');

done();
}, {
features: {
FetchExternalResources: ["link", "script"]
}
});
});

it('ajax.html', function(done) {
this.timeout(2000);

jsdom.env('test/ajax.html', function(err, window) {
var document = window.document;
assert.equal(document.querySelector('#__vconsole') !== null, true);

document.querySelector('.page a:nth-of-type(1)').click(); // asyncAjax
setTimeout(function () {
assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-fold-outer').innerHTML, 'Object {ret: 0, msg: "ok"}');
done();
}, 10)

}, {
features: {
FetchExternalResources: ["link", "script"]
}
});
});

});

0 comments on commit 790259d

Please sign in to comment.