代码之家  ›  专栏  ›  技术社区  ›  arunmmanoharan

材料界面反应测试用例失败-JEST,酶

  •  0
  • arunmmanoharan  · 技术社区  · 5 年前

    我有一个连接的组件,并且在组件中集成了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 回复  |  直到 5 年前
        1
  •  1
  •   Dez    5 年前

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

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