forked from remeda/remeda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isEmpty.test.ts
135 lines (112 loc) · 3.6 KB
/
isEmpty.test.ts
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { isEmpty } from './isEmpty';
describe('isEmpty', () => {
test('returns true for an empty array', () => {
expect(isEmpty([])).toBe(true);
});
test('returns false for a non-empty array', () => {
expect(isEmpty([1, 2, 3])).toBe(false);
});
test('returns true for an empty string', () => {
expect(isEmpty('')).toBe(true);
});
test('returns false for a non-empty string', () => {
expect(isEmpty('test')).toBe(false);
});
test('returns true for an empty object', () => {
expect(isEmpty({})).toBe(true);
});
test('returns false for a non-empty object', () => {
expect(isEmpty({ length: 0 })).toBe(false);
});
test('returns true for undefined', () => {
expect(isEmpty(undefined)).toBe(true);
});
test('does not accept invalid input types', () => {
// @ts-expect-error [ts2769] number is not a valid input type
isEmpty(2);
// @ts-expect-error [ts2769] boolean is not a valid input type
isEmpty(false);
// @ts-expect-error [ts2769] null is not a valid input type
isEmpty(null);
// @ts-expect-error [ts2769] undefined is only allowed with strings
isEmpty([] as ReadonlyArray<string> | undefined);
// @ts-expect-error [ts2769] undefined is only allowed with strings
isEmpty({} as Record<string, string> | undefined);
});
});
describe('strings are narrowed correctly', () => {
test('just undefined', () => {
const data = undefined;
if (isEmpty(data)) {
expectTypeOf(data).toEqualTypeOf<undefined>();
}
});
test('just string', () => {
const data = '' as string;
if (isEmpty(data)) {
expectTypeOf(data).toEqualTypeOf<''>();
}
});
test('just EMPTY string', () => {
const data = '' as const;
if (isEmpty(data)) {
expectTypeOf(data).toEqualTypeOf<''>();
}
});
test('string or undefined', () => {
const data = undefined as string | undefined;
if (isEmpty(data)) {
expectTypeOf(data).toEqualTypeOf<'' | undefined>();
}
});
test('string literals that CANT be empty or undefined', () => {
const data = 'cat' as 'cat' | 'dog';
if (isEmpty(data)) {
// unreachable
expectTypeOf(data).toEqualTypeOf<never>();
}
});
test('string literals that CAN be empty', () => {
const data = 'cat' as 'cat' | 'dog' | '';
if (isEmpty(data)) {
expectTypeOf(data).toEqualTypeOf<''>();
}
});
test('string literals that CAN be undefined', () => {
const data = 'cat' as 'cat' | 'dog' | undefined;
if (isEmpty(data)) {
expectTypeOf(data).toEqualTypeOf<undefined>();
}
});
test('string literals that CAN be undefined or empty', () => {
const data = 'cat' as 'cat' | 'dog' | '' | undefined;
if (isEmpty(data)) {
expectTypeOf(data).toEqualTypeOf<'' | undefined>();
}
});
test('string templates that CANT be empty or undefined', () => {
const data = 'prefix_0' as `prefix_${number}`;
if (isEmpty(data)) {
// unreachable
expectTypeOf(data).toEqualTypeOf<never>();
}
});
test('string templates that CAN be empty', () => {
const data = '' as `prefix_${number}` | '';
if (isEmpty(data)) {
expectTypeOf(data).toEqualTypeOf<''>();
}
});
test('string templates that CAN be undefined', () => {
const data = 'prefix_0' as `prefix_${number}` | undefined;
if (isEmpty(data)) {
expectTypeOf(data).toEqualTypeOf<undefined>();
}
});
test('string templates that CAN be undefined or empty', () => {
const data = 'prefix_0' as `prefix_${number}` | '' | undefined;
if (isEmpty(data)) {
expectTypeOf(data).toEqualTypeOf<'' | undefined>();
}
});
});