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