1
- const assert = require ( 'assert' ) ;
2
1
const React = require ( 'react' ) ;
3
2
const parse = require ( '..' ) ;
4
3
const { data, render } = require ( './helpers/' ) ;
5
4
6
5
describe ( 'HTML to React' , ( ) => {
7
6
describe ( 'exports' , ( ) => {
8
7
it ( 'has default ES Module' , ( ) => {
9
- assert . strictEqual ( parse . default , parse ) ;
8
+ expect ( parse . default ) . toBe ( parse ) ;
10
9
} ) ;
11
10
12
11
it ( 'has domToReact' , ( ) => {
13
- assert . strictEqual ( parse . domToReact , require ( '../lib/dom-to-react' ) ) ;
12
+ expect ( parse . domToReact ) . toBe ( require ( '../lib/dom-to-react' ) ) ;
14
13
} ) ;
15
14
16
15
it ( 'has htmlToDOM' , ( ) => {
17
- assert . strictEqual ( parse . htmlToDOM , require ( 'html-dom-parser' ) ) ;
16
+ expect ( parse . htmlToDOM ) . toBe ( require ( 'html-dom-parser' ) ) ;
18
17
} ) ;
19
18
} ) ;
20
19
21
20
describe ( 'parser' , ( ) => {
22
21
[ undefined , null , { } , [ ] , 42 ] . forEach ( value => {
23
22
it ( `throws an error if first argument is ${ value } ` , ( ) => {
24
- assert . throws ( ( ) => {
23
+ expect ( ( ) => {
25
24
parse ( value ) ;
26
- } , TypeError ) ;
25
+ } ) . toThrow ( TypeError ) ;
27
26
} ) ;
28
27
} ) ;
29
28
30
29
it ( 'converts empty string to empty array' , ( ) => {
31
- assert . deepEqual ( parse ( '' ) , [ ] ) ;
30
+ expect ( parse ( '' ) ) . toEqual ( [ ] ) ;
32
31
} ) ;
33
32
34
33
it ( 'returns string if it cannot be parsed as HTML' , ( ) => {
35
- assert . strictEqual ( parse ( 'foo' ) , 'foo' ) ;
34
+ expect ( parse ( 'foo' ) ) . toBe ( 'foo' ) ;
36
35
} ) ;
37
36
38
37
it ( 'converts single HTML element to React' , ( ) => {
39
38
const html = data . html . single ;
40
39
const reactElement = parse ( html ) ;
41
- assert . strictEqual ( render ( reactElement ) , html ) ;
40
+
41
+ expect ( render ( reactElement ) ) . toBe ( html ) ;
42
42
} ) ;
43
43
44
44
it ( 'converts single HTML element and ignores comment' , ( ) => {
45
45
const html = data . html . single ;
46
46
// comment should be ignored
47
47
const reactElement = parse ( html + data . html . comment ) ;
48
- assert . strictEqual ( render ( reactElement ) , html ) ;
48
+
49
+ expect ( render ( reactElement ) ) . toBe ( html ) ;
49
50
} ) ;
50
51
51
52
it ( 'converts multiple HTML elements to React' , ( ) => {
52
53
const html = data . html . multiple ;
53
54
const reactElements = parse ( html ) ;
54
- assert . strictEqual (
55
- render ( React . createElement ( 'div' , { } , reactElements ) ) ,
56
- '<div>' + html + '</div>'
57
- ) ;
55
+
56
+ expect (
57
+ render ( React . createElement ( React . Fragment , { } , reactElements ) )
58
+ ) . toBe ( html ) ;
58
59
} ) ;
59
60
60
61
it ( 'converts complex HTML to React' , ( ) => {
61
62
const html = data . html . complex ;
62
63
const reactElement = parse ( data . html . doctype + html ) ;
63
- assert . strictEqual ( render ( reactElement ) , html ) ;
64
+
65
+ expect ( render ( reactElement ) ) . toBe ( html ) ;
64
66
} ) ;
65
67
66
68
it ( 'converts empty <script> to React' , ( ) => {
67
69
const html = '<script></script>' ;
68
70
const reactElement = parse ( html ) ;
69
- assert . strictEqual ( render ( reactElement ) , html ) ;
71
+
72
+ expect ( render ( reactElement ) ) . toBe ( html ) ;
70
73
} ) ;
71
74
72
75
it ( 'converts empty <style> to React' , ( ) => {
73
76
const html = '<style></style>' ;
74
77
const reactElement = parse ( html ) ;
75
- assert . strictEqual ( render ( reactElement ) , html ) ;
78
+
79
+ expect ( render ( reactElement ) ) . toBe ( html ) ;
76
80
} ) ;
77
81
78
82
it ( 'converts SVG to React' , ( ) => {
79
83
const svg = data . svg . complex ;
80
84
const reactElement = parse ( svg ) ;
81
- assert . strictEqual ( render ( reactElement ) , svg ) ;
85
+
86
+ expect ( render ( reactElement ) ) . toBe ( svg ) ;
82
87
} ) ;
83
88
84
89
it ( 'decodes HTML entities' , ( ) => {
85
90
const encodedEntities = 'asdf & ÿ ü '' ;
86
91
const decodedEntities = "asdf & ÿ ü '" ;
87
92
const reactElement = parse ( '<i>' + encodedEntities + '</i>' ) ;
88
- assert . strictEqual ( reactElement . props . children , decodedEntities ) ;
93
+
94
+ expect ( reactElement . props . children ) . toBe ( decodedEntities ) ;
89
95
} ) ;
90
96
} ) ;
91
97
@@ -100,15 +106,15 @@ describe('HTML to React', () => {
100
106
}
101
107
}
102
108
} ) ;
103
- assert . strictEqual (
104
- render ( reactElement ) ,
109
+
110
+ expect ( render ( reactElement ) ) . toBe (
105
111
html . replace ( '<title>Title</title>' , '<title>Replaced Title</title>' )
106
112
) ;
107
113
} ) ;
108
114
109
115
it ( 'does not override the element if an invalid React element is returned' , ( ) => {
110
116
const html = data . html . complex ;
111
- const reactElement = parse ( html , {
117
+ const options = {
112
118
replace : node => {
113
119
if ( node . attribs && node . attribs . id === 'header' ) {
114
120
return {
@@ -117,14 +123,10 @@ describe('HTML to React', () => {
117
123
} ;
118
124
}
119
125
}
120
- } ) ;
121
- assert . notEqual (
122
- render ( reactElement ) ,
123
- html . replace (
124
- '<header id="header">Header</header>' ,
125
- '<h1>Heading</h1>'
126
- )
127
- ) ;
126
+ } ;
127
+ const reactElement = parse ( html , options ) ;
128
+
129
+ expect ( render ( reactElement ) ) . toBe ( html ) ;
128
130
} ) ;
129
131
} ) ;
130
132
@@ -134,7 +136,8 @@ describe('HTML to React', () => {
134
136
const html = data . html . single ;
135
137
const options = { library : Preact } ;
136
138
const preactElement = parse ( html , options ) ;
137
- assert . deepEqual ( preactElement , Preact . createElement ( 'p' , { } , 'foo' ) ) ;
139
+
140
+ expect ( preactElement ) . toEqual ( Preact . createElement ( 'p' , { } , 'foo' ) ) ;
138
141
} ) ;
139
142
} ) ;
140
143
@@ -146,10 +149,8 @@ describe('HTML to React', () => {
146
149
const html = '<ul><li/><li/></ul>' ;
147
150
const options = { htmlparser2 : { xmlMode : true } } ;
148
151
const reactElements = parse ( html , options ) ;
149
- assert . strictEqual (
150
- render ( reactElements ) ,
151
- '<ul><li></li><li></li></ul>'
152
- ) ;
152
+
153
+ expect ( render ( reactElements ) ) . toBe ( '<ul><li></li><li></li></ul>' ) ;
153
154
} ) ;
154
155
} ) ;
155
156
@@ -160,16 +161,17 @@ describe('HTML to React', () => {
160
161
</tbody>
161
162
</table>` ;
162
163
const reactElement = parse ( html ) ;
163
- assert . strictEqual ( render ( reactElement ) , html ) ;
164
+
165
+ expect ( render ( reactElement ) ) . toBe ( html ) ;
164
166
} ) ;
165
167
166
168
it ( 'removes whitespace text nodes when enabled' , ( ) => {
167
169
const html = `<table>
168
170
<tbody><tr><td> text </td><td> </td>\t</tr>\r</tbody>\n</table>` ;
169
171
const options = { trim : true } ;
170
172
const reactElement = parse ( html , options ) ;
171
- assert . strictEqual (
172
- render ( reactElement ) ,
173
+
174
+ expect ( render ( reactElement ) ) . toBe (
173
175
'<table><tbody><tr><td> text </td><td></td></tr></tbody></table>'
174
176
) ;
175
177
} ) ;
0 commit comments