首页 > 解决方案 > 如何让我的测试将非多维数组传递给我的 React 测试?

问题描述

我有一个正在编写测试的实用程序方法,并且我正在尝试传递参数以准确描述使用情况并生成非多维数组。这是我的代码

const arrayInsert = (arr: any[], index: number, ...newItems) => [
    ...arr.slice(0, index),
    ...newItems,
    ...arr.slice(index)
];

export default arrayInsert;

如您所见,最后一个参数是spread运算符。现在我将参数传递给我的测试,以生成一个多维数组,而不是像这个例子一样添加到数组中:

arrayInsert(["A", "B", "F"], 1, "C", "D", "E");

// produces
["A", "C", "D", "E", "B", "F"]
所以我希望我的测试能够准确地描述上面的例子。

import arrayInsert from "./arrayInsert";

describe("arrayInsert", () => {
    it("should return array containing an array of new items", () => {
        const arr = ["A", "B", "F"];
        const newItems = ["C", "D", "E"];
        const index = 1;
        const expected = ["A", ["C", "D", "E"], "B", "F"];

        expect(arrayInsert(arr, index, newItems)).toEqual(expected);
    });
});

我怎样才能使结果一维?

标签: arraysreactjsjestjs

解决方案


推荐阅读