11const assert = require ( 'assert' ) ;
22const React = require ( 'react' ) ;
3- const Preact = require ( 'preact' ) ;
43const htmlToDOM = require ( 'html-dom-parser' ) ;
54const domToReact = require ( '../lib/dom-to-react' ) ;
65const { data, render } = require ( './helpers/' ) ;
76const utilities = require ( '../lib/utilities' ) ;
87
9- describe ( 'dom-to-react parser' , ( ) => {
10- let actualReactVersion ;
11- beforeEach ( ( ) => {
12- actualReactVersion = React . version ;
13- } ) ;
14-
15- afterEach ( ( ) => {
16- React . version = actualReactVersion ;
17- } ) ;
18-
8+ describe ( 'dom-to-react' , ( ) => {
199 it ( 'converts single DOM node to React' , ( ) => {
2010 const html = data . html . single ;
2111 const reactElement = domToReact ( htmlToDOM ( html ) ) ;
@@ -39,7 +29,7 @@ describe('dom-to-react parser', () => {
3929 ] ) ;
4030 } ) ;
4131
42- // https://facebook.github.io/react/ docs/forms.html#why -textarea-value
32+ // https://reactjs.org/ docs/forms.html#the -textarea-tag
4333 it ( 'converts <textarea> correctly' , ( ) => {
4434 const html = data . html . textarea ;
4535 const reactElement = domToReact ( htmlToDOM ( html ) ) ;
@@ -131,7 +121,7 @@ describe('dom-to-react parser', () => {
131121 ] ) ;
132122 } ) ;
133123
134- it ( "handles svg's with a viewBox" , ( ) => {
124+ it ( 'converts SVG element with viewBox attribute' , ( ) => {
135125 const html = data . svg . simple ;
136126 const reactElement = domToReact (
137127 htmlToDOM ( html , { lowerCaseAttributeNames : false } )
@@ -160,47 +150,61 @@ describe('dom-to-react parser', () => {
160150 ) ;
161151 } ) ;
162152
163- it ( 'handles using a custom component library' , ( ) => {
164- const html = data . html . single ;
165- const preactElement = domToReact ( htmlToDOM ( html ) , { library : Preact } ) ;
153+ describe ( 'library' , ( ) => {
154+ const Preact = require ( 'preact' ) ;
155+
156+ it ( 'converts with Preact instead of React' , ( ) => {
157+ const html = data . html . single ;
158+ const preactElement = domToReact ( htmlToDOM ( html ) , { library : Preact } ) ;
166159
167- assert . deepEqual ( preactElement , Preact . createElement ( 'p' , { } , 'foo' ) ) ;
160+ assert . deepEqual ( preactElement , Preact . createElement ( 'p' , { } , 'foo' ) ) ;
161+ } ) ;
168162 } ) ;
169163
170- it ( 'does not modify keys for replacement if it has one' , ( ) => {
171- const html = [ data . html . single , data . html . customElement ] . join ( '' ) ;
164+ describe ( 'replace' , ( ) => {
165+ it ( "does not set key if there's 1 node" , ( ) => {
166+ const replaceElement = React . createElement ( 'p' ) ;
167+ const reactElement = domToReact ( htmlToDOM ( data . html . single ) , {
168+ replace : ( ) => replaceElement
169+ } ) ;
170+ assert . deepEqual ( reactElement , replaceElement ) ;
171+ } ) ;
172172
173- const reactElements = domToReact ( htmlToDOM ( html ) , {
174- replace : node => {
175- if ( node . name === 'p' ) {
176- return React . createElement ( 'p' , { } , 'replaced foo' ) ;
177- }
178- if ( node . name === 'custom-button' ) {
179- return React . createElement (
180- 'custom-button' ,
181- {
182- key : 'myKey' ,
183- class : 'myClass' ,
184- 'custom-attribute' : 'replaced value'
185- } ,
186- null
187- ) ;
173+ it ( "does not modify keys if it's already set" , ( ) => {
174+ const html = [ data . html . single , data . html . customElement ] . join ( '' ) ;
175+
176+ const reactElements = domToReact ( htmlToDOM ( html ) , {
177+ replace : node => {
178+ if ( node . name === 'p' ) {
179+ return React . createElement ( 'p' , { } , 'replaced foo' ) ;
180+ }
181+ if ( node . name === 'custom-button' ) {
182+ return React . createElement (
183+ 'custom-button' ,
184+ {
185+ key : 'myKey' ,
186+ class : 'myClass' ,
187+ 'custom-attribute' : 'replaced value'
188+ } ,
189+ null
190+ ) ;
191+ }
188192 }
189- }
193+ } ) ;
194+
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+ ] ) ;
190207 } ) ;
191-
192- assert . deepEqual ( reactElements , [
193- React . createElement ( 'p' , { key : 0 } , 'replaced foo' ) ,
194- React . createElement (
195- 'custom-button' ,
196- {
197- key : 'myKey' ,
198- class : 'myClass' ,
199- 'custom-attribute' : 'replaced value'
200- } ,
201- null
202- )
203- ] ) ;
204208 } ) ;
205209
206210 describe ( 'when React <16' , ( ) => {
0 commit comments