反应-“期望(接收)。等于(期望)”

时间:2019-05-05 15:01:40

标签: reactjs react-redux jestjs enzyme

接着是React,Jest和Enzyme教程。这是link to the code

这是测试:

import React from 'react';
import { mount, shallow } from 'enzyme';
import { Loot } from './Loot';

describe('Loot', () => {
  const mockFetchBitcoin = jest.fn();
  let props = { balance: 10, bitcoin: {} };
  let loot = shallow(<Loot {...props} />);

  it('renders properly', () => {
    expect(loot).toMatchSnapshot();
  });

  describe('when mounted', () => {
    beforeEach(() => {
      props.fetchBitcoin = mockFetchBitcoin;
      loot = mount(<Loot {...props} />);
    });

    it('dispatches the `fetchBitcoin()` method it receives from props', () => {
      expect(mockFetchBitcoin).toHaveBeenCalled();
    });
  });

  describe('when there are valid bitcoin props', () => {
    beforeEach(() => {
      props = { balance: 10, bitcoin: { bpi: { usd: { rate: '1,000' } } } };
      loot = shallow(<Loot {...props} />);
    });
  });

  it('displays the correct bitcoin value', () => {
    expect(loot.find('h3').text()).toEqual('Bitcoin balance: 0.01');
  });
  // it('displays the correct bitcoin value', () => {
  //   expect(loot.find('h3').text()).toEqual('Bitcoin balance: ');
  // });
});

这是正在测试的代码:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { fetchBitcoin } from "../actions/bitcoin";

export class Loot extends Component {
  componentDidMount() {
    this.props.fetchBitcoin();
  }

  computeBitcoin() {
    const { bitcoin } = this.props;

    if (Object.keys(bitcoin).length === 0) return '';

    return this.props.balance / parseInt(bitcoin.bpi.USD.rate.replace(/,/g,''), 10);
  }

  render() {
    return(
      <h3>Bitcoin balance: {this.computeBitcoin()}</h3>
    )
  }
}

export default connect(state => state, { fetchBitcoin })(Loot);

在命令行中收到以下错误消息:

CMD Line

此外,此错误消息也会出现在浏览器中:

TypeError: Cannot convert undefined or null to object

0 个答案:

没有答案