Repository navigation
Expand file tree
/
Copy pathindex.spec.ts
More file actions
211 lines (175 loc) · 5.5 KB
/
Copy pathindex.spec.ts
File metadata and controls
211 lines (175 loc) · 5.5 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { describe, it, test, expect } from 'vitest'
import {
isArrayNotEmpty,
isArrayEmpty,
isObjectEmpty,
isObject,
isNil,
isNotNil,
isDate,
toDate,
toDateFormat,
isStringEmpty,
ensureString,
} from './index'
describe('isArrayNotEmpty', () => {
test('returns true for non-empty arrays', () => {
expect(isArrayNotEmpty([1, 2, 3])).toBe(true)
})
test('returns false for empty arrays', () => {
expect(isArrayNotEmpty([])).toBe(false)
})
test('returns false for non-array values', () => {
expect(isArrayNotEmpty(123)).toBe(false)
expect(isArrayNotEmpty('abc')).toBe(false)
expect(isArrayNotEmpty({})).toBe(false)
expect(isArrayNotEmpty(null)).toBe(false)
expect(isArrayNotEmpty(undefined)).toBe(false)
})
})
describe('isArrayEmpty', () => {
test('returns false for non-empty arrays', () => {
expect(isArrayEmpty([1, 2, 3])).toBe(false)
})
test('returns true for empty arrays', () => {
expect(isArrayEmpty([])).toBe(true)
})
test('returns false for non-array values', () => {
expect(isArrayEmpty(123)).toBe(false)
expect(isArrayEmpty('abc')).toBe(false)
expect(isArrayEmpty({})).toBe(false)
expect(isArrayEmpty(null)).toBe(false)
expect(isArrayEmpty(undefined)).toBe(false)
})
})
describe('isObjectEmpty', () => {
test('returns true for empty objects', () => {
expect(isObjectEmpty({})).toBe(true)
})
test('returns false for non-empty objects', () => {
expect(isObjectEmpty({ a: 1, b: 2 })).toBe(false)
})
test('returns false for non-object values', () => {
expect(isObjectEmpty(123)).toBe(false)
expect(isObjectEmpty('abc')).toBe(false)
expect(isObjectEmpty([])).toBe(false)
expect(isObjectEmpty(null)).toBe(false)
expect(isObjectEmpty(undefined)).toBe(false)
})
})
describe('isObject', () => {
test('returns true for objects', () => {
expect(isObject({})).toBe(true)
expect(isObject({ a: 1, b: 2 })).toBe(true)
})
test('returns false for non-object values', () => {
expect(isObject(123)).toBe(false)
expect(isObject('abc')).toBe(false)
expect(isObject([])).toBe(false)
expect(isObject(null)).toBe(false)
expect(isObject(undefined)).toBe(false)
})
})
describe('isNil', () => {
test('returns true for null and undefined', () => {
expect(isNil(null)).toBe(true)
expect(isNil(undefined)).toBe(true)
})
test('returns false for other values', () => {
expect(isNil(123)).toBe(false)
expect(isNil('abc')).toBe(false)
expect(isNil({})).toBe(false)
expect(isNil([])).toBe(false)
})
})
describe('isNotNil', () => {
it('should return true for a non-nil value', () => {
expect(isNotNil('foo')).toBe(true)
})
it('should return false for a nil value', () => {
expect(isNotNil(null)).toBe(false)
})
})
describe('isDate', () => {
it('returns true for a Date object', () => {
expect(isDate(new Date())).toBe(true)
})
it('returns false for a string', () => {
expect(isDate('2023-02-07')).toBe(false)
})
it('returns false for a number', () => {
expect(isDate(123456789)).toBe(false)
})
})
describe('toDate', () => {
it('returns a Date object for a valid string date', () => {
const date = toDate('2023-02-07')
expect(isDate(date)).toBe(true)
})
it('returns a Date object for a valid numeric timestamp', () => {
const date = toDate(1612738400000)
expect(isDate(date)).toBe(true)
})
it('returns undefined for an invalid date string', () => {
expect(toDate('not a date')).toBe(undefined)
})
it('returns undefined for an invalid numeric value', () => {
expect(toDate('not a number')).toBe(undefined)
})
})
describe('toDateFormat', () => {
it('returns an empty string for a null date', () => {
expect(toDateFormat(undefined)).toBe('')
})
it('returns a formatted date string for a valid date and default format', () => {
expect(toDateFormat(new Date('2023-02-07 00:00:00'))).toBe('2023-02-07')
})
it('returns a default formatted date string for a unsupported custom format', () => {
expect(toDateFormat(new Date('2023-02-07 00:00:00'), 'dd/mm/yyyy')).toBe(
'Tue Feb 07 2023',
)
})
})
describe('isStringEmpty', () => {
it('returns true for an empty string', () => {
expect(isStringEmpty('')).toBe(true)
})
it('returns false for a non-empty string', () => {
expect(isStringEmpty('hello')).toBe(false)
})
it('returns false for a string with only spaces', () => {
expect(isStringEmpty(' ')).toBe(false)
})
it('returns false for null', () => {
expect(isStringEmpty(null)).toBe(false)
})
it('returns false for undefined', () => {
expect(isStringEmpty(undefined)).toBe(false)
})
it('returns false for non-string values', () => {
expect(isStringEmpty(123)).toBe(false)
expect(isStringEmpty({})).toBe(false)
expect(isStringEmpty([])).toBe(false)
expect(isStringEmpty(true)).toBe(false)
})
})
describe('ensureString', () => {
it('returns the same string for string input', () => {
expect(ensureString('hello')).toBe('hello')
})
it('returns empty string for null', () => {
expect(ensureString(null)).toBe('')
})
it('returns empty string for undefined', () => {
expect(ensureString(undefined)).toBe('')
})
it('returns empty string for non-string values', () => {
expect(ensureString(123)).toBe('')
expect(ensureString({})).toBe('')
expect(ensureString([])).toBe('')
expect(ensureString(true)).toBe('')
})
it('returns empty string for empty string input', () => {
expect(ensureString('')).toBe('')
})
})