11'use strict' 
22
3- import  test  from   'ava' 
4- import  HtmlTableToJson  from   './' 
3+ const  test  =   require ( 'ava' ) 
4+ const  HtmlTableToJson  =   require ( './' ) 
55
66const  singleEmpty  =  `<table></table>` 
77const  doubleEmpty  =  `${ singleEmpty } ${ singleEmpty }  ` 
@@ -21,7 +21,7 @@ test('double table count is 2', t => {
2121
2222const  singleWithHeaderRow  =  `<table><tr><th>${ h1 }  </th><th>${ h2 }  </th><th>${ h3 }  </th><th>${ h4 }  </th></tr></table>` 
2323
24- test ( 'extracts headers correctly' ,  t  =>  { 
24+ test ( 'extracts th  headers correctly' ,  t  =>  { 
2525  const  toString  =  value  =>  value  +  '' 
2626
2727  const  headers  =  new  HtmlTableToJson ( singleWithHeaderRow ) . headers [ 0 ] 
@@ -32,10 +32,70 @@ test('extracts headers correctly', t => {
3232  t . is ( headers [ 3 ] ,  toString ( h4 ) ) 
3333} ) 
3434
35+ const  singleWithoutHeaderRow  =  `<table> 
36+                                   <tr><td>${ h1 }  </td><td>${ h2 }  </td><td>${ h3 }  </td><td>${ h4 }  </td></tr> 
37+                                   <tr><td>A</td><td>B</td><td>C</td><td>D</td></tr> 
38+                                 </table>` 
39+ 
40+ test ( 'falls back to first row for headers' ,  t  =>  { 
41+   const  toString  =  value  =>  value  +  '' 
42+ 
43+   const  _sut  =  new  HtmlTableToJson ( singleWithoutHeaderRow ) 
44+ 
45+   const  headers  =  _sut . headers [ 0 ] 
46+   t . is ( headers [ 0 ] ,  toString ( h1 ) ) 
47+   t . is ( headers [ 1 ] ,  toString ( h2 ) ) 
48+   t . is ( headers [ 2 ] ,  toString ( h3 ) ) 
49+   t . is ( headers [ 3 ] ,  toString ( h4 ) ) 
50+ 
51+   const  rows  =  _sut . results [ 0 ] 
52+   t . is ( rows [ 0 ] [ headers [ 0 ] ] ,  'A' ) 
53+   t . is ( rows [ 0 ] [ headers [ 1 ] ] ,  'B' ) 
54+   t . is ( rows [ 0 ] [ headers [ 2 ] ] ,  'C' ) 
55+   t . is ( rows [ 0 ] [ headers [ 3 ] ] ,  'D' ) 
56+ } ) 
57+ 
3558test ( 'throws when html is not a string' ,  t  =>  { 
3659  t . throws ( ( )  =>  new  HtmlTableToJson ( { } ) ) 
3760  t . throws ( ( )  =>  new  HtmlTableToJson ( [ ] ) ) 
3861  t . throws ( ( )  =>  new  HtmlTableToJson ( 1 ) ) 
3962  t . throws ( ( )  =>  new  HtmlTableToJson ( NaN ) ) 
4063  t . throws ( ( )  =>  new  HtmlTableToJson ( true ) ) 
4164} ) 
65+ 
66+ test ( 'parse method' ,  t  =>  { 
67+   const  _sut  =  HtmlTableToJson . parse ( doubleEmpty ) 
68+ 
69+   t . is ( _sut . count ,  2 ) 
70+ } ) 
71+ 
72+ const  sampleTable  =  `<table> 
73+                         <tr> 
74+                             <th>Animal</th> 
75+                             <th>Color</th> 
76+                             <th>Name</th> 
77+                         </tr> 
78+                         <tr> 
79+                             <td>Unicorn</td> 
80+                             <td>Pink</td> 
81+                             <td>Billy</td> 
82+                         </tr> 
83+                         <tr> 
84+                             <td>Walrus</td> 
85+                             <td>Orange</td> 
86+                             <td>Sue</td> 
87+                         </tr> 
88+                     </table>` 
89+ 
90+ test ( 'values option' ,  t  =>  { 
91+   const  _sut  =  new  HtmlTableToJson ( sampleTable ,  {  values : true  } ) 
92+ 
93+   t . is ( _sut . count ,  1 ) 
94+ 
95+   const  results  =  _sut . results [ 0 ] 
96+   t . is ( results . length ,  2 ) 
97+   t . true ( Array . isArray ( results [ 0 ] ) ) 
98+   t . true ( Array . isArray ( results [ 1 ] ) ) 
99+   t . is ( results [ 0 ] [ 0 ] ,  'Unicorn' ) 
100+   t . is ( results [ 1 ] [ 2 ] ,  'Sue' ) 
101+ } ) 
0 commit comments