@@ -13,10 +13,12 @@ import {
1313 wrapWithWrappingComponent ,
1414 getWrappingComponentMountRenderer ,
1515 fakeDynamicImport ,
16+ assertDomAvailable ,
1617} from 'enzyme-adapter-utils' ;
18+ import wrap from 'mocha-wrap' ;
1719
1820import './_helpers/setupAdapters' ;
19- import { describeIf } from './_helpers' ;
21+ import { describeIf , describeWithDOM } from './_helpers' ;
2022import { is } from './_helpers/version' ;
2123
2224describe ( 'enzyme-adapter-utils' , ( ) => {
@@ -205,6 +207,65 @@ describe('enzyme-adapter-utils', () => {
205207 } ) ;
206208 } ) ;
207209
210+ describe ( 'elementToTree' , ( ) => {
211+ class Target extends React . Component { render ( ) { return null ; } }
212+ const classNodeType = is ( '< 0.14' ) ? 'function' : 'class' ;
213+
214+ it ( 'produces a tree' , ( ) => {
215+ const target = elementToTree ( < Target a = "1" > < div /> </ Target > ) ;
216+ expect ( target ) . to . eql ( {
217+ nodeType : classNodeType ,
218+ type : Target ,
219+ props : {
220+ a : '1' ,
221+ children : < div /> ,
222+ } ,
223+ key : undefined ,
224+ ref : null ,
225+ instance : null ,
226+ rendered : {
227+ instance : null ,
228+ key : undefined ,
229+ nodeType : 'host' ,
230+ props : { } ,
231+ ref : null ,
232+ rendered : null ,
233+ type : 'div' ,
234+ } ,
235+ } ) ;
236+ } ) ;
237+
238+ it ( 'works with Array map' , ( ) => {
239+ const targets = [ < Target a = "1" > < div /> </ Target > ] ;
240+ expect ( targets . map ( elementToTree ) ) . to . eql ( [ {
241+ nodeType : classNodeType ,
242+ type : Target ,
243+ props : {
244+ a : '1' ,
245+ children : < div /> ,
246+ } ,
247+ key : undefined ,
248+ ref : null ,
249+ instance : null ,
250+ rendered : {
251+ instance : null ,
252+ key : undefined ,
253+ nodeType : 'host' ,
254+ props : { } ,
255+ ref : null ,
256+ rendered : null ,
257+ type : 'div' ,
258+ } ,
259+ } ] ) ;
260+ } ) ;
261+
262+ it ( 'throws when `dangerouslySetInnerHTML` and `children` are combined on host elements' , ( ) => {
263+ /* eslint react/no-danger-with-children: 0 */
264+ expect ( ( ) => elementToTree ( < div dangerouslySetInnerHTML = "hi" > nope</ div > ) ) . to . throw ( ) ;
265+ expect ( ( ) => elementToTree ( < Target dangerouslySetInnerHTML = "hi" > yep</ Target > ) ) . not . to . throw ( ) ;
266+ } ) ;
267+ } ) ;
268+
208269 describe ( 'findElement' , ( ) => {
209270 class Target extends React . Component { render ( ) { return null ; } }
210271 class Other extends React . Component { render ( ) { return null ; } }
@@ -405,4 +466,21 @@ describe('enzyme-adapter-utils', () => {
405466 } ) ;
406467 } ) ;
407468 } ) ;
469+
470+ describe ( 'assertDomAvailable' , ( ) => {
471+ describeWithDOM ( 'with DOM' , ( ) => {
472+ it ( 'throws' , ( ) => {
473+ expect ( global ) . to . have . property ( 'document' ) ;
474+ expect ( global . document ) . to . have . property ( 'createElement' ) ;
475+ expect ( assertDomAvailable ) . not . to . throw ( ) ;
476+ } ) ;
477+ } ) ;
478+
479+ describe ( 'without DOM' , ( ) => {
480+ wrap ( ) . withGlobal ( 'document' , ( ) => null ) . it ( 'noops' , ( ) => {
481+ expect ( ! ! global . document ) . to . equal ( false ) ;
482+ expect ( assertDomAvailable ) . to . throw ( ) ;
483+ } ) ;
484+ } ) ;
485+ } ) ;
408486} ) ;
0 commit comments