TypeError:routingState.equals不是函数开玩笑的测试用例

时间:2018-11-13 06:03:52

标签: javascript reactjs react-redux jestjs enzyme

这是我的selector.js文件:-

const makeSelectLocationState = () => {
  let prevRoutingState;
  let prevRoutingStateJS;

  return (state) => {
    const routingState = state.get('route'); // or state.route

    if (!routingState.equals(prevRoutingState)) {
      prevRoutingState = routingState;
      prevRoutingStateJS = routingState.toJS();
    }

    return prevRoutingStateJS;
  };
};

以及我编写的以下测试用例:-

it('makeSelectLocationState function testing', () => {
    const selectLocationState = makeSelectLocationState();
    const mockedState = fromJS({
      route: '',
    });
    expect(selectLocationState(mockedState)).toEqual('');
  });

使用笑话运行测试用例时,出现以下错误:-

TypeError: routingState.equals is not a function

2 个答案:

答案 0 :(得分:1)

问题在这里:

const routingState = state.get('route'); 

感觉到您没有获得路由状态,可能未定义,因此您将收到上述错误消息。因此,请检查路由状态的日期。

在测试文件中,将mockedState传递给selectLocationState

selectLocationState(mockedState)

但是,makeSelectLocationState函数没有属性。可以更改为此:

const makeSelectLocationState = (state) => {}

答案 1 :(得分:0)

route: ''
const routingState = state.get('route');

It looks like route is a string (an empty string). Strings do not have an equals method, so routingState.equals() doesn't exist.

Perhaps you could set mockedState.route to an object that has an equals function:

const mockedState = fromJS({
  route: { equals: item => item === item },
});