forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorUtilsTest.js
67 lines (51 loc) · 3.28 KB
/
ErrorUtilsTest.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
import * as ErrorUtils from '../../src/libs/ErrorUtils';
describe('ErrorUtils', () => {
test('should add a new error message for a given inputID', () => {
const errors = {};
ErrorUtils.addErrorMessage(errors, 'username', 'Username cannot be empty');
expect(errors).toEqual({username: ['Username cannot be empty', {isTranslated: true}]});
});
test('should append an error message to an existing error message for a given inputID', () => {
const errors = {username: 'Username cannot be empty'};
ErrorUtils.addErrorMessage(errors, 'username', 'Username must be at least 6 characters long');
expect(errors).toEqual({username: ['Username cannot be empty\nUsername must be at least 6 characters long', {isTranslated: true}]});
});
test('should add an error to input which does not contain any errors yet', () => {
const errors = {username: 'Username cannot be empty'};
ErrorUtils.addErrorMessage(errors, 'password', 'Password cannot be empty');
expect(errors).toEqual({username: 'Username cannot be empty', password: ['Password cannot be empty', {isTranslated: true}]});
});
test('should not mutate the errors object when message is empty', () => {
const errors = {username: 'Username cannot be empty'};
ErrorUtils.addErrorMessage(errors, 'username', '');
expect(errors).toEqual({username: 'Username cannot be empty'});
});
test('should not mutate the errors object when inputID is null', () => {
const errors = {username: 'Username cannot be empty'};
ErrorUtils.addErrorMessage(errors, null, 'InputID cannot be null');
expect(errors).toEqual({username: 'Username cannot be empty'});
});
test('should not mutate the errors object when message is null', () => {
const errors = {username: 'Username cannot be empty'};
ErrorUtils.addErrorMessage(errors, 'username', null);
expect(errors).toEqual({username: 'Username cannot be empty'});
});
test('should add multiple error messages for the same inputID', () => {
const errors = {};
ErrorUtils.addErrorMessage(errors, 'username', 'Username cannot be empty');
ErrorUtils.addErrorMessage(errors, 'username', 'Username must be at least 6 characters long');
ErrorUtils.addErrorMessage(errors, 'username', 'Username must contain at least one letter');
expect(errors).toEqual({username: ['Username cannot be empty\nUsername must be at least 6 characters long\nUsername must contain at least one letter', {isTranslated: true}]});
});
test('should append multiple error messages to an existing error message for the same inputID', () => {
const errors = {username: 'Username cannot be empty\nUsername must be at least 6 characters long'};
ErrorUtils.addErrorMessage(errors, 'username', 'Username must contain at least one letter');
ErrorUtils.addErrorMessage(errors, 'username', 'Username must not contain special characters');
expect(errors).toEqual({
username: [
'Username cannot be empty\nUsername must be at least 6 characters long\nUsername must contain at least one letter\nUsername must not contain special characters',
{isTranslated: true},
],
});
});
});