如何从嵌套的reducer中测试一个Reselect选择器?

时间:2017-07-30 01:46:34

标签: redux expect create-react-app reselect

我使用Reselect库创建了一个简单的Redux选择器。选择器获取数组中的最后一个对象。

Reselect docs中,测试示例是使用Assert库而不是Expect库构建的,但我的应用程序源自React Create App,因此我包含了Expect和Jest。

无论如何,无论我如何编写选择器测试,它都会挂起嵌套的reducer:TypeError: Cannot read property 'arrayOfObj' of undefined

Selectors.js

import { createSelector } from 'reselect'

const getArrayOfObj = (state) => state.reducer1.arrayOfObj

export const getLastObj = createSelector(
  [ getArrayOfObj ],
  (arrayOfObj) => {
    return arrayOfObj[arrayOfObj.length-1]
  }
)

Selectors.test.js

import * as selectors from 'Selectors'

const mockState = {
    reducer1: {
        arrayOfObj: [
        {id: 1}, 
        {id: 2}, 
        {id: 3}, 
        {id: 4},
      ],
      ...
    },
    reducer2: {
      ...
    }
}

describe('selectors', () => {
  it('getLastObj should get last obj in array', () => {
    expect(selectors.getLastObj(mockState)).toDeepEqual({id: 4});
  });
});

1 个答案:

答案 0 :(得分:1)

我可以通过将expect语句从.toDeepEqual()更改为.toEqual()

来解决错误