Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Jest-映射项目数组并测试其值_Reactjs_Typescript_Unit Testing_Jestjs_Enzyme - Fatal编程技术网

Reactjs Jest-映射项目数组并测试其值

Reactjs Jest-映射项目数组并测试其值,reactjs,typescript,unit-testing,jestjs,enzyme,Reactjs,Typescript,Unit Testing,Jestjs,Enzyme,我有一个简单的实用函数。它接受一个枚举,并将其转换为一个对象数组,其形状为{label:string,value:string} 以下是它使用的函数和接口: export interface ISelectOption { value: string; label: string; selected?: boolean; } const transformEnumToISelectArray = (object: object): ISelectOption[] => O

我有一个简单的实用函数。它接受一个枚举,并将其转换为一个对象数组,其形状为
{label:string,value:string}

以下是它使用的函数和接口:

export interface ISelectOption {
  value: string;
  label: string;
  selected?: boolean;
}

const transformEnumToISelectArray = (object: object): ISelectOption[] =>
  Object.keys(object).map(value => ({
    label: value,
    value
  }));

export default transformEnumToISelectArray;
我有一个重复的测试。基本上,如果我提供一个枚举,我会检查label/value的输出是否符合预期。以下是目前正在进行的测试:

import transformEnumToISelectArray from '../transformEnumToISelectArray';
import ISelectOption from '../../interfaces/ISelectOption';

describe('transformEnumToISelectArray', () => {
  enum mockEnum {
    FIELD1 = 'FIELD1',
    FIELD2 = 'FIELD2',
    FIELD3 = 'FIELD3'
  }

  let expectedResult: ISelectOption[] = [];
  let nonExpectedResult: ISelectOption[] = [];

  describe('ExpectedResults', () => {
    beforeEach(() => {
      expectedResult = [
        {
          label: 'FIELD1',
          value: 'FIELD1'
        },
        {
          label: 'FIELD2',
          value: 'FIELD2'
        },
        {
          label: 'FIELD3',
          value: 'FIELD3'
        }
      ];
    });
    it('should return an Array of ISelectOption shaped Object with correct `label` and `value`', () => {
      const response = transformEnumToISelectArray(mockEnum);
      expect(response[0].label).toEqual(expectedResult[0].label);
      expect(response[1].label).toEqual(expectedResult[1].label);
      expect(response[2].label).toEqual(expectedResult[2].label);
      expect(response[0].value).toEqual(expectedResult[0].value);
      expect(response[1].value).toEqual(expectedResult[1].value);
      expect(response[2].value).toEqual(expectedResult[2].value);
    });
  });
});
请忽略beforeach()之前的
,我有更多的测试,但我已经完成了。我的问题是,这只是一个优化,我想让它变得更好。因此,我想我会使用酶的
.map(fn)=>Array
通过
response
expectedResult
的数组
map

这就是我所做的:

expect(response.map(index => response[index].lable)).toEqual(
        expectedResult.map(index => expectedResult[index].label)
      );
但我得到了以下信息:

Type 'ISelectOption' cannot be used as an index type.ts(2538)
有人明白这一点吗。我是否需要在ISelectOption界面上指定索引属性才能工作?有什么想法吗?

response.map()
不会将索引作为参数发送给函数,在您的例子中,它是一个
ISelectOption
实例。在代码中,
索引
实际上是您的对象。将其更改为此应该可以:

expect(response.map(obj=>obj.label)).toEqual(
expectedResult.map(obj=>obj.label)
);