单元测试 - React-redux减少器的预期值

时间:2018-04-17 16:02:13

标签: unit-testing react-redux reducers

我正在尝试为redux减速机进行单元测试。我想知道预期的回报价值。

目前我期望值等于:

  

<“isConnected”:false>

但我收到了:

  

<“0”:<“isConnected”:true>,“isConnected”:false>

我在文档中找到了两种reducer语法 第一个返回一个对象:

  

<“0”:<“isConnected”:true>,“isConnected”:false>

第二个数组:

  

[<“isConnected”:false>]

正确的减速器应该返回什么值?

我的package.json:

{
  "name": "app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "react-scripts": "1.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1",
    "react-test-renderer": "^16.3.2"
  }
}

我的减速机:

import * as types from '../actions/actionsTypes'

const initialState = {
    isConnected : true
};

const appReducer = ( state = initialState, action) => {
    switch (action.type) {
        case types.OPEN_CONNECTION :
            return {
                ...state,
                isConnected : true
            };

        case types.CLOSE_CONNECTION :
            return {
                ...state,
                isConnected : false
            };

        default:
            return state
    }
};

export default appReducer;

我的测试:

it('appReducer Test', () => {
    expect(appReducer([{isConnected : false}], {type: types.OPEN_CONNECTION})).toEqual({isConnected : true})
})

1 个答案:

答案 0 :(得分:0)

我收到了这个:

  

<“0”:<“isConnected”:true>,“isConnected”:false>

因为我在我的测试中将initialState作为数组给出:

  

[{isConnected:false}]

测试的正确语法是:

expect(appReducer({isConnected : false}, {type: types.OPEN_CONNECTION})).toEqual({isConnected : true})