UNPKG

2.52 kBPlain TextView Raw
1import { describe, expect, test } from "vitest";
2
3import isEmpty from "../src/index";
4
5describe("Truthy cases", () => {
6 test("empty array", () => {
7 // biome-ignore lint/suspicious/noExplicitAny: <explanation>
8 const value: any[] = [];
9 expect(isEmpty(value)).toBeTruthy();
10 });
11
12 test("empty object", () => {
13 const value = {};
14 expect(isEmpty(value)).toBeTruthy();
15 });
16
17 test("empty string", () => {
18 const value = "";
19 expect(isEmpty(value)).toBeTruthy();
20 });
21
22 test("number zero", () => {
23 const value = 0;
24 expect(isEmpty(value)).toBeTruthy();
25 });
26
27 test("non-existing array item", () => {
28 const value = ["Dhaka", ["Uttara", [1, 2]]];
29 expect(isEmpty(value[10])).toBeTruthy();
30 });
31
32 test("non-existing object item", () => {
33 const value = {
34 city: "Dhaka",
35 location: {
36 latitude: "23.809473999508782",
37 longitude: "90.4151957081839",
38 },
39 };
40 // @ts-ignore
41 expect(isEmpty(value.capital)).toBeTruthy();
42 });
43
44 test("non-existing nested array item", () => {
45 const value = ["Dhaka", ["Uttara", [1, 2]]];
46 // @ts-ignore
47 expect(isEmpty(value[0][100])).toBeTruthy();
48 });
49
50 test("non-existing nested object item", () => {
51 const value = {
52 city: "Dhaka",
53 location: {
54 latitude: "23.809473999508782",
55 longitude: "90.4151957081839",
56 },
57 };
58 // @ts-ignore
59 expect(isEmpty(value.location.area)).toBeTruthy();
60 });
61});
62
63describe("Falsy cases", () => {
64 test("negative number", () => {
65 const value = -1;
66 expect(isEmpty(value)).toBeFalsy();
67 });
68
69 test("positive number", () => {
70 const value = 1;
71 expect(isEmpty(value)).toBeFalsy();
72 });
73
74 test("array item", () => {
75 const value = ["Dhaka", ["Uttara", [1, 2]]];
76 expect(isEmpty(value[0])).toBeFalsy();
77 });
78
79 test("object item", () => {
80 const value = {
81 city: "Dhaka",
82 location: {
83 latitude: "23.809473999508782",
84 longitude: "90.4151957081839",
85 },
86 };
87 // @ts-ignore
88 expect(isEmpty(value.city)).toBeFalsy();
89 });
90
91 test("nested array item", () => {
92 const value = ["Dhaka", ["Uttara", [1, 2]]];
93 // @ts-ignore
94 expect(isEmpty(value[0][0])).toBeFalsy();
95 });
96
97 test("nested object item", () => {
98 const value = {
99 city: "Dhaka",
100 location: {
101 latitude: "23.809473999508782",
102 longitude: "90.4151957081839",
103 },
104 };
105 // @ts-ignore
106 expect(isEmpty(value.location.latitude)).toBeFalsy();
107 });
108});