【问题标题】:How can mock data in state in order to test a Redux reducer which deletes from the state?如何在 state 中模拟数据以测试从 state 中删除的 Redux reducer?
【发布时间】:2019-02-19 13:58:35
【问题描述】:

在我的 ReactJS/Redux 应用程序中测试 reducer 时,如何向 state 添加一些测试数据?

我有这个测试用例:

it("should store selected filter", () => {

    const selectedFilterData = { 
        id: 4321, 
        filterGroupId: 1234, 
        filterName: "gender", 
        text: "Male", 
        value: "pdl_profile_gender_1"
    }

    const action = { type: FILTER_SELECTED, data: selectedFilterData }

    Reducer(filters).expect(action).toReturnState({
        1234: {
            id: 1234,
            selectedFilters: [
                {
                    id: 4321,
                    filterName: "gender",
                    text: "Male",
                    value: "pdl_profile_gender_1"
                }
            ]
        }
    })
})

此操作/reducer 更新对应 ID 的现有“selectedFilters”属性。当状态中没有任何内容时,上述测试正在运行,因此会引发错误。如何在该特定测试用例的状态中添加测试数据?

感谢任何帮助

【问题讨论】:

    标签: reactjs unit-testing redux jestjs


    【解决方案1】:

    看起来您正在使用redux-testkit。根据文档,您可以使用withState。这是一个例子。

    Reducer(filters)
      .withState(state) // like this!
      .expect(action)
      .toReturnState(result);
    

    https://github.com/wix/redux-testkit/blob/master/README.md

    【讨论】:

      猜你喜欢
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多