我有一个简单的实用程序,其中基于一些比较功能返回一个布尔值:
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 |
关于做什么的任何想法?我的意思是整个工作正常。但是我怎么了?
答案 0 :(得分:1)
find
函数用于数组而不是对象。
const selectedOptions = {
label: '*positions',
value: '*positions'
};
将以上内容更改为有效的数组对象。
尝试一下
const selectedOptions = [{
label: '*positions',
value: '*positions'
}];