【问题标题】:Test React Reducer using Jest Enzyme使用 Jest Enzyme 测试 React Reducer
【发布时间】:2020-08-07 16:04:41
【问题描述】:

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

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 行没有被覆盖。我如何覆盖reducer文件中的if else条件。

【问题讨论】:

    标签: javascript reactjs react-redux jestjs enzyme


    【解决方案1】:

    我认为您的测试用例没有正确测试,clearErrors 接受两个参数:stateaction,但您仅将 CARD_TYPES.REWARD 传递给 clearErrors 函数。

    所以你只需要提供正确的单元测试来涵盖每个条件:)

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

    第 3 行:return {

      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 行:return state;

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

    【讨论】:

    • @user3019647 不用担心!
    猜你喜欢
    • 2017-02-12
    • 2017-11-26
    • 2019-01-29
    • 1970-01-01
    • 2017-11-10
    • 2018-08-20
    • 2019-01-27
    • 2021-07-29
    • 2019-05-06
    相关资源
    最近更新 更多