【问题标题】:Jest React Redux TestingJest React Redux 测试
【发布时间】:2019-03-22 19:01:08
【问题描述】:

我是 redux 的新手,我想知道如何测试 Actions 和 Reducers。

我已附上这两个文件的副本,并希望任何人都可以帮助我使用一种常见的模式。

我正在使用 Jest 进行单元测试。

网址是http://localhost:30001

只想知道如何进行测试以及如何在测试用例中使用 fetch 以及我可以将什么作为预期结果。

操作页面

import {
  REQUEST_CHARITIES,
  REQUEST_PAYMENT,
  SHOW_DONATION_AMOUNT_LIST,
  UPDATE_DONATION_AMOUNT_LIST,
  URL,
  PAY_NOW,
  UPDATE_MESSAGE,
  UPDATE_TOTAL_DONATE
} from './const';

//get list of all the charities
export function requestCharitiesList() {
  const request = fetch(`${URL}/charities`, {
      method: 'GET'
  }).then(response => response.json())
  return {
      type: REQUEST_CHARITIES,
      payload: request
  }
}

//get list of all payment amount
export function requestDonationAmount() {
  return {
      type: REQUEST_PAYMENT,
      payload: [{
              "id": 0,
              "price": 10
          },
          {
              "id": 1,
              "price": 20
          },
          {
              "id": 2,
              "price": 50
          },
          {
              "id": 3,
              "price": 100
          },
          {
              "id": 4,
              "price": 500
          },
      ]
  }
}

//get the total count of charities and update the payment options list visibility
export function showDonationList() {
  const paymentOptionsShow = []
  const request = fetch(`${URL}/charities`, {
      method: 'GET'
  }).then( response =>  response.json())
  request.then(function(result) {
    if(result.length >= 1){
      let arrayLength = result.length

      for( var i = 0 ; i < arrayLength ; i++ ) {
        paymentOptionsShow.push({active: false });
      }  
    }
 })

  return {
      type: SHOW_DONATION_AMOUNT_LIST,
      payload: paymentOptionsShow
  }
}

//to show and hide the payment options for each card
export function updateDonationList(list,id){
  return {
    type: UPDATE_DONATION_AMOUNT_LIST,
    payload: {
        "list": list,
        "id": id
        }
  }
}


//post the current paid amount
export function payNow(id, amount, currency) {
  const request = fetch(`${URL}/payments`, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: `{ "charitiesId": ${id}, "amount": ${amount}, "currency": "${currency}" }`
  })
  .then(response => response.json())
  return {
    type: PAY_NOW,
    payload: request
  }
}

//show thank you message
export function updateMessage(message) {
  return {
    type: UPDATE_MESSAGE,
    message: message
  }
}

//get the total number of payments made
export function summaryTotal() {
  const request = fetch(`${URL}/payments`,
    { method: 'GET' }).then(response => response.json())
  return {
    type: UPDATE_TOTAL_DONATE,
    payload: request
  }
}

我的一个减速器

import {
  REQUEST_CHARITIES
} from '../actions/const'

export default function (state = null, action) {
  switch (action.type) {
    case REQUEST_CHARITIES:
      return action.payload
    default:
      return state;
  }
}

【问题讨论】:

  • reducer 只是函数,所以如果你熟悉测试它是相同的原理。您的减速器已经导出,因此可以导入并在测试文件中使用。在您的测试用例中运行函数并传递不同的状态和动作对象,然后您可以根据这些参数期待您期望的结果。
  • 明白你的意思。我将尝试您建议的解决方案。我附上了我的动作测试的小代码,如果你不介意你可以看看,让我知道一切是否正确谢谢你的帮助

标签: reactjs redux react-redux jestjs jest-fetch-mock


【解决方案1】:

所以,这是我的评论如何发挥作用的一个简单示例。明白吗?

describe('Test case', () => {

  it('should return a new state', () => {
    const myReducer = nameOfReducer( { state }, { action } );

    expect(myReducer).toEqual({ state });
  });
});

【讨论】:

  • it('requestDonationAmount查看捐款金额', () => { let expectedResult = { type: all.REQUEST_PAYMENT, payload: [ { id: 0, price: 10 }, { id: 1, price: 20 }, { id: 2, price: 50 }, { id: 3, price: 100 }, { id: 4, price: 500 } } } expect(requestDonationAmount()).toEqual(expectedResult ) })
  • it('requestCharitiesList 检查长度', () => { requestCharitiesList().payload.then(res => { expect(res.length).toEqual(5) }) })
  • 我以为你在测试减速器?
  • 我正在测试 action 和 reducers
  • 像任何函数一样,你必须考虑我给它什么值以及我期望得到什么回报。使用模拟值调用函数并期待您的结果。
猜你喜欢
  • 1970-01-01
  • 2018-03-17
  • 2022-01-11
  • 1970-01-01
  • 2020-05-03
  • 2019-01-27
  • 2020-11-20
  • 2018-09-14
  • 2017-09-23
相关资源
最近更新 更多