forked from logaretm/vee-validate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary.js
146 lines (117 loc) · 3.26 KB
/
dictionary.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
import Dictionary from './../src/dictionary';
test('does not merge if a non object is provided', () => {
const dict = new Dictionary('a string');
expect(dict.hasLocale('en')).toBe(false);
});
test('can check for locale existance', () => {
const dict = new Dictionary({
en: { }
});
expect(dict.hasLocale('en')).toBe(true);
expect(dict.hasLocale('ar')).toBe(false);
});
test('can check for message existance', () => {
const dict = new Dictionary({
en: {
messages: {
winter: 'Winter is coming'
}
}
});
expect(dict.hasMessage('en', 'winter')).toBe(true);
});
test('can fetch a message', () => {
const dict = new Dictionary({
en: {
messages: {
winter: 'Winter is coming'
}
}
});
expect(dict.getMessage('en', 'winter')).toBe('Winter is coming');
});
test('can check for attribute existance', () => {
const dict = new Dictionary({
en: {
attributes: {
email: 'Email Address'
}
}
});
expect(dict.hasAttribute('en', 'email')).toBe(true);
});
test('can fetch an attribute', () => {
const dict = new Dictionary({
en: {
attributes: {
email: 'Email Address'
}
}
});
expect(dict.getAttribute('en', 'email')).toBe('Email Address');
});
test('can default to a fallback if message or attribute does not exist', () => {
const dict = new Dictionary();
expect(dict.getMessage('en', 'winter', 'Winter is still coming')).toBe('Winter is still coming');
expect(dict.getAttribute('en', 'john', 'Knows nothing')).toBe('Knows nothing');
});
test('can set messages', () => {
const dict = new Dictionary();
dict.setMessage('en', 'winter', 'Winter is coming');
expect(dict.getMessage('en', 'winter')).toBe('Winter is coming');
});
test('can set attributes', () => {
const dict = new Dictionary();
dict.setAttribute('en', 'email', 'Email Address');
expect(dict.getAttribute('en', 'email')).toBe('Email Address');
});
test('returns the default message for the given locale when no fallback is provided', () => {
const dict = new Dictionary({
en: {
messages: {
_default: 'This is default'
}
},
ar: {
messages: {
_default: 'رسالة افتراضية'
}
},
fr: {
messages: {}
}
});
// default for locale
expect(dict.getMessage('ar', 'any')).toBe('رسالة افتراضية');
// no default message
expect(dict.getMessage('fr', 'any')).toBe('This is default');
});
test('custom messages can be provided for specific fields', () => {
const dict = new Dictionary({
en: {
messages: {
alpha: 'not letters'
}
}
});
expect(dict.getMessage('en', 'alpha')).toBe('not letters');
// test fallback
expect(dict.getFieldMessage('en', 'name', 'alpha')).toBe('not letters');
dict.merge({
en: {
custom: {
name: {
alpha: 'custom message'
}
}
}
});
expect(dict.getFieldMessage('en', 'name', 'alpha')).toBe('custom message');
});
test('able to get and set dateformat attributes', () => {
const dict = new Dictionary();
dict.setDateFormat('en', 'MM/DD/YYYY');
dict.setDateFormat('ar', 'DD/MM/YYYY');
expect(dict.getDateFormat('en')).toBe('MM/DD/YYYY');
expect(dict.getDateFormat('ar')).toBe('DD/MM/YYYY');
});