@@ -11,7 +11,7 @@ describe('DOM to React', () => {
11
11
const reactElement = domToReact ( htmlToDOM ( html ) ) ;
12
12
13
13
assert ( React . isValidElement ( reactElement ) ) ;
14
- assert . deepEqual ( reactElement , React . createElement ( 'p' , { } , 'foo' ) ) ;
14
+ expect ( reactElement ) . toMatchSnapshot ( ) ;
15
15
} ) ;
16
16
17
17
it ( 'converts multiple DOM nodes to React' , ( ) => {
@@ -23,10 +23,7 @@ describe('DOM to React', () => {
23
23
assert ( reactElement . key ) ;
24
24
} ) ;
25
25
26
- assert . deepEqual ( reactElements , [
27
- React . createElement ( 'p' , { key : 0 } , 'foo' ) ,
28
- React . createElement ( 'p' , { key : 1 } , 'bar' )
29
- ] ) ;
26
+ expect ( reactElements ) . toMatchSnapshot ( ) ;
30
27
} ) ;
31
28
32
29
// https://reactjs.org/docs/forms.html#the-textarea-tag
@@ -35,54 +32,23 @@ describe('DOM to React', () => {
35
32
const reactElement = domToReact ( htmlToDOM ( html ) ) ;
36
33
37
34
assert ( React . isValidElement ( reactElement ) ) ;
38
- assert . deepEqual (
39
- reactElement ,
40
- React . createElement (
41
- 'textarea' ,
42
- {
43
- defaultValue : 'foo'
44
- } ,
45
- null
46
- )
47
- ) ;
35
+ expect ( reactElement ) . toMatchSnapshot ( ) ;
48
36
} ) ;
49
37
50
38
it ( 'does not escape <script> content' , ( ) => {
51
39
const html = data . html . script ;
52
40
const reactElement = domToReact ( htmlToDOM ( html ) ) ;
53
41
54
42
assert ( React . isValidElement ( reactElement ) ) ;
55
- assert . deepEqual (
56
- reactElement ,
57
- React . createElement (
58
- 'script' ,
59
- {
60
- dangerouslySetInnerHTML : {
61
- __html : 'alert(1 < 2);'
62
- }
63
- } ,
64
- null
65
- )
66
- ) ;
43
+ expect ( reactElement ) . toMatchSnapshot ( ) ;
67
44
} ) ;
68
45
69
46
it ( 'does not escape <style> content' , ( ) => {
70
47
const html = data . html . style ;
71
48
const reactElement = domToReact ( htmlToDOM ( html ) ) ;
72
49
73
50
assert ( React . isValidElement ( reactElement ) ) ;
74
- assert . deepEqual (
75
- reactElement ,
76
- React . createElement (
77
- 'style' ,
78
- {
79
- dangerouslySetInnerHTML : {
80
- __html : 'body > .foo { color: #f00; }'
81
- }
82
- } ,
83
- null
84
- )
85
- ) ;
51
+ expect ( reactElement ) . toMatchSnapshot ( ) ;
86
52
} ) ;
87
53
88
54
it ( 'does not have `children` for void elements' , ( ) => {
@@ -108,17 +74,12 @@ describe('DOM to React', () => {
108
74
] . join ( '' ) ;
109
75
110
76
const reactElements = domToReact ( htmlToDOM ( html ) ) ;
111
- reactElements . forEach ( reactElement => {
112
- assert ( React . isValidElement ( reactElement ) ) ;
113
- assert ( reactElement . key ) ;
77
+ reactElements . forEach ( ( reactElement , index ) => {
78
+ assert . strictEqual ( React . isValidElement ( reactElement ) , true ) ;
79
+ assert ( reactElement . key , String ( index ) ) ;
114
80
} ) ;
115
81
116
- assert . deepEqual ( reactElements , [
117
- // doctype
118
- React . createElement ( 'p' , { key : 1 } , 'foo' ) ,
119
- // comment is in the middle
120
- React . createElement ( 'p' , { key : 3 } , 'foo' )
121
- ] ) ;
82
+ expect ( reactElements ) . toMatchSnapshot ( ) ;
122
83
} ) ;
123
84
124
85
it ( 'converts SVG element with viewBox attribute' , ( ) => {
@@ -127,32 +88,26 @@ describe('DOM to React', () => {
127
88
htmlToDOM ( html , { lowerCaseAttributeNames : false } )
128
89
) ;
129
90
130
- assert . deepEqual (
131
- reactElement ,
132
- React . createElement ( 'svg' , { viewBox : '0 0 512 512' , id : 'foo' } , 'Inner' )
133
- ) ;
91
+ expect ( reactElement ) . toMatchSnapshot ( ) ;
134
92
} ) ;
135
93
136
94
it ( 'does not modify attributes on custom elements' , ( ) => {
137
95
const html = data . html . customElement ;
138
96
const reactElement = domToReact ( htmlToDOM ( html ) ) ;
139
97
140
- assert . deepEqual (
141
- reactElement ,
142
- React . createElement (
143
- 'custom-button' ,
144
- {
145
- class : 'myClass' ,
146
- 'custom-attribute' : 'value'
147
- } ,
148
- null
149
- )
150
- ) ;
98
+ expect ( reactElement ) . toMatchSnapshot ( ) ;
151
99
} ) ;
152
100
153
101
describe ( 'library' , ( ) => {
154
102
const Preact = require ( 'preact' ) ;
155
103
104
+ it ( 'converts with React (default)' , ( ) => {
105
+ const html = data . html . single ;
106
+ const reactElement = domToReact ( htmlToDOM ( html ) ) ;
107
+
108
+ assert . deepEqual ( reactElement , React . createElement ( 'p' , { } , 'foo' ) ) ;
109
+ } ) ;
110
+
156
111
it ( 'converts with Preact instead of React' , ( ) => {
157
112
const html = data . html . single ;
158
113
const preactElement = domToReact ( htmlToDOM ( html ) , { library : Preact } ) ;
@@ -162,16 +117,16 @@ describe('DOM to React', () => {
162
117
} ) ;
163
118
164
119
describe ( 'replace' , ( ) => {
165
- it ( "does not set key if there's 1 node" , ( ) => {
120
+ it ( "does not set key if there's a single node" , ( ) => {
166
121
const replaceElement = React . createElement ( 'p' ) ;
167
122
const reactElement = domToReact ( htmlToDOM ( data . html . single ) , {
168
123
replace : ( ) => replaceElement
169
124
} ) ;
170
- assert . deepEqual ( reactElement , replaceElement ) ;
125
+ assert . strictEqual ( reactElement . key , null ) ;
171
126
} ) ;
172
127
173
128
it ( "does not modify keys if it's already set" , ( ) => {
174
- const html = [ data . html . single , data . html . customElement ] . join ( '' ) ;
129
+ const html = data . html . single + data . html . customElement ;
175
130
176
131
const reactElements = domToReact ( htmlToDOM ( html ) , {
177
132
replace : node => {
@@ -192,18 +147,17 @@ describe('DOM to React', () => {
192
147
}
193
148
} ) ;
194
149
195
- assert . deepEqual ( reactElements , [
196
- React . createElement ( 'p' , { key : 0 } , 'replaced foo' ) ,
197
- React . createElement (
198
- 'custom-button' ,
199
- {
200
- key : 'myKey' ,
201
- class : 'myClass' ,
202
- 'custom-attribute' : 'replaced value'
203
- } ,
204
- null
205
- )
206
- ] ) ;
150
+ assert . strictEqual ( reactElements [ 0 ] . key , '0' ) ;
151
+ assert . strictEqual ( reactElements [ 1 ] . key , 'myKey' ) ;
152
+ } ) ;
153
+ } ) ;
154
+
155
+ describe ( 'when React >=16' , ( ) => {
156
+ it ( 'preserves unknown attributes' , ( ) => {
157
+ const html = data . html . customElement ;
158
+ const reactElement = domToReact ( htmlToDOM ( html ) ) ;
159
+
160
+ expect ( reactElement ) . toMatchSnapshot ( ) ;
207
161
} ) ;
208
162
} ) ;
209
163
@@ -218,14 +172,11 @@ describe('DOM to React', () => {
218
172
utilities . PRESERVE_CUSTOM_ATTRIBUTES = PRESERVE_CUSTOM_ATTRIBUTES ;
219
173
} ) ;
220
174
221
- it ( 'modifies attributes on custom elements ' , ( ) => {
175
+ it ( 'removes unknown attributes ' , ( ) => {
222
176
const html = data . html . customElement ;
223
177
const reactElement = domToReact ( htmlToDOM ( html ) ) ;
224
178
225
- assert . deepEqual (
226
- reactElement ,
227
- React . createElement ( 'custom-button' , { className : 'myClass' } , null )
228
- ) ;
179
+ expect ( reactElement ) . toMatchSnapshot ( ) ;
229
180
} ) ;
230
181
} ) ;
231
182
} ) ;
0 commit comments