@@ -4,57 +4,43 @@ const domToReact = require('../lib/dom-to-react');
4
4
const { data, render } = require ( './helpers/' ) ;
5
5
const utilities = require ( '../lib/utilities' ) ;
6
6
7
- describe ( 'DOM to React ' , ( ) => {
7
+ describe ( 'domToReact ' , ( ) => {
8
8
it ( 'converts single DOM node to React' , ( ) => {
9
- const html = data . html . single ;
10
- const reactElement = domToReact ( htmlToDOM ( html ) ) ;
11
-
9
+ const reactElement = domToReact ( htmlToDOM ( data . html . single ) ) ;
12
10
expect ( reactElement ) . toMatchSnapshot ( ) ;
13
11
} ) ;
14
12
15
13
it ( 'converts multiple DOM nodes to React' , ( ) => {
16
- const html = data . html . multiple ;
17
- const reactElements = domToReact ( htmlToDOM ( html ) ) ;
18
-
14
+ const reactElements = domToReact ( htmlToDOM ( data . html . multiple ) ) ;
19
15
reactElements . forEach ( ( reactElement , index ) => {
20
16
expect ( reactElement . key ) . toBe ( String ( index ) ) ;
21
17
} ) ;
22
-
23
18
expect ( reactElements ) . toMatchSnapshot ( ) ;
24
19
} ) ;
25
20
26
- // https://reactjs.org/docs/forms.html#the-textarea-tag
27
21
it ( 'converts <textarea> correctly' , ( ) => {
28
- const html = data . html . textarea ;
29
- const reactElement = domToReact ( htmlToDOM ( html ) ) ;
30
-
22
+ // https://reactjs.org/docs/forms.html#the-textarea-tag
23
+ const reactElement = domToReact ( htmlToDOM ( data . html . textarea ) ) ;
31
24
expect ( reactElement ) . toMatchSnapshot ( ) ;
32
25
} ) ;
33
26
34
27
it ( 'does not escape <script> content' , ( ) => {
35
- const html = data . html . script ;
36
- const reactElement = domToReact ( htmlToDOM ( html ) ) ;
37
-
28
+ const reactElement = domToReact ( htmlToDOM ( data . html . script ) ) ;
38
29
expect ( reactElement ) . toMatchSnapshot ( ) ;
39
30
} ) ;
40
31
41
32
it ( 'does not escape <style> content' , ( ) => {
42
- const html = data . html . style ;
43
- const reactElement = domToReact ( htmlToDOM ( html ) ) ;
44
-
33
+ const reactElement = domToReact ( htmlToDOM ( data . html . style ) ) ;
45
34
expect ( reactElement ) . toMatchSnapshot ( ) ;
46
35
} ) ;
47
36
48
37
it ( 'does not have `children` for void elements' , ( ) => {
49
- const html = data . html . img ;
50
- const reactElement = domToReact ( htmlToDOM ( html ) ) ;
51
-
38
+ const reactElement = domToReact ( htmlToDOM ( data . html . img ) ) ;
52
39
expect ( reactElement . props . children ) . toBe ( null ) ;
53
40
} ) ;
54
41
55
42
it ( 'does not throw an error for void elements' , ( ) => {
56
- const html = data . html . void ;
57
- const reactElements = domToReact ( htmlToDOM ( html ) ) ;
43
+ const reactElements = domToReact ( htmlToDOM ( data . html . void ) ) ;
58
44
expect ( ( ) => {
59
45
render ( React . createElement ( 'div' , { } , reactElements ) ) ;
60
46
} ) . not . toThrow ( ) ;
@@ -76,90 +62,83 @@ describe('DOM to React', () => {
76
62
} ) ;
77
63
78
64
it ( 'converts SVG element with viewBox attribute' , ( ) => {
79
- const html = data . svg . simple ;
80
65
const reactElement = domToReact (
81
- htmlToDOM ( html , { lowerCaseAttributeNames : false } )
66
+ htmlToDOM ( data . svg . simple , { lowerCaseAttributeNames : false } )
82
67
) ;
83
-
84
68
expect ( reactElement ) . toMatchSnapshot ( ) ;
85
69
} ) ;
86
70
87
71
it ( 'does not modify attributes on custom elements' , ( ) => {
88
- const html = data . html . customElement ;
89
- const reactElement = domToReact ( htmlToDOM ( html ) ) ;
90
-
72
+ const reactElement = domToReact ( htmlToDOM ( data . html . customElement ) ) ;
91
73
expect ( reactElement ) . toMatchSnapshot ( ) ;
92
74
} ) ;
75
+ } ) ;
93
76
94
- describe ( 'library' , ( ) => {
95
- const Preact = require ( 'preact' ) ;
96
-
97
- it ( 'converts with React (default)' , ( ) => {
98
- const reactElement = domToReact ( htmlToDOM ( data . html . single ) ) ;
77
+ describe ( 'domToReact with library option' , ( ) => {
78
+ const Preact = require ( 'preact' ) ;
99
79
100
- expect ( React . isValidElement ( reactElement ) ) . toBe ( true ) ;
101
- expect ( Preact . isValidElement ( reactElement ) ) . toBe ( false ) ;
80
+ it ( 'converts with React by default' , ( ) => {
81
+ const reactElement = domToReact ( htmlToDOM ( data . html . single ) ) ;
82
+ expect ( React . isValidElement ( reactElement ) ) . toBe ( true ) ;
83
+ expect ( Preact . isValidElement ( reactElement ) ) . toBe ( false ) ;
84
+ expect ( reactElement ) . toEqual ( React . createElement ( 'p' , { } , 'foo' ) ) ;
85
+ } ) ;
102
86
103
- expect ( reactElement ) . toEqual ( React . createElement ( 'p' , { } , 'foo' ) ) ;
87
+ it ( 'converts with Preact' , ( ) => {
88
+ const parsedElement = domToReact ( htmlToDOM ( data . html . single ) , {
89
+ library : Preact
104
90
} ) ;
91
+ const preactElement = Preact . createElement ( 'p' , { } , 'foo' ) ;
92
+ expect ( React . isValidElement ( parsedElement ) ) . toBe ( false ) ;
93
+ expect ( Preact . isValidElement ( parsedElement ) ) . toBe ( true ) ;
94
+ // remove `__v` key since it's causing test equality to fail
95
+ delete parsedElement . __v ;
96
+ delete preactElement . __v ;
97
+ expect ( parsedElement ) . toEqual ( preactElement ) ;
98
+ } ) ;
99
+ } ) ;
105
100
106
- it ( 'converts with Preact instead of React' , ( ) => {
107
- const parsedElement = domToReact ( htmlToDOM ( data . html . single ) , {
108
- library : Preact
109
- } ) ;
110
- const preactElement = Preact . createElement ( 'p' , { } , 'foo' ) ;
111
-
112
- expect ( React . isValidElement ( parsedElement ) ) . toBe ( false ) ;
113
- expect ( Preact . isValidElement ( parsedElement ) ) . toBe ( true ) ;
114
-
115
- // remove `__v` key since it's causing test equality to fail
116
- delete parsedElement . __v ;
117
- delete preactElement . __v ;
118
- expect ( parsedElement ) . toEqual ( preactElement ) ;
101
+ describe ( 'domToReact replace option' , ( ) => {
102
+ it ( "does not set key if there's a single node" , ( ) => {
103
+ const replaceElement = React . createElement ( 'p' ) ;
104
+ const reactElement = domToReact ( htmlToDOM ( data . html . single ) , {
105
+ replace : ( ) => replaceElement
119
106
} ) ;
107
+ expect ( reactElement . key ) . toBe ( null ) ;
120
108
} ) ;
121
109
122
- describe ( 'replace' , ( ) => {
123
- it ( "does not set key if there's a single node" , ( ) => {
124
- const replaceElement = React . createElement ( 'p' ) ;
125
- const reactElement = domToReact ( htmlToDOM ( data . html . single ) , {
126
- replace : ( ) => replaceElement
127
- } ) ;
128
- expect ( reactElement . key ) . toBe ( null ) ;
129
- } ) ;
110
+ it ( "does not modify keys if it's already set" , ( ) => {
111
+ const html = data . html . single + data . html . customElement ;
130
112
131
- it ( "does not modify keys if it's already set" , ( ) => {
132
- const html = data . html . single + data . html . customElement ;
133
-
134
- const reactElements = domToReact ( htmlToDOM ( html ) , {
135
- replace : node => {
136
- if ( node . name === 'p' ) {
137
- return React . createElement ( 'p' , { } , 'replaced foo' ) ;
138
- }
139
- if ( node . name === 'custom-button' ) {
140
- return React . createElement (
141
- 'custom-button' ,
142
- {
143
- key : 'myKey' ,
144
- class : 'myClass' ,
145
- 'custom-attribute' : 'replaced value'
146
- } ,
147
- null
148
- ) ;
149
- }
113
+ const reactElements = domToReact ( htmlToDOM ( html ) , {
114
+ replace : node => {
115
+ if ( node . name === 'p' ) {
116
+ return React . createElement ( 'p' , { } , 'replaced foo' ) ;
150
117
}
151
- } ) ;
152
-
153
- expect ( reactElements [ 0 ] . key ) . toBe ( '0' ) ;
154
- expect ( reactElements [ 1 ] . key ) . toBe ( 'myKey' ) ;
118
+ if ( node . name === 'custom-button' ) {
119
+ return React . createElement (
120
+ 'custom-button' ,
121
+ {
122
+ key : 'myKey' ,
123
+ class : 'myClass' ,
124
+ 'custom-attribute' : 'replaced value'
125
+ } ,
126
+ null
127
+ ) ;
128
+ }
129
+ }
155
130
} ) ;
131
+
132
+ expect ( reactElements [ 0 ] . key ) . toBe ( '0' ) ;
133
+ expect ( reactElements [ 1 ] . key ) . toBe ( 'myKey' ) ;
156
134
} ) ;
135
+ } ) ;
157
136
137
+ describe ( 'domToReact' , ( ) => {
158
138
describe ( 'when React >=16' , ( ) => {
159
139
it ( 'preserves unknown attributes' , ( ) => {
160
140
const html = data . html . customElement ;
161
141
const reactElement = domToReact ( htmlToDOM ( html ) ) ;
162
-
163
142
expect ( reactElement ) . toMatchSnapshot ( ) ;
164
143
} ) ;
165
144
} ) ;
0 commit comments