forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidationUtilsTest.js
316 lines (263 loc) · 14.6 KB
/
ValidationUtilsTest.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import {addDays, format, subYears, startOfDay} from 'date-fns';
import CONST from '../../src/CONST';
const ValidationUtils = require('../../src/libs/ValidationUtils');
describe('ValidationUtils', () => {
describe('isValidDate', () => {
test('Should return true for a valid date within the range', () => {
const validDate = '2023-07-18';
const isValid = ValidationUtils.isValidDate(validDate);
expect(isValid).toBe(true);
});
test('Should return false for an invalid date', () => {
const invalidDate = '2023-07-32';
const isValid = ValidationUtils.isValidDate(invalidDate);
expect(isValid).toBe(false);
});
test('Should return false for an empty date', () => {
const invalidDate = '';
const isValid = ValidationUtils.isValidDate(invalidDate);
expect(isValid).toBe(false);
});
test('Should return false for a date after the range', () => {
const futureDate = '3024-07-18';
const isValid = ValidationUtils.isValidDate(futureDate);
expect(isValid).toBe(false);
});
});
describe('isValidPastDate', () => {
test('Should return true for a valid past date within the range', () => {
const validPastDate = '1990-01-01';
const isValid = ValidationUtils.isValidPastDate(validPastDate);
expect(isValid).toBe(true);
});
test('Should return false for an invalid date', () => {
const invalidDate = '2023-07-32';
const isValid = ValidationUtils.isValidPastDate(invalidDate);
expect(isValid).toBe(false);
});
test('Should return false for an empty date', () => {
const emptyDate = '';
const isValid = ValidationUtils.isValidPastDate(emptyDate);
expect(isValid).toBe(false);
});
test('Should return false for a future date', () => {
const futureDate = format(addDays(new Date(), 1), CONST.DATE.FNS_FORMAT_STRING);
const isValid = ValidationUtils.isValidPastDate(futureDate);
expect(isValid).toBe(false);
});
});
describe('isRequiredFulfilled', () => {
test('Should return true for a non-empty string value', () => {
const stringValue = 'Test';
const isFulfilled = ValidationUtils.isRequiredFulfilled(stringValue);
expect(isFulfilled).toBe(true);
});
test('Should return false for an empty string value', () => {
const emptyStringValue = '';
const isFulfilled = ValidationUtils.isRequiredFulfilled(emptyStringValue);
expect(isFulfilled).toBe(false);
});
test('Should return false for a whitespace string value', () => {
const whitespaceStringValue = ' ';
const isFulfilled = ValidationUtils.isRequiredFulfilled(whitespaceStringValue);
expect(isFulfilled).toBe(false);
});
test('Should return true for a valid date value', () => {
const dateValue = new Date();
const isFulfilled = ValidationUtils.isRequiredFulfilled(dateValue);
expect(isFulfilled).toBe(true);
});
test('Should return false for an invalid date value', () => {
const invalidDateValue = new Date('2023-07-33');
const isFulfilled = ValidationUtils.isRequiredFulfilled(invalidDateValue);
expect(isFulfilled).toBe(false);
});
test('Should return true for a non-empty array value', () => {
const arrayValue = [1, 2, 3];
const isFulfilled = ValidationUtils.isRequiredFulfilled(arrayValue);
expect(isFulfilled).toBe(true);
});
test('Should return false for an empty array value', () => {
const emptyArrayValue = [];
const isFulfilled = ValidationUtils.isRequiredFulfilled(emptyArrayValue);
expect(isFulfilled).toBe(false);
});
test('Should return true for a non-empty object value', () => {
const objectValue = {key: 'value'};
const isFulfilled = ValidationUtils.isRequiredFulfilled(objectValue);
expect(isFulfilled).toBe(true);
});
test('Should return false for an empty object value', () => {
const emptyObjectValue = {};
const isFulfilled = ValidationUtils.isRequiredFulfilled(emptyObjectValue);
expect(isFulfilled).toBe(false);
});
});
describe('isValidExpirationDate', () => {
test('Should return true for a valid formats expiration date in the future', () => {
const firstFutureExpirationDate = '12/25'; // MM/YY format, in the future
const secondFutureExpirationDate = '12/2025'; // MM/YYYY format, in the future
const thirdFutureExpirationDate = '1225'; // MMYY format, in the future
const fourthFutureExpirationDate = '122025'; // MMYYYY format, in the future
expect(ValidationUtils.isValidExpirationDate(firstFutureExpirationDate)).toBe(true);
expect(ValidationUtils.isValidExpirationDate(secondFutureExpirationDate)).toBe(true);
expect(ValidationUtils.isValidExpirationDate(thirdFutureExpirationDate)).toBe(true);
expect(ValidationUtils.isValidExpirationDate(fourthFutureExpirationDate)).toBe(true);
});
test('Should return false for a valid expiration date, but in the past', () => {
const pastExpirationDate = '06/20'; // MM/YY format, in the past
const isValid = ValidationUtils.isValidExpirationDate(pastExpirationDate);
expect(isValid).toBe(false);
});
test('Should return false for an invalid expiration date format', () => {
const invalidExpirationDate = '2006'; // Invalid format, missing YYMM
const isValid = ValidationUtils.isValidExpirationDate(invalidExpirationDate);
expect(isValid).toBe(false);
});
});
describe('meetsMinimumAgeRequirement', () => {
test('Should return true for a date that meets the minimum age requirement', () => {
const validDate = format(subYears(new Date(), 18), CONST.DATE.FNS_FORMAT_STRING); // Date of birth 18 years ago
const meetsRequirement = ValidationUtils.meetsMinimumAgeRequirement(validDate);
expect(meetsRequirement).toBe(true);
});
test('Should return false for a date that does not meet the minimum age requirement', () => {
const invalidDate = format(subYears(new Date(), 17), CONST.DATE.FNS_FORMAT_STRING); // Date of birth 17 years ago
const meetsRequirement = ValidationUtils.meetsMinimumAgeRequirement(invalidDate);
expect(meetsRequirement).toBe(false);
});
test('Should return false for an invalid date', () => {
const invalidDate = '2023-07-32'; // Invalid date
const meetsRequirement = ValidationUtils.meetsMinimumAgeRequirement(invalidDate);
expect(meetsRequirement).toBe(false);
});
});
describe('meetsMaximumAgeRequirement', () => {
test('Should return true for a date that meets the maximum age requirement', () => {
const validDate = format(subYears(new Date(), 65), CONST.DATE.FNS_FORMAT_STRING); // Date of birth 65 years ago
const meetsRequirement = ValidationUtils.meetsMaximumAgeRequirement(validDate);
expect(meetsRequirement).toBe(true);
});
test('Should return false for a date that does not meet the maximum age requirement', () => {
const invalidDate = format(subYears(new Date(), 151), CONST.DATE.FNS_FORMAT_STRING); // Date of birth 151 years ago
const meetsRequirement = ValidationUtils.meetsMaximumAgeRequirement(invalidDate);
expect(meetsRequirement).toBe(false);
});
test('Should return false for an invalid date', () => {
const invalidDate = '2023-07-32'; // Invalid date
const meetsRequirement = ValidationUtils.meetsMaximumAgeRequirement(invalidDate);
expect(meetsRequirement).toBe(false);
});
});
describe('getAgeRequirementError', () => {
test('Should return an empty string for a date within the specified range', () => {
const validDate = format(subYears(new Date(), 30), CONST.DATE.FNS_FORMAT_STRING); // Date of birth 30 years ago
const error = ValidationUtils.getAgeRequirementError(validDate, 18, 150);
expect(error).toBe('');
});
test('Should return an error message for a date before the minimum age requirement', () => {
const invalidDate = format(subYears(new Date(), 17), CONST.DATE.FNS_FORMAT_STRING); // Date of birth 17 years ago
const error = ValidationUtils.getAgeRequirementError(invalidDate, 18, 150);
expect(error).toEqual(['privatePersonalDetails.error.dateShouldBeBefore', {dateString: format(startOfDay(subYears(new Date(), 18)), CONST.DATE.FNS_FORMAT_STRING)}]);
});
test('Should return an error message for a date after the maximum age requirement', () => {
const invalidDate = format(subYears(new Date(), 160), CONST.DATE.FNS_FORMAT_STRING); // Date of birth 160 years ago
const error = ValidationUtils.getAgeRequirementError(invalidDate, 18, 150);
expect(error).toEqual(['privatePersonalDetails.error.dateShouldBeAfter', {dateString: format(startOfDay(subYears(new Date(), 150)), CONST.DATE.FNS_FORMAT_STRING)}]);
});
test('Should return an error message for an invalid date', () => {
const invalidDate = '2023-07-32'; // Invalid date
const error = ValidationUtils.getAgeRequirementError(invalidDate, 18, 150);
expect(error).toBe('common.error.dateInvalid');
});
});
describe('isValidTwoFactorCode', () => {
test('numeric two-factor code', () => {
expect(ValidationUtils.isValidTwoFactorCode('123456')).toBe(true);
});
test('numeric two-factor code with leading zeroes', () => {
expect(ValidationUtils.isValidTwoFactorCode('000001')).toBe(true);
});
test('alphanumeric two-factor code', () => {
expect(ValidationUtils.isValidTwoFactorCode('abc123')).toBe(false);
});
test('special characters two-factor code', () => {
expect(ValidationUtils.isValidTwoFactorCode('!@#$%^')).toBe(false);
});
test('partial special characters two-factor code', () => {
expect(ValidationUtils.isValidTwoFactorCode('123$%^')).toBe(false);
});
});
describe('isValidRoomName', () => {
test('room name without #', () => {
expect(ValidationUtils.isValidRoomName('test')).toBe(false);
});
test('room name with upper case letters', () => {
expect(ValidationUtils.isValidRoomName('#Test')).toBe(false);
});
test('room name with special character other than dash', () => {
expect(ValidationUtils.isValidRoomName('#test_room')).toBe(false);
});
test('room name with less than one character', () => {
expect(ValidationUtils.isValidRoomName('#')).toBe(false);
});
test('room name with 81 characters', () => {
expect(ValidationUtils.isValidRoomName('#123456789012345678901234567890123456789012345678901234567890123456789012345678901')).toBe(false);
});
test('room name with lowercase letters, numbers, and dashes', () => {
expect(ValidationUtils.isValidRoomName('#this-is-a-room1')).toBe(true);
});
test('room name with spanish Accented letters and dashes', () => {
expect(ValidationUtils.isValidRoomName('#sala-de-opinión')).toBe(true);
});
test('room name with division sign (÷)', () => {
expect(ValidationUtils.isValidRoomName('#room-name-with-÷-sign')).toBe(false);
});
test('room name with Greek alphabets and Cyrillic alphabets', () => {
expect(ValidationUtils.isValidRoomName('#σοβαρός-серьезный')).toBe(true);
});
});
describe('isValidWebsite', () => {
test('Valid URLs with https protocol', () => {
expect(ValidationUtils.isValidWebsite('https://www.expensify.com')).toBe(true);
expect(ValidationUtils.isValidWebsite('https://expensify.com/inbox/')).toBe(true);
expect(ValidationUtils.isValidWebsite('https://we.are.expensify.com/how-we-got-here')).toBe(true);
expect(ValidationUtils.isValidWebsite('https://blog.google')).toBe(true);
expect(ValidationUtils.isValidWebsite('https://blog.google:65535')).toBe(true);
});
test('Valid URLs with http protocol', () => {
expect(ValidationUtils.isValidWebsite('http://www.expensify.com')).toBe(true);
expect(ValidationUtils.isValidWebsite('http://use.expensify.com')).toBe(true);
});
test('Valid URL with ftp protocol', () => {
expect(ValidationUtils.isValidWebsite('ftp://expensify.com/files')).toBe(true);
});
test('Invalid URLs', () => {
expect(ValidationUtils.isValidWebsite('expensify')).toBe(false);
expect(ValidationUtils.isValidWebsite('expensify.')).toBe(false);
expect(ValidationUtils.isValidWebsite('192.168.0.1')).toBe(false);
expect(ValidationUtils.isValidWebsite('www.googlecom')).toBe(false);
expect(ValidationUtils.isValidWebsite('www.google.com:65536')).toBe(false);
});
test('Invalid URLs without protocols', () => {
expect(ValidationUtils.isValidWebsite('www.expensify.com')).toBe(false);
expect(ValidationUtils.isValidWebsite('expensify.com')).toBe(false);
});
test('Invalid URLs with special characters and emojis', () => {
expect(ValidationUtils.isValidWebsite('www.~expensify.com')).toBe(false);
expect(ValidationUtils.isValidWebsite('https://www.expen$ify.com')).toBe(false);
expect(ValidationUtils.isValidWebsite('www.expensify😄.com')).toBe(false);
});
});
describe('ValidateAccountRoute', () => {
test('Valid account route', () => {
expect(ValidationUtils.isValidAccountRoute(123123)).toBe(true);
expect(ValidationUtils.isValidAccountRoute(5612)).toBe(true);
});
test('Invalid account route', () => {
expect(ValidationUtils.isValidAccountRoute(undefined)).toBe(false);
expect(ValidationUtils.isValidAccountRoute(0)).toBe(false);
expect(ValidationUtils.isValidAccountRoute('123aaa')).toBe(false);
});
});
});