作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用enzyme
来断言DOM节点。我的 Component
看起来像
import React, {Component} from 'react';
import TransactionListRow from './TransactionListRow';
import {Table, TableBody, TableHeader, TableHeaderColumn, TableRow} from 'material-ui/Table';
export default class TransactionList extends Component {
render() {
const { transactions } = this.props;
return (
<Table>
<TableHeader displaySelectAll={false}>
<TableRow>
<TableHeaderColumn>Name</TableHeaderColumn>
<TableHeaderColumn>Amount</TableHeaderColumn>
<TableHeaderColumn>Transaction</TableHeaderColumn>
<TableHeaderColumn>Category</TableHeaderColumn>
</TableRow>
</TableHeader>
<TableBody>
{transactions.map(transaction =>
<TransactionListRow key={transaction.id} transaction={transaction}/>
)}
</TableBody>
</Table>
);
}
};
我的测试
看起来像
import expect from 'expect';
import React from 'react';
import {mount} from 'enzyme';
import TransactionList from '../TransactionList';
import {TableHeaderColumn} from 'material-ui/Table';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
describe("<TransactionList />", () => {
const mountWithContext = (node) => mount(node, {
context: {
muiTheme: getMuiTheme(),
},
childContextTypes: {
muiTheme: React.PropTypes.object.isRequired,
}
});
it('renders five <TableHeaderColumn /> components', () => {
const wrapper = mountWithContext(<TransactionList transactions={[]}/>)
console.log(wrapper.html());
// expect(wrapper.find('thead').length).toBe(1);
expect(wrapper.contains(<TableHeaderColumn>Name</TableHeaderColumn>)).to.equal(true)
});
});
当我运行这个时,我得到
● <TransactionList /> › renders five <TableHeaderColumn /> components
TypeError: Cannot read property 'equal' of undefined
at Object.<anonymous> (src/components/transactions/__tests__/TransactionList.test.js:24:250)
at process._tickCallback (internal/process/next_tick.js:103:7)
根据 enzyme
docs ,
.contains() expects a ReactElement, not a selector (like many other methods). Make sure that when you are calling it you are calling it with a ReactElement or a JSX expression.
我做错了什么?
谢谢
更新
我删除了 import Expect from 'expect
并将其运行为
import React from 'react';
import {mount} from 'enzyme';
import TransactionList from '../TransactionList';
import TableHeaderColumn from 'material-ui/Table';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
describe("<TransactionList />", () => {
const mountWithContext = (node) => mount(node, {
context: {
muiTheme: getMuiTheme(),
},
childContextTypes: {
muiTheme: React.PropTypes.object.isRequired,
}
});
it('renders five <TableHeaderColumn /> components', () => {
const wrapper = mountWithContext(<TransactionList transactions={[]}/>)
// console.log(wrapper.html());
expect(wrapper.find('thead').length).toBe(1);
expect(wrapper.find('td').length).toBe(0);
// this is not working
expect(wrapper.contains(<TableHeaderColumn/>)).toEqual(true);
});
});
现在失败了
FAIL src/components/transactions/__tests__/TransactionList.test.js
● <TransactionList /> › renders five <TableHeaderColumn /> components
expect(received).toEqual(expected)
Expected value to equal:
true
Received:
false
at Object.<anonymous> (src/components/transactions/__tests__/TransactionList.test.js:26:164)
并与
expect(wrapper.contains(<TableHeaderColumn/>)).to.equal(true);
我明白
Warning: Unknown props `onMouseEnter`, `onMouseLeave`, `onClick` on <th> tag. Remove these props from the element.
FAIL src/components/transactions/__tests__/TransactionList.test.js
● <TransactionList /> › renders five <TableHeaderColumn /> components
TypeError: Cannot read property 'equal' of undefined
at Object.<anonymous> (src/components/transactions/__tests__/TransactionList.test.js:26:166)
at process._tickCallback (internal/process/next_tick.js:103:7)
我仍然无法在 ReactElement
上断言
最佳答案
这不是 enzyme
问题。
expect(...).to
是未定义
,因为您已经安装了 expect.js并且您正在使用 chai语法。
这个
expect(wrapper.contains(<TableHeaderColumn>Name</TableHeaderColumn>)).to.equal(true)
应该是
expect(wrapper.contains(<TableHeaderColumn>Name</TableHeaderColumn>)).toEqual(true)
关于javascript - 类型错误 : Cannot read property 'equal' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39926517/
我是一名优秀的程序员,十分优秀!