-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.test.ts
73 lines (63 loc) · 1.78 KB
/
forms.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
import { RegisterFormSchema } from 'src/schemas/forms'
import { RegisterFormType } from 'src/types/forms'
/**
* Test the RegisterFormSchema.
* This validation is very similar to the LoginFormSchema.
* @param data is the data to test.
*
* @returns a boolean indicating whether the data is valid.
*/
const isValid = (data: RegisterFormType) => {
try {
RegisterFormSchema.parse(data)
} catch (error) {
if (error) {
return false
}
}
return true
}
describe('Forms', () => {
const invalidUsername = 'a'
const validUsername = 'test'
const invalidEmail = 'invalidEmail'
const validEmail = 'test.test@example.com'
const invalidPassword = '12345'
const validPassword = '123456'
it('only allows valid email', () => {
const actual: RegisterFormType = {
email: invalidEmail,
username: 'test',
password: 'password',
repeatPassword: 'password'
}
expect(isValid(actual)).toBe(false)
expect(isValid({ ...actual, email: validEmail })).toBe(true)
})
it('only allows valid username', () => {
const actual: RegisterFormType = {
email: validEmail,
username: invalidUsername,
password: 'password',
repeatPassword: 'password'
}
expect(isValid(actual)).toBe(false)
expect(isValid({ ...actual, username: validUsername })).toBe(true)
})
it('only allows valid password', () => {
const invalid: RegisterFormType = {
email: validEmail,
username: validUsername,
password: invalidPassword,
repeatPassword: invalidPassword
}
const valid: RegisterFormType = {
email: validEmail,
username: validUsername,
password: validPassword,
repeatPassword: validPassword
}
expect(isValid(invalid)).toBe(false)
expect(isValid(valid)).toBe(true)
})
})