-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboolean.test.ts
More file actions
87 lines (74 loc) · 2.65 KB
/
Copy pathboolean.test.ts
File metadata and controls
87 lines (74 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { describe, expect, it } from "vitest";
import { toBoolean, isTruthy, isFalsy } from "../src/common/utils/boolean.js";
describe("toBoolean", () => {
describe("strings — truthy set", () => {
it.each(["true", "t", "yes", "y", "on", "1"])('returns true for "%s"', value => {
expect(toBoolean(value)).toBe(true);
});
it.each(["TRUE", "True", "YES", "Yes", "ON", "On", "T", "Y"])(
'returns true for uppercase/mixed "%s"',
value => {
expect(toBoolean(value)).toBe(true);
}
);
it.each([" true ", " 1 ", " yes "])('returns true for whitespace-padded "%s"', value => {
expect(toBoolean(value)).toBe(true);
});
});
describe("strings — falsy set", () => {
it.each(["false", "f", "no", "n", "off", "0", "", "banana", "2", "null"])(
'returns false for "%s"',
value => {
expect(toBoolean(value)).toBe(false);
}
);
});
describe("numbers", () => {
it("returns true for 1", () => {
expect(toBoolean(1)).toBe(true);
});
it.each([0, 2, -1, 100, NaN, Infinity, -Infinity])("returns false for %s", value => {
expect(toBoolean(value)).toBe(false);
});
});
describe("booleans", () => {
it("returns true for true", () => {
expect(toBoolean(true)).toBe(true);
});
it("returns false for false", () => {
expect(toBoolean(false)).toBe(false);
});
});
describe("other types", () => {
it.each([null, undefined, {}, [], () => {}, Symbol("x")])(
"returns false for non-string/number/boolean values",
value => {
expect(toBoolean(value)).toBe(false);
}
);
});
});
describe("isTruthy", () => {
it("mirrors toBoolean for a truthy value", () => {
expect(isTruthy("true")).toBe(true);
expect(isTruthy(1)).toBe(true);
expect(isTruthy(true)).toBe(true);
});
it("mirrors toBoolean for a falsy value", () => {
expect(isTruthy("false")).toBe(false);
expect(isTruthy(0)).toBe(false);
expect(isTruthy(null)).toBe(false);
});
});
describe("isFalsy", () => {
it("is the inverse of toBoolean for a truthy value", () => {
expect(isFalsy("true")).toBe(false);
expect(isFalsy(1)).toBe(false);
expect(isFalsy(true)).toBe(false);
});
it("is the inverse of toBoolean for a falsy value", () => {
expect(isFalsy("false")).toBe(true);
expect(isFalsy(0)).toBe(true);
expect(isFalsy(null)).toBe(true);
});
});