如何测试一个 React 组件是否包含另一个带有 Tape 和 Enzyme 的组件?
How to test if a React component contains another component with Tape and Enzyme?
假设我有以下 React 组件:
import React from 'react'
import AppBar from 'material-ui/lib/app-bar'
class NavBar extends React.Component {
render () {
return (
<div>
<AppBar
title='My NavBar Title'
/>
</div>
)
}
}
export default NavBar
我想设置一个测试以确保用户看到 material-ui AppBar when the NavBar
is rendered, using Tape and Enzyme:
import NavBar from './NavBar'
import React from 'react'
import test from 'tape'
// import { I don't know exactly what to import here. Maybe `shallow`? } from 'enzyme'
test('NavBar component test', (assert) => {
test('I should see an AppBar', (assert) => {
// How can I test for it?
// Also, can I test only for the presence of `AppBar`, without
// necessarily for the overriding of props? For example,
// even if I actually have <AppBar title='My NavBar title' />,
// can I test only for `AppBar`?
assert.end()
})
assert.end()
})
我怎样才能正确地做到这一点?
我没有用过胶带和酶,但是根据我的理解,这个问题更多的是和React测试工具有关。
反正工具有方法:https://facebook.github.io/react/docs/test-utils.html#findallinrenderedtree
您可以使用它来查看组件是否像这样呈现:
const u = require('react-addons-test-utils');
const hasAppBar = u.findAllInRenderedTree(NavBar, (component) => {
return u.isCompositeComponentWithType(component, AppBar);
});
只需确保 AppBar 确实是您正在搜索的组件的显示名称。
您将返回一个与谓词匹配的组件数组,因此您可以检查长度是否 > 0。
我明白了。它是:
test('I should see one AppBar', (assert) => {
const wrapper = shallow(<NavBar />)
assert.equal(wrapper.find('AppBar').length === 1, true)
assert.end()
})
shallow
function from enzyme
returns a wrapper which has the method find
。 find
returns 具有 属性 length
的对象。如果我的组件中有两个 AppBar
,length
将等于 2
,因此我可以测试它以获得 === 1
来完成我的测试。
假设我有以下 React 组件:
import React from 'react'
import AppBar from 'material-ui/lib/app-bar'
class NavBar extends React.Component {
render () {
return (
<div>
<AppBar
title='My NavBar Title'
/>
</div>
)
}
}
export default NavBar
我想设置一个测试以确保用户看到 material-ui AppBar when the NavBar
is rendered, using Tape and Enzyme:
import NavBar from './NavBar'
import React from 'react'
import test from 'tape'
// import { I don't know exactly what to import here. Maybe `shallow`? } from 'enzyme'
test('NavBar component test', (assert) => {
test('I should see an AppBar', (assert) => {
// How can I test for it?
// Also, can I test only for the presence of `AppBar`, without
// necessarily for the overriding of props? For example,
// even if I actually have <AppBar title='My NavBar title' />,
// can I test only for `AppBar`?
assert.end()
})
assert.end()
})
我怎样才能正确地做到这一点?
我没有用过胶带和酶,但是根据我的理解,这个问题更多的是和React测试工具有关。
反正工具有方法:https://facebook.github.io/react/docs/test-utils.html#findallinrenderedtree
您可以使用它来查看组件是否像这样呈现:
const u = require('react-addons-test-utils');
const hasAppBar = u.findAllInRenderedTree(NavBar, (component) => {
return u.isCompositeComponentWithType(component, AppBar);
});
只需确保 AppBar 确实是您正在搜索的组件的显示名称。
您将返回一个与谓词匹配的组件数组,因此您可以检查长度是否 > 0。
我明白了。它是:
test('I should see one AppBar', (assert) => {
const wrapper = shallow(<NavBar />)
assert.equal(wrapper.find('AppBar').length === 1, true)
assert.end()
})
shallow
function from enzyme
returns a wrapper which has the method find
。 find
returns 具有 属性 length
的对象。如果我的组件中有两个 AppBar
,length
将等于 2
,因此我可以测试它以获得 === 1
来完成我的测试。