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