11const React = require ( 'react' ) ;
22const htmlToDOM = require ( 'html-dom-parser' ) ;
3+
34const domToReact = require ( '../lib/dom-to-react' ) ;
4- const { data, render } = require ( './helpers/' ) ;
55const utilities = require ( '../lib/utilities' ) ;
66
7+ const { render } = require ( './helpers' ) ;
8+ const { html, svg } = require ( './data' ) ;
9+
710describe ( 'domToReact' , ( ) => {
811 it ( 'converts single DOM node to React' , ( ) => {
9- const reactElement = domToReact ( htmlToDOM ( data . html . single ) ) ;
12+ const reactElement = domToReact ( htmlToDOM ( html . single ) ) ;
1013 expect ( reactElement ) . toMatchSnapshot ( ) ;
1114 } ) ;
1215
1316 it ( 'converts multiple DOM nodes to React' , ( ) => {
14- const reactElements = domToReact ( htmlToDOM ( data . html . multiple ) ) ;
17+ const reactElements = domToReact ( htmlToDOM ( html . multiple ) ) ;
1518 reactElements . forEach ( ( reactElement , index ) => {
1619 expect ( reactElement . key ) . toBe ( String ( index ) ) ;
1720 } ) ;
@@ -20,41 +23,36 @@ describe('domToReact', () => {
2023
2124 it ( 'converts <textarea> correctly' , ( ) => {
2225 // https://reactjs.org/docs/forms.html#the-textarea-tag
23- const reactElement = domToReact ( htmlToDOM ( data . html . textarea ) ) ;
26+ const reactElement = domToReact ( htmlToDOM ( html . textarea ) ) ;
2427 expect ( reactElement ) . toMatchSnapshot ( ) ;
2528 } ) ;
2629
2730 it ( 'does not escape <script> content' , ( ) => {
28- const reactElement = domToReact ( htmlToDOM ( data . html . script ) ) ;
31+ const reactElement = domToReact ( htmlToDOM ( html . script ) ) ;
2932 expect ( reactElement ) . toMatchSnapshot ( ) ;
3033 } ) ;
3134
3235 it ( 'does not escape <style> content' , ( ) => {
33- const reactElement = domToReact ( htmlToDOM ( data . html . style ) ) ;
36+ const reactElement = domToReact ( htmlToDOM ( html . style ) ) ;
3437 expect ( reactElement ) . toMatchSnapshot ( ) ;
3538 } ) ;
3639
3740 it ( 'does not have `children` for void elements' , ( ) => {
38- const reactElement = domToReact ( htmlToDOM ( data . html . img ) ) ;
41+ const reactElement = domToReact ( htmlToDOM ( html . img ) ) ;
3942 expect ( reactElement . props . children ) . toBe ( null ) ;
4043 } ) ;
4144
4245 it ( 'does not throw an error for void elements' , ( ) => {
43- const reactElements = domToReact ( htmlToDOM ( data . html . void ) ) ;
46+ const reactElements = domToReact ( htmlToDOM ( html . void ) ) ;
4447 expect ( ( ) => {
4548 render ( React . createElement ( 'div' , { } , reactElements ) ) ;
4649 } ) . not . toThrow ( ) ;
4750 } ) ;
4851
4952 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+ ) ;
5856 expect ( reactElements ) . toHaveLength ( 2 ) ;
5957 expect ( reactElements [ 0 ] . key ) . toBe ( '1' ) ;
6058 expect ( reactElements [ 1 ] . key ) . toBe ( '3' ) ;
@@ -63,13 +61,13 @@ describe('domToReact', () => {
6361
6462 it ( 'converts SVG element with viewBox attribute' , ( ) => {
6563 const reactElement = domToReact (
66- htmlToDOM ( data . svg . simple , { lowerCaseAttributeNames : false } )
64+ htmlToDOM ( svg . simple , { lowerCaseAttributeNames : false } )
6765 ) ;
6866 expect ( reactElement ) . toMatchSnapshot ( ) ;
6967 } ) ;
7068
7169 it ( 'converts custom element with attributes' , ( ) => {
72- const reactElement = domToReact ( htmlToDOM ( data . html . customElement ) ) ;
70+ const reactElement = domToReact ( htmlToDOM ( html . customElement ) ) ;
7371 expect ( reactElement ) . toMatchSnapshot ( ) ;
7472 } ) ;
7573} ) ;
@@ -78,14 +76,14 @@ describe('domToReact with library option', () => {
7876 const Preact = require ( 'preact' ) ;
7977
8078 it ( 'converts with React by default' , ( ) => {
81- const reactElement = domToReact ( htmlToDOM ( data . html . single ) ) ;
79+ const reactElement = domToReact ( htmlToDOM ( html . single ) ) ;
8280 expect ( React . isValidElement ( reactElement ) ) . toBe ( true ) ;
8381 expect ( Preact . isValidElement ( reactElement ) ) . toBe ( false ) ;
8482 expect ( reactElement ) . toEqual ( React . createElement ( 'p' , { } , 'foo' ) ) ;
8583 } ) ;
8684
8785 it ( 'converts with Preact' , ( ) => {
88- const parsedElement = domToReact ( htmlToDOM ( data . html . single ) , {
86+ const parsedElement = domToReact ( htmlToDOM ( html . single ) , {
8987 library : Preact
9088 } ) ;
9189 const preactElement = Preact . createElement ( 'p' , { } , 'foo' ) ;
@@ -101,16 +99,14 @@ describe('domToReact with library option', () => {
10199describe ( 'domToReact replace option' , ( ) => {
102100 it ( "does not set key if there's a single node" , ( ) => {
103101 const replaceElement = React . createElement ( 'p' ) ;
104- const reactElement = domToReact ( htmlToDOM ( data . html . single ) , {
102+ const reactElement = domToReact ( htmlToDOM ( html . single ) , {
105103 replace : ( ) => replaceElement
106104 } ) ;
107105 expect ( reactElement . key ) . toBe ( null ) ;
108106 } ) ;
109107
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 = {
114110 replace : node => {
115111 if ( node . name === 'p' ) {
116112 return React . createElement ( 'p' , { } , 'replaced foo' ) ;
@@ -127,8 +123,12 @@ describe('domToReact replace option', () => {
127123 ) ;
128124 }
129125 }
130- } ) ;
126+ } ;
131127
128+ const reactElements = domToReact (
129+ htmlToDOM ( html . single + html . customElement ) ,
130+ options
131+ ) ;
132132 expect ( reactElements [ 0 ] . key ) . toBe ( '0' ) ;
133133 expect ( reactElements [ 1 ] . key ) . toBe ( 'myKey' ) ;
134134 } ) ;
@@ -137,8 +137,7 @@ describe('domToReact replace option', () => {
137137describe ( 'domToReact' , ( ) => {
138138 describe ( 'when React >=16' , ( ) => {
139139 it ( 'preserves unknown attributes' , ( ) => {
140- const html = data . html . customElement ;
141- const reactElement = domToReact ( htmlToDOM ( html ) ) ;
140+ const reactElement = domToReact ( htmlToDOM ( html . customElement ) ) ;
142141 expect ( reactElement ) . toMatchSnapshot ( ) ;
143142 } ) ;
144143 } ) ;
@@ -155,9 +154,7 @@ describe('domToReact', () => {
155154 } ) ;
156155
157156 it ( 'removes unknown attributes' , ( ) => {
158- const html = data . html . customElement ;
159- const reactElement = domToReact ( htmlToDOM ( html ) ) ;
160-
157+ const reactElement = domToReact ( htmlToDOM ( html . customElement ) ) ;
161158 expect ( reactElement ) . toMatchSnapshot ( ) ;
162159 } ) ;
163160 } ) ;
0 commit comments