TypeError: Non è possibile leggere la proprietà 'uguale' undefined

voti
7

Sto cercando di utilizzare enzymeper affermare nodi DOM. I miei Componentassomiglia

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

I miei testassomiglia

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

Quando eseguo questo, ottengo

  ● <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)

Come da Enzyme documenti ,

.contains () si aspetta una ReactElement, non un selettore (come molti altri metodi). Assicurarsi che quando si sta chiamando che si sta chiamando con un ReactElement o un'espressione JSX.

Che cosa sto facendo di sbagliato?

Grazie

UPDATE
Ho rimosso il import expect from 'expecte corse come

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

Viene a mancare ora con

 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)

e con

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

ottengo

      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)

Non riesco ancora a valere su ReactElement

È pubblicato 07/10/2016 alle 22:13
fonte dall'utente
In altre lingue...                            


1 risposte

voti
22

Questo non è un Enzymeproblema.

expect(...).toè undefinedperché si è installato expect.js e si utilizza chai sintassi.

Questo

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

dovrebbe essere

expect(wrapper.contains(<TableHeaderColumn>Name</TableHeaderColumn>)).toEqual(true)
Risposto il 07/10/2016 a 22:24
fonte dall'utente

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more