JEST测试中的TS错误-SelectOptions.find不是函数

时间:2019-07-19 14:23:04

标签: javascript reactjs typescript jestjs enzyme

我有一个简单的实用程序,其中基于一些比较功能返回一个布尔值:

import ISelect from './interfaces/ISelect';

const checkCaseSensitiveSelectUtil = (inputValue: ISelect, selectOptions: ISelect[]): boolean => {
  const exactValueExists = selectOptions.find(input => input.value === inputValue.value);
  return !exactValueExists;
};

export default checkCaseSensitiveSelectUtil;

这是我的测试:

  it('should return TRUE if the values are equal', () => {
    const selectedOptions = {
      label: '*positions',
      value: '*positions'
    };
    expect(checkCaseSensitiveSelectUtil(caseInsensitiveValues, selectedOptions)).toEqual(true);
  });

这是我得到的一个错误:

    TypeError: selectOptions.find is not a function

      2 |
      3 | const checkCaseSensitiveSelectUtil = (inputValue: ISelect, selectOptions: ISelect[]): boolean => {
    > 4 |   const exactValueExists = selectOptions.find(input => input.value === inputValue.value);
        |                                          ^
      5 |   return !exactValueExists;
      6 | };
      7 |

关于做什么的任何想法?我的意思是整个工作正常。但是我怎么了?

1 个答案:

答案 0 :(得分:1)

find函数用于数组而不是对象。

const selectedOptions = {
  label: '*positions',
  value: '*positions'
};

将以上内容更改为有效的数组对象。

尝试一下

const selectedOptions = [{
  label: '*positions',
  value: '*positions'
}];