2016-12-18 124 views
0

爲了測試,我使用摩卡以及chai和chai-enzyme用於測試斷言。摩卡和酶 - 如何測試React組件沒有孩子?

說我有下面的表象成分反應:

import React from 'react' 

import { Component, PropTypes } from 'react'; 

const LayerList = (props) => (
    <div> 

    </div> 
    ) 

LayerList.PropTypes = { 
    layers: PropTypes.arrayOf(PropTypes.string).isRequired, 
    layerActions: PropTypes.shape({ 
     addLayer: PropTypes.func, 
     removeLayer: PropTypes.func, 
     toggleDragLayer: PropTypes.func, 
     moveLayerIndex: PropTypes.func, 
     updateLayerColour: PropTypes.func, 
     toggleLayerVisibility: PropTypes.func 
    }).isRequired 
} 

export default LayerList 

我怎麼會用酶和摩卡來測試該組件沒有孩子當道具層的長度爲0的數組?

這是我目前的嘗試:

it('should render only one div as first child element if layers prop is empty array',() => { 
     const wrapper = shallow(<LayerList layers={[]}/>);  
     expect(wrapper.type()).to.equal('div'); 
     expect(wrapper.children()).length.to.equal(); 
    }); 

Mocha Test Result

回答

2

.children()上ShallowWrapper應該做的:

const wrapper = shallow(<LayerList layers={[]} />); 
expect(wrapper.children()).to.have.length(0); 
相關問題