Skip to content

Commit 790259d

Browse files
committed
fixed: localStorage is null. add unit test script. add editorconfig.
1 parent 65e361c commit 790259d

File tree

8 files changed

+181
-16
lines changed

8 files changed

+181
-16
lines changed

.editorconfig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# editorconfig.org
2+
root = true
3+
4+
# Unix-style newlines with a newline ending every file
5+
[*]
6+
indent_style = space
7+
indent_size = 2
8+
end_of_line = lf
9+
charset = utf-8
10+
trim_trailing_whitespace = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false
14+
15+
[*.{html,js,ts,css,less,scss,xml,json}]
16+
indent_style = space
17+
indent_size = 2
18+
19+
[*.yml]
20+
indent_style = space
21+
indent_size = 2
22+
23+
[jdists]
24+
indent_style = space
25+
indent_size = 2

dist/vconsole.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"homepage": "https://github.com/WechatFE/vConsole",
66
"main": "dist/vconsole.min.js",
77
"scripts": {
8-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "mocha",
9+
"dist": "webpack && npm test"
910
},
1011
"keywords": [
1112
"console",
@@ -29,7 +30,10 @@
2930
"less": "^2.5.3",
3031
"less-loader": "^2.2.3",
3132
"style-loader": "^0.13.1",
32-
"webpack": "~1.12.11"
33+
"webpack": "~1.12.11",
34+
"jsdom": "^9.2.1",
35+
"mocha": "^2.5.3",
36+
"chai": "^3.5.0"
3337
},
3438
"author": "WechatFE Team",
3539
"license": "MIT"

