-
-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathemail.test.ts
More file actions
313 lines (247 loc) · 8.56 KB
/
Copy pathemail.test.ts
File metadata and controls
313 lines (247 loc) · 8.56 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
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
import { describe, expect, test } from 'vitest';
import { EMAIL_REGEX } from '../../regex.ts';
import type { StringIssue } from '../../schemas/index.ts';
import { expectActionIssue, expectNoActionIssue } from '../../vitest/index.ts';
import { email, type EmailAction, type EmailIssue } from './email.ts';
describe('email', () => {
describe('should return action object', () => {
const baseAction: Omit<EmailAction<string, never>, 'message'> = {
kind: 'validation',
type: 'email',
reference: email,
expects: null,
requirement: EMAIL_REGEX,
async: false,
'~run': expect.any(Function),
};
test('with undefined message', () => {
const action: EmailAction<string, undefined> = {
...baseAction,
message: undefined,
};
expect(email()).toStrictEqual(action);
expect(email(undefined)).toStrictEqual(action);
});
test('with string message', () => {
expect(email('message')).toStrictEqual({
...baseAction,
message: 'message',
} satisfies EmailAction<string, string>);
});
test('with function message', () => {
const message = () => 'message';
expect(email(message)).toStrictEqual({
...baseAction,
message,
} satisfies EmailAction<string, typeof message>);
});
});
describe('should return dataset without issues', () => {
const action = email();
// General tests
test('for untyped inputs', () => {
const issues: [StringIssue] = [
{
kind: 'schema',
type: 'string',
input: null,
expected: 'string',
received: 'null',
message: 'message',
},
];
expect(
action['~run']({ typed: false, value: null, issues }, {})
).toStrictEqual({
typed: false,
value: null,
issues,
});
});
test('for simple email', () => {
expectNoActionIssue(action, ['email@example.com']);
});
test('for very short email', () => {
expectNoActionIssue(action, ['a@b.cd']);
});
// Start of local part
test('for underscore in beginning of local part', () => {
expectNoActionIssue(action, ['_email@example.com']);
});
test('for hyphen in beginning of local part', () => {
expectNoActionIssue(action, ['-email@example.com']);
});
test('for plus in beginning of local part', () => {
expectNoActionIssue(action, ['+email@example.com']);
});
// End of local part
test('for underscore in end of local part', () => {
expectNoActionIssue(action, ['email_@example.com']);
});
test('for hyphen in end of local part', () => {
expectNoActionIssue(action, ['email-@example.com']);
});
test('for plus in end of local part', () => {
expectNoActionIssue(action, ['email+@example.com']);
});
// Middle of local part
test('for dot in local part', () => {
expectNoActionIssue(action, ['firstname.lastname@example.com']);
});
test('for underscore in local part', () => {
expectNoActionIssue(action, ['firstname_lastname@example.com']);
});
test('for hyphen in local part', () => {
expectNoActionIssue(action, ['firstname-lastname@example.com']);
});
test('for plus in local part', () => {
expectNoActionIssue(action, ['firstname+lastname@example.com']);
});
// Special local parts
test('for numerical local part', () => {
expectNoActionIssue(action, ['1234567890@example.com']);
});
test('for underscore local part', () => {
expectNoActionIssue(action, ['_______@example.com']);
});
// Domain part variations
test('for hyphen in domain part', () => {
expectNoActionIssue(action, ['email@example-domain.com']);
});
test('for domain with subdomain', () => {
expectNoActionIssue(action, ['email@subdomain.example.com']);
});
test('for subdomain and hyphen in domain', () => {
expectNoActionIssue(action, ['email@subdomain.example-domain.com']);
});
test('for long top level domain', () => {
expectNoActionIssue(action, ['email@example.technology']);
});
test('for country code TLD', () => {
expectNoActionIssue(action, ['email@example.co.uk']);
});
test('for subdomain and country code TLD', () => {
expectNoActionIssue(action, ['email@subdomain.example.co.uk']);
});
test('for numerical domain', () => {
expectNoActionIssue(action, ['email@123.com']);
});
});
describe('should return dataset with issues', () => {
const action = email('message');
const baseIssue: Omit<EmailIssue<string>, 'input' | 'received'> = {
kind: 'validation',
type: 'email',
expected: null,
message: 'message',
requirement: EMAIL_REGEX,
};
// General tests
test('for empty strings', () => {
expectActionIssue(action, baseIssue, ['', ' ', '\n']);
});
test('for simple strings', () => {
expectActionIssue(action, baseIssue, ['email', 'emailexamplecom']);
});
test('for email with spaces', () => {
expectActionIssue(action, baseIssue, [
' email@example.com',
'e mail@example.com',
'email @example.com',
'email@ example.com',
'email@exa mple.com',
'email@example. com',
'email@example.com ',
]);
});
// Missing parts
test('for missing local part', () => {
expectActionIssue(action, baseIssue, ['@example.com']);
});
test('for missing @ symbol', () => {
expectActionIssue(action, baseIssue, [
'example.com',
'email.example.com',
]);
});
test('for missing domain part', () => {
expectActionIssue(action, baseIssue, ['email@']);
});
test('for missing domain name', () => {
expectActionIssue(action, baseIssue, ['email@.com']);
});
test('for missing TLD', () => {
expectActionIssue(action, baseIssue, ['email@example']);
});
// Invalid repeated chars
test('for two following dots in local part', () => {
expectActionIssue(action, baseIssue, ['email..email@example.com']);
});
test('for two following @ symbols', () => {
expectActionIssue(action, baseIssue, ['email@@example.com']);
});
test('for dot in end of domain name', () => {
expectActionIssue(action, baseIssue, ['email@example..com']);
});
// Beginning and end of local part
test('for dot in beginning of local part', () => {
expectActionIssue(action, baseIssue, ['.email@example.com']);
});
test('for dot in end of local part', () => {
expectActionIssue(action, baseIssue, ['email.@example.com']);
});
// Beginning and end of domain part
test('for underscore in beginning of domain part', () => {
expectActionIssue(action, baseIssue, ['email@_example.com']);
});
test('for hypen in beginning of domain part', () => {
expectActionIssue(action, baseIssue, ['email@-example.com']);
});
test('for plus in beginning of domain part', () => {
expectActionIssue(action, baseIssue, ['email@+example.com']);
});
test('for dot in beginning of domain part', () => {
expectActionIssue(action, baseIssue, ['email@.example.com']);
});
test('for underscore in end of domain name', () => {
expectActionIssue(action, baseIssue, ['email@example_.com']);
});
test('for hypen in end of domain name', () => {
expectActionIssue(action, baseIssue, ['email@example-.com']);
});
test('for plus in end of domain name', () => {
expectActionIssue(action, baseIssue, ['email@example+.com']);
});
test('for numerical TLD', () => {
expectActionIssue(action, baseIssue, ['email@example.123']);
});
test('for single char TLD', () => {
expectActionIssue(action, baseIssue, ['email@example.a']);
});
// Other special cases
test('for repeated domain part', () => {
expectActionIssue(action, baseIssue, ['email@example@example.com']);
});
test('for special chars', () => {
expectActionIssue(action, baseIssue, [
'#$&%@example.com',
'email@#$&%.com',
'email@example.#$&%',
]);
});
test('for non ASCII chars', () => {
expectActionIssue(action, baseIssue, [
'あいうえお@example.com',
'email@あいうえお.com',
'email@example.あいう',
'Kſ@example.com',
]);
});
test('for email username', () => {
expectActionIssue(action, baseIssue, [
'Joe Smith <email@example.com>',
'email@example.com (Joe Smith)',
]);
});
});
});