Javascript 用Jest酶测试反应还原剂

Javascript 用Jest酶测试反应还原剂,javascript,reactjs,react-redux,jestjs,enzyme,Javascript,Reactjs,React Redux,Jestjs,Enzyme,我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器 减速机 import React, { useEffect } from 'react' import

我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器。我的react应用程序中有一个这样的减速器

减速机

import React, { useEffect } from 'react'
import PropTypes from 'prop-types'

import FormField from 'wf-dbd-react-ui/es/FormField'
import FormFieldLabel from 'wf-dbd-react-ui/es/FormFieldLabel'
import FormFieldErrors from 'wf-dbd-react-ui/es/FormFieldErrors'
import Translatable from 'wf-dbd-react-ui/es/Translatable'
import Block from 'wf-dbd-react-ui/es/Block'
import withStrings from 'wf-dbd-react-ui/es/withStrings'

import { getMemoLabel } from '../../../../lib/utils/manage-payee-util'
import FormInput from 'wf-dbd-react-ui/es/FormInput'
import withMemoValidation from './withMemoValidation'
import ArialiveMemo from './ArialiveMemo'
import styles from './Memo.less'
import FormFieldErrorIcon from 'wf-dbd-react-ui/es/FormFieldErrorIcon'
import { MEMO_MAX_CHARS } from '../../../../components/workflows/edit-payment/constants'

const Memo = ({
  fieldId,
  defaultMemo,
  memo,
  isConfirm,
  getString,
  validateMemoField,
  userTypedMemo,
  isMemoFieldError,
  memoFieldValidate,
  defaultMemoValidate
}) => {
  useEffect(() => {
    memoFieldValidate(userTypedMemo <= MEMO_MAX_CHARS)
  }, [userTypedMemo, memoFieldValidate] )

  useEffect(() => {
    defaultMemoValidate(defaultMemo && defaultMemo.length <= MEMO_MAX_CHARS)
  }, [defaultMemo, defaultMemoValidate] )


  const memoLabel = getMemoLabel()
  return !isConfirm ? (
    <Block className={styles.memo}>
      <FormField fieldId={fieldId}>
        <FormFieldLabel>
          { isMemoFieldError &&
          <FormFieldErrorIcon className={styles.errorIcon} />
          }
          <div className={isMemoFieldError ? styles.errorMemoLabel : styles.memoLabel}>
            <Translatable id={memoLabel} />
          </div>
        </FormFieldLabel>
        <FormInput
          className={isMemoFieldError ? styles.inputerror : styles.input}
          initialValue={defaultMemo}
          onChange={validateMemoField}
          type="text"
          maxLength={MEMO_MAX_CHARS}
          placeholder={getString('memoPlaceholder')}
          deleteOnUnmount={true}
        />
        <FormFieldErrors />
        {userTypedMemo && <ArialiveMemo count={userTypedMemo} />}
      </FormField>
    </Block>
  ) : (
    <React.Fragment>
      <Translatable id={memoLabel} />
      <Block>
        <strong className={memo.length > MEMO_MAX_CHARS ? styles.inputerror : ''}>
          {memo}
        </strong>
      </Block>
    </React.Fragment>
  )
}

Memo.defaultProps = {
  isConfirm: false
}

Memo.propTypes = {
  fieldId: PropTypes.string,
  defaultMemo: PropTypes.string,
  isConfirm: PropTypes.bool,
  memo: PropTypes.string,
  validateMemoField: PropTypes.func,
  userTypedMemo: PropTypes.number,
  isMemoFieldError: PropTypes.bool,
  memoFieldValidate: PropTypes.func,
  defaultMemoValidate: PropTypes.func
}

export default withMemoValidation(withStrings(Memo))

import React,{useffect}来自“React”
从“道具类型”导入道具类型
从“wf dbd react ui/es/FormField”导入FormField
从“wf dbd react ui/es/FormFieldLabel”导入FormFieldLabel
从“wf dbd react ui/es/FormFieldErrors”导入FormFieldErrors
从“wf dbd react ui/es/TRANSTABLE”导入可翻译文件
从“wf dbd react ui/es/Block”导入块
从“wf dbd react ui/es/withStrings”导入withStrings
从“../../../../lib/utils/manage paye util”导入{getMemoLabel}
从“wf dbd react ui/es/FormInput”导入FormInput
从“/withMemoValidation”导入withMemoValidation
从“/ArialiveMemo”导入ArialiveMemo
从“./Memo.less”导入样式
从“wf dbd react ui/es/FormFieldErrorIcon”导入FormFieldErrorIcon
从“../../../../components/workflows/edit payment/constants”导入{MEMO_MAX_CHARS}
施工备忘录=({
菲尔德,
默认备忘录,
备忘录
我确信,
getString,
梅莫菲尔德,
userTypedMemo,
isMemoFieldError,
备忘录验证,
默认备忘录验证
}) => {
useffect(()=>{
memoFieldValidate(userTypedMemo{
defaultMemoValidate(defaultMemo&&defaultMemo.length
{memo}

)
}
Memo.defaultProps={
isConfirm:false
}
Memo.propTypes={
fieldId:PropTypes.string,
defaultMemo:PropTypes.string,
isConfirm:PropTypes.bool,
备注:PropTypes.string,
validateMemoField:PropTypes.func,
userTypedMemo:PropTypes.number,
isMemoFieldError:PropTypes.bool,
memoFieldValidate:PropTypes.func,
defaultMemoValidate:PropTypes.func
}
使用备忘录验证导出默认值(使用字符串(备忘录))

但是,当我运行
testcov
时,它说第2、3和8行没有被覆盖。我如何覆盖reducer文件中的if-else条件。

我认为您的测试用例没有正确测试,
clearErrors
包含两个参数:
state
action
,但您只传递
卡片类型。奖励
i进入
清除错误
功能

因此,您只需提供正确的单元测试,以涵盖每种情况:)

第2行:
if(action.payload.cardType==CARD\u TYPES.REWARD){

第3行:
返回{

  it("should clear out error codes", () => {
    const action = { payload: { cardType: CARD_TYPES.REWARD } };
    const state = clearErrors(initialState, action);
    const expectedState = { ... };
    expect(state).toEqual(expectedState);
  });
第8行:
返回状态;

  it("should not clear out error codes", () => {
    const action = { payload: { cardType: 'other' } };
    const state = clearErrors(initialState, action);
    expect(state).toEqual(initialState);
  });

@用户3019647别担心!