Skip to content

Commit 8bec86b

Browse files
committed
Tests for static methods of WebStorageExplorer class
1 parent 02ff971 commit 8bec86b

File tree

2 files changed

+123
-4
lines changed

2 files changed

+123
-4
lines changed

js/panel.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,12 @@ class WebStorageExplorer {
440440
}
441441
}
442442

443-
$(document).ready(() => {
444-
let App = new WebStorageExplorer();
445-
App.init();
446-
});
443+
// Tiny UMD
444+
if(typeof exports !== 'undefined') {
445+
module.exports = WebStorageExplorer;
446+
} else {
447+
$(document).ready(() => {
448+
let App = new WebStorageExplorer();
449+
App.init();
450+
});
451+
}

tests/panel-class.test.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
const chai = require('chai');
2+
const expect = chai.expect;
3+
const PanelClass = require('../js/panel');
4+
5+
describe('Test static methods of WebStorageExplorer class', () => {
6+
describe('tryParseJSON', () => {
7+
it('should return an empty object for "{}"', () => {
8+
const result = PanelClass.tryParseJSON('{}');
9+
expect(JSON.stringify(result)).to.be.equal('{}');
10+
});
11+
12+
it('should parse usual JSON successfully', () => {
13+
const result = PanelClass.tryParseJSON('{ "a" : 123, "b" : true, "c": "abc"}');
14+
expect(result).to.be.deep.equal({a: 123, b: true, c: 'abc'});
15+
});
16+
17+
it('should return null for broken JSON', () => {
18+
const result = PanelClass.tryParseJSON('{ a : 123, b : true, c: "abc"}'); //no quotes
19+
expect(result).to.be.a.null;
20+
});
21+
22+
it('should return null for string', () => {
23+
const result = PanelClass.tryParseJSON('hello');
24+
expect(result).to.be.a.null;
25+
});
26+
27+
it('should return number for number', () => {
28+
const result = PanelClass.tryParseJSON(123);
29+
expect(result).to.be.equal(123);
30+
});
31+
32+
it('should return null for null', () => {
33+
const result = PanelClass.tryParseJSON(null);
34+
expect(result).to.be.null;
35+
});
36+
37+
it('should return true for true', () => {
38+
const result = PanelClass.tryParseJSON(true);
39+
expect(result).to.be.true;
40+
});
41+
42+
it('should return false for false', () => {
43+
const result = PanelClass.tryParseJSON(true);
44+
expect(result).to.be.true;
45+
});
46+
});
47+
48+
describe('guessType', () => {
49+
it('should return object for {}', () => {
50+
const result = PanelClass.guessType({});
51+
expect(result).to.be.equal('object');
52+
});
53+
54+
it('should return null for null', () => {
55+
const result = PanelClass.guessType(null);
56+
expect(result).to.be.equal('null');
57+
});
58+
59+
it('should return array for []', () => {
60+
const result = PanelClass.guessType([]);
61+
expect(result).to.be.equal('array');
62+
});
63+
64+
it('should return boolean for true', () => {
65+
const result = PanelClass.guessType(true);
66+
expect(result).to.be.equal('boolean');
67+
});
68+
69+
it('should return boolean for false', () => {
70+
const result = PanelClass.guessType(false);
71+
expect(result).to.be.equal('boolean');
72+
});
73+
74+
it('should return string for normal string', () => {
75+
const result = PanelClass.guessType('hello');
76+
expect(result).to.be.equal('string');
77+
});
78+
79+
it('should return string for empty string', () => {
80+
const result = PanelClass.guessType('');
81+
expect(result).to.be.equal('string');
82+
});
83+
84+
it('should return number for a number', () => {
85+
const result = PanelClass.guessType(123);
86+
expect(result).to.be.equal('number');
87+
});
88+
89+
it('should return number for a string number', () => {
90+
const result = PanelClass.guessType('321');
91+
expect(result).to.be.equal('number');
92+
});
93+
94+
it('should return string for string "111a"', () => {
95+
const result = PanelClass.guessType('111a');
96+
expect(result).to.be.equal('string');
97+
});
98+
99+
it('should return other for NaN', () => {
100+
const result = PanelClass.guessType(NaN);
101+
expect(result).to.be.equal('other');
102+
});
103+
104+
it('should return other for Infinity', () => {
105+
const result = PanelClass.guessType(Infinity);
106+
expect(result).to.be.equal('other');
107+
});
108+
109+
it('should return other for a function', () => {
110+
const result = PanelClass.guessType(() => {});
111+
expect(result).to.be.equal('other');
112+
});
113+
});
114+
});

0 commit comments

Comments
 (0)