web-dev-qa-db-ja.com

TypeError:未定義のプロパティ「等しい」を読み取ることができません

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>
    );
  }
};

私のtestは次のようになります

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)

Enzymedocs に従って、

.contains()は、セレクターではなくReactElementを必要とします(他の多くのメソッドと同様)。呼び出すときは、ReactElementまたはJSX式で呼び出していることを確認してください。

私は何を間違えていますか?

ありがとう

[〜#〜] update [〜#〜]
削除しました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でまだアサートできません

17
daydreamer

それはEnzymeの問題ではありません。

expect(...).toundefinedです。これは expect.js をインストールしており、使用しているためです chai 構文。

この

expect(wrapper.contains(<TableHeaderColumn>Name</TableHeaderColumn>)).to.equal(true)

あるべき

expect(wrapper.contains(<TableHeaderColumn>Name</TableHeaderColumn>)).toEqual(true)
52
QoP