重大UI React测试用例失败-JEST,ENZYME

时间:2019-03-03 01:24:05

标签: javascript reactjs react-redux jestjs material-ui

我有一个已连接的组件,并且已在我的组件中集成了MaterialUI。由于某些原因,测试仍然失败,我无法在线找到一些文章来解决此问题。

请咨询。

下面是我的代码。

import React, {Component} from 'react';
import {connect} from 'react-redux';
import SourceCurrencyInput from './components/SourceCurrencyInput';
import TargetCurrencyInput from './components/TargetCurrencyInput';
import {fetchCurrencyConverterRates} from './actions/currencyConverterActions';
import CurrencyConverterValue from './components/CurrencyConverterValue';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';
import {withStyles} from '@material-ui/core/styles';
import './App.css';
import {
  changeSourceCurrencyValue,
  changeTargetCurrencyValue
} from './actions/actions';

const styles = {
  root: {
    flexGrow: 1,
  },
  grow: {
    flexGrow: 1,
  },
  menuButton: {
    marginLeft: -12,
    marginRight: 20,
  },
};

class App extends Component {

  componentDidMount() {
    this.props.fetchCurrencyConverterRates(
      this.props.srcCurrencyType,
      this.props.tgtCurrencyType
    );
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    if (prevProps.srcCurrencyType !== this.props.srcCurrencyType
      || prevProps.tgtCurrencyType !== this.props.tgtCurrencyType) {
      this.props.fetchCurrencyConverterRates(
        this.props.srcCurrencyType,
        this.props.tgtCurrencyType);
    }
  }

  clearAll = () => {
    this.props.sourceValue('');
    this.props.targetValue('');
  };

  render() {
    const {srcCurrencyType, tgtCurrencyType, srcCurrencyValue, tgtCurrencyValue, currencyConversionRate, classes} = this.props;

    return (
      <div className="App">
        <AppBar position="static">
          <Toolbar>
            <Typography variant="h6" color="inherit" className={classes.grow}>
              Currency Converter by Arun Manohar
            </Typography>
          </Toolbar>
        </AppBar>
        <header className="App-header">
          <SourceCurrencyInput/>
          <TargetCurrencyInput/>
          <Button variant="contained" color="primary" className={classes.button}
            onClick={() => this.clearAll()}>Clear</Button>
        </header>
        <CurrencyConverterValue srcCurrencyType={srcCurrencyType}
          tgtCurrencyType={tgtCurrencyType}
          srcCurrencyValue={srcCurrencyValue}
          tgtCurrencyValue={tgtCurrencyValue}
          currencyConversionRate={currencyConversionRate}
        />
        <footer><a href={'https://currencyconverterapi.com'} target={'_blank'}>API from currencyconverterapi.com</a></footer>
      </div>
    );
  }
}

const mapStateToProps = state => {
  return {
    srcCurrencyType: state.currencyConverterReducer.sourceCurrencyType,
    tgtCurrencyType: state.currencyConverterReducer.targetCurrencyType,
    srcCurrencyValue: state.currencyConverterReducer.sourceCurrencyValue,
    tgtCurrencyValue: state.currencyConverterReducer.targetCurrencyValue,
    currencyConversionRate: state.getConvertedRates.data[0]
  };
};

const mapDispatchToProps = (dispatch) => ({
  fetchCurrencyConverterRates: (src, tgt) => dispatch(fetchCurrencyConverterRates(src, tgt)),
  sourceValue: (val) => dispatch(changeSourceCurrencyValue(val)),
  targetValue: (val) => dispatch(changeTargetCurrencyValue(val)),
});

export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(App));

下面是我的测试用例。

import React from 'react';
import {Provider} from 'react-redux';
import configureStore from 'redux-mock-store';
import App from './App';
import {createMount} from '@material-ui/core/test-utils';

const mockStore = configureStore();
const initialState = {sourceCurrencyType: 'USD'};
const store = mockStore(initialState);

describe('<App />', () => {
  let mount;

  beforeEach(() => {
    mount = createMount();
  });

  it('should work', () => {
    const wrapper = mount(<Provider store={store}><App/></Provider>);
    console.log(wrapper.debug());
  });
});

这是我得到的错误。

TypeError: Cannot read property 'sourceCurrencyType' of undefined

我只需要一种在测试中访问此组件的方法。请帮帮我。

1 个答案:

答案 0 :(得分:1)

您的初始状态必须与reducer对象保持相同的结构,例如:

const initialState = {
  currencyConverterReducer: {
    sourceCurrencyType: 'USD',
    // rest of attributes of currencyConverterReducer
  }
};