使用Jest酶测试React Reducer

时间:2020-04-24 06:18:18

标签: javascript reactjs react-redux jestjs enzyme

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

reducer.ts

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))

但是,当我运行test-cov时,它表示第2、3和8行未涵盖。如何覆盖化简文件中的if else条件。

1 个答案:

答案 0 :(得分:1)

我认为您的测试用例未正确测试,clearErrors接受两个参数:stateaction,但是您仅将CARD_TYPES.REWARD传递到{{1 }}功能。

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

第2行:clearErrors

第3行:if (action.payload.cardType === CARD_TYPES.REWARD) {

return {

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

return state;
相关问题