用于在Jest中排序的单元测试用例

时间:2017-12-22 02:43:31

标签: javascript unit-testing ecmascript-6 jest

我是反应和开玩笑的新手。我正在尝试编写一个测试用例,它将传递用容器编写的sort函数。我不知道怎么写。

当我尝试运行测试用例时,我收到TypeError: Cannot read property 'sort' of undefined错误。

实际数组

let testArray=[{trackingId:'1',updated: '1512885243'},
                {trackingId:'1',updated: '1512485253'},
                {trackingId:'3',updated: '1512884233'},
                {trackingId:'2',updated: '1512885243'}];

预期数组

let sortedArray = [{trackingId: '1', updated: '1512885243'},
                   {trackingId: '1', updated: '1512885253'},
                 {trackingId: '2', updated: '1512885243'},
                 {trackingId: '3', updated: '1512884233'}]

// I am thinking something like:
describe('Sorting', () => {
      it('Array should be sortedby trackingId', () => {
        testArray.sort((a,b) => {
          if (a.trackingId !== b.trackingId) {
            return a.trackingId - b.trackingId;
          }
          return new Date(b.updated) - new Date(a.updated);
        });
        expect(component.contains(result))
          .toEqual(expect.arrayContaining(sortedArray))

});

在我正在编写单元测试的容器类中对函数进行排序:

customerTrackingInfo = customerTrackingInfo.sort( (a,b) => {
        if (a.trackingId !== b.trackingId) {
          return a.trackingId - b.trackingId;
        }
        return new Date(b.updated) - new Date(a.updated);
});

0 个答案:

没有答案