shallow rendering能夠把組件當作一個單元來測試,並且確保不會由於子組件的行爲而直接出現斷言(Shallow rendering is useful to constrain yourself to testing a component as a unit, and to ensure that your tests aren't indirectly asserting on behavior of child components.)html
import { shallow } from 'enzyme'; describe('<MyComponent />', () => { it('should render three <Foo /> components', () => { const wrapper = shallow(<MyComponent />); expect(wrapper.find(Foo)).to.have.length(3); }); it('should render an `.icon-star`', () => { const wrapper = shallow(<MyComponent />); expect(wrapper.find('.icon-star')).to.have.length(1); }); it('should render children when passed in', () => { const wrapper = shallow( <MyComponent> <div className="unique" /> </MyComponent> ); expect(wrapper.contains(<div className="unique" />)).to.equal(true); }); it('simulates click events', () => { const onButtonClick = sinon.spy(); const wrapper = shallow( <Foo onButtonClick={onButtonClick} /> ); wrapper.find('button').simulate('click'); expect(onButtonClick.calledOnce).to.equal(true); }); });
node (ReactElement)要渲染的節點node
options (對象 [可選]):api
options.context: (對象 [可選]): 傳給組件的Contextapp
ShallowWrapper:這是一個cheerio的解析對象函數
.find(selector) => ShallowWrapper
找到全部匹配的節點測試
.findWhere(predicate) => ShallowWrapper
找到全部渲染樹下知足函數內判斷的節點code
.filter(selector) => ShallowWrapper
過濾匹配的節點component
.filterWhere(predicate) => ShallowWrapperhtm
.contains(nodeOrNodes) => Boolean對象
.containsMatchingElement(node) => Boolean
.containsAllMatchingElements(nodes) => Boolean
.containsAllMatchingElements(nodes) => Boolean
.equals(node) => Boolean
.matchesElement(node) => Boolean
.hasClass(className) => Boolean
.is(selector) => Boolean
.isEmpty() => Boolean
.not(selector) => ShallowWrapper
.children() => ShallowWrapper
.childAt(index) => ShallowWrapper
.parents() => ShallowWrapper
.parent() => ShallowWrapper
.closest(selector) => ShallowWrapper
.shallow([options]) => ShallowWrapper
.render() => CheerioWrapper
參照這裏,不一一列舉,方法和jQuery