src/lib/tool.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ export function isObject(value) {
6363
||
6464
// if it isn't a primitive value, then it is a common object
6565
(
66-
!isNumber(value) &&
67-
!isString(value) &&
68-
!isArray(value) &&
69-
!isNull(value) &&
70-
!isFunction(value) &&
71-
!isUndefined(value) &&
66+
!isNumber(value) &&
67+
!isString(value) &&
68+
!isArray(value) &&
69+
!isNull(value) &&
70+
!isFunction(value) &&
71+
!isUndefined(value) &&
7272
!isSymbol(value)
7373
)
7474
);
@@ -185,10 +185,16 @@ export function JSONStringify(obj) {
185185
* localStorage methods
186186
*/
187187
export function setStorage(key, value) {
188+
if (!window.localStorage) {
189+
return;
190+
}
188191
key = 'vConsole_' + key;
189192
localStorage.setItem(key, value);
190193
}
191194
export function getStorage(key) {
195+
if (!window.localStorage) {
196+
return;
197+
}
192198
key = 'vConsole_' + key;
193199
return localStorage.getItem(key);
194200
}

test/ajax.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<link href="../example/lib/weui.min.css" rel="stylesheet"/>
88
<link href="../example/lib/demo.css" rel="stylesheet"/>
99

10-
<script src="../example//lib/zepto.min.js"></script>
11-
<script src="../example//lib/zepto.touch.min.js"></script>
10+
<script src="../example/lib/zepto.min.js"></script>
11+
<script src="../example/lib/zepto.touch.min.js"></script>
1212

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

test/log.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<link href="../example/lib/weui.min.css" rel="stylesheet"/>
88
<link href="../example/lib/demo.css" rel="stylesheet"/>
99

10-
<script src="../example//lib/zepto.min.js"></script>
11-
<script src="../example//lib/zepto.touch.min.js"></script>
10+
<script src="../example/lib/zepto.min.js"></script>
11+
<script src="../example/lib/zepto.touch.min.js"></script>
1212

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

test/plugin.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<link href="../example/lib/weui.min.css" rel="stylesheet"/>
88
<link href="../example/lib/demo.css" rel="stylesheet"/>
99

10-
<script src="../example//lib/zepto.min.js"></script>
11-
<script src="../example//lib/zepto.touch.min.js"></script>
10+
<script src="../example/lib/zepto.min.js"></script>
11+
<script src="../example/lib/zepto.touch.min.js"></script>
1212

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

test/test.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
describe("vConsole", function() {
2+
3+
var assert = require('chai').assert;
4+
var util = require('util');
5+
var jsdom = require('jsdom');
6+
7+
it('touch event', function(done) {
8+
jsdom.env('test/log.html', function(err, window) {
9+
var document = window.document;
10+
11+
window.localStorage = {
12+
getItem: function (key) {
13+
return this[key];
14+
},
15+
setItem: function (key, value) {
16+
this[key] = String(value);
17+
}
18+
};
19+
global.localStorage = window.localStorage;
20+
21+
var vcSwitch = document.querySelector('.vc-switch');
22+
23+
var eventTouchstart = document.createEvent('Event');
24+
eventTouchstart.initEvent('touchstart', true, true);
25+
var point = { x: 10, y: 10 };
26+
eventTouchstart.touches = [{
27+
identifier: Math.random(),
28+
pageX: point.x,
29+
pageY: point.y,
30+
screenX: point.x,
31+
screenY: point.y,
32+
clientX: point.x,
33+
clientY: point.y
34+
}];
35+
vcSwitch.dispatchEvent(eventTouchstart);
36+
37+
var eventTouchmove = document.createEvent('Event');
38+
eventTouchmove.initEvent('touchmove', true, true);
39+
var point = { x: 12, y: 12 };
40+
eventTouchmove.touches = [{
41+
identifier: Math.random(),
42+
pageX: point.x,
43+
pageY: point.y,
44+
screenX: point.x,
45+
screenY: point.y,
46+
clientX: point.x,
47+
clientY: point.y
48+
}];
49+
vcSwitch.dispatchEvent(eventTouchmove);
50+
51+
var eventTouchend = document.createEvent('Event');
52+
eventTouchend.initEvent('touchend', true, true);
53+
vcSwitch.dispatchEvent(eventTouchend);
54+
setTimeout(function () {
55+
assert.equal(localStorage.vConsole_switch_x, '8');
56+
57+
global.localStorage = null;
58+
window.localStorage = null;
59+
done();
60+
}, 100);
61+
}, {
62+
features: {
63+
FetchExternalResources: ["link", "script"]
64+
}
65+
});
66+
});
67+
68+
it('log.html', function(done) {
69+
jsdom.env('test/log.html', function(err, window) {
70+
var document = window.document;
71+
72+
assert.equal(document.querySelector('#__vconsole') !== null, true);
73+
74+
document.querySelector('.weui_btn.weui_btn_default:nth-of-type(1)').click(); // formattedLog
75+
assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-item .vc-item-content').innerHTML, ' formattedLog() Start');
76+
77+
document.querySelector('.vc-toolbar .vc-tool.vc-tool-default:nth-of-type(1)').click(); // clear
78+
assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-item .vc-item-content'), null);
79+
80+
document.querySelector('.weui_btn.weui_btn_default:nth-of-type(2)').click(); // normalObject
81+
assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-item .vc-item-content').innerHTML, ' normalObject() Start');
82+
83+
document.querySelector('.vc-toolbar .vc-tool.vc-tool-default:nth-of-type(1)').click(); // clear
84+
assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-item .vc-item-content'), null);
85+
86+
done();
87+
}, {
88+
features: {
89+
FetchExternalResources: ["link", "script"]
90+
}
91+
});
92+
});
93+
94+
it('plugin.html', function(done) {
95+
jsdom.env('test/plugin.html', function(err, window) {
96+
var document = window.document;
97+
assert.equal(document.querySelector('#__vconsole') !== null, true);
98+
99+
document.querySelector('.page a:nth-of-type(1)').click(); // newTab
100+
assert.equal(document.querySelector('.vc-tabbar .vc-tab:nth-of-type(4)').innerHTML, 'Tab1');
101+
102+
done();
103+
}, {
104+
features: {
105+
FetchExternalResources: ["link", "script"]
106+
}
107+
});
108+
});
109+
110+
it('ajax.html', function(done) {
111+
this.timeout(2000);
112+
113+
jsdom.env('test/ajax.html', function(err, window) {
114+
var document = window.document;
115+
assert.equal(document.querySelector('#__vconsole') !== null, true);
116+
117+
document.querySelector('.page a:nth-of-type(1)').click(); // asyncAjax
118+
setTimeout(function () {
119+
assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-fold-outer').innerHTML, 'Object {ret: 0, msg: "ok"}');
120+
done();
121+
}, 10)
122+
123+
}, {
124+
features: {
125+
FetchExternalResources: ["link", "script"]
126+
}
127+
});
128+
});
129+
130+
});

0 commit comments

Comments
 (0)