用酶进行单元测试

时间:2019-07-11 06:39:49

标签: javascript reactjs react-native jestjs enzyme

我的React-Native应用程序中具有以下组件。我被要求为此使用玩笑和酶编写单元测试,但是我是单元测试的新手。因此,如何分解并编写适当的测试是我所不知道的。有人可以帮我吗?

import React, { Component } from 'react';
import { View } from 'react-native';
import { WebView } from 'react-native-webview';
import { Button, Loader, ScreenContainer } from '../../../../../components';

import {
  decodeBase64,
  hasWordsInString,
  setFirstAndFamilyName,
} from '../../../../../library/Utils';

import { url, searchWords, signUpMethods } from '../../../../../config';
import { SIGN_UP_FORM } from '../../../../constants/forms';

// tslint:disable-next-line: max-line-length
const response = 'some-token';

class MyWebView extends Component {
  state = {
    loaderStatus: true,
  };

  stopLoader = () => {
    this.setState({ loaderStatus: false });
  }

  startLoader = () => {
    this.setState({ loaderStatus: true });
  }

  displayLoader = () => {
    const { loaderStatus } = this.state;
    return loaderStatus && <Loader />;
  }

  render() {
    const { navigation, addFormData, setSignUpMethod } = this.props;

    return (
      <View style={{ flex: 1 }}>
        <WebView
          source={{ uri: url }}
          onLoadStart={() => this.startLoader()}
          onLoad={() => this.stopLoader()}
          onLoadEnd={(syntheticEvent) => {
            const { nativeEvent } = syntheticEvent;

            if (nativeEvent.title === 'Consent Platform') {
              if (hasWordsInString(nativeEvent.url, searchWords)) {
                const { address, ...rest } = decodeBase64(response).data;
                const userName = setFirstAndFamilyName(rest.name);
                rest.firstName = userName.firstName;
                rest.familyName = userName.familyName;

                addFormData({ form: SIGN_UP_FORM, data: { values: { ...rest, ...address } } });
                setSignUpMethod(signUpMethods.MY_INFO);

                navigation.replace('ConfirmName', rest);
              }
            }
          }}
        />

        {this.displayLoader()}
      </View>
    );
  }
}

export default MyWebView;

如何使用笑话和酶为上述代码编写一些适当的单元测试?我必须遵循的原则是什么?是什么使单元测试更好呢?

1 个答案:

答案 0 :(得分:0)

这里是一个浅层测试,然后您可以传递自己的虚拟数据并比较快照,因此,基本上,如果有人更改了此组件中使用的任何自定义组件,则测试将失败

x = [76.6352  551.6941  702.1137   10.4739   13.6470  250.4344   22.4630   
28.4753  100.6441];

fval =  1.0e+303 *[-0.0000   -0.0000   -6.7174   -0.0000   -0.0000   -0.0000   
-0.0000   -0.0000   -0.0000]