UNPKG

@applicaster/zapp-react-native-utils

Version:

Applicaster Zapp React Native utilities package

41 lines (31 loc) 1.09 kB
import { take } from "../take"; describe("take", () => { it("takes n elements from the beginning", () => { expect(take(2, [1, 2, 3])).toEqual([1, 2]); }); it("returns the whole array if n is larger than length", () => { expect(take(5, [1, 2, 3])).toEqual([1, 2, 3]); }); it("returns empty array if n is 0", () => { expect(take(0, [1, 2, 3])).toEqual([]); }); it("returns empty array for empty input array", () => { expect(take(2, [])).toEqual([]); }); it("returns empty array if n is negative", () => { expect(take(-1, [1, 2, 3])).toEqual([]); }); it("works with strings in array", () => { expect(take(2, ["a", "b", "c"])).toEqual(["a", "b"]); }); it("works with objects in array", () => { const arr = [{ id: 1 }, { id: 2 }]; expect(take(1, arr)).toEqual([{ id: 1 }]); }); it("returns empty array if input is not an array", () => { // @ts-expect-error testing non-array input expect(take(2, null)).toEqual([]); // @ts-expect-error testing non-array input expect(take(2, undefined)).toEqual([]); }); });