-
Notifications
You must be signed in to change notification settings - Fork 42
/
test.js
182 lines (142 loc) · 6.33 KB
/
test.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
import test from 'ava';
import CSVFileValidator from './src/csv-file-validator';
const requiredError = (headerName, rowNumber, columnNumber) => (
`<div class="red">${headerName} is required in the <strong>${rowNumber} row</strong> / <strong>${columnNumber} column</strong></div>`
)
const validateError = (headerName, rowNumber, columnNumber) => (
`<div class="red">${headerName} is not valid in the <strong>${rowNumber} row</strong> / <strong>${columnNumber} column</strong></div>`
)
const dependentValidateError = (headerName, rowNumber, columnNumber) => (
`<div class="red">${headerName} is not valid. Country should be set to Ukraine. <strong>${rowNumber} row</strong> / <strong>${columnNumber} column</strong></div>`
)
const isEmailValid = (email) => {
const reqExp = /[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$/
return reqExp.test(email)
}
const isRoleForCountryValid = (country, row) => {
const role = row[5];
return country === 'Ukraine' && role === 'user';
}
const isPasswordValid = (password) => (password.length >= 4)
const uniqueError = (headerName, rowNumber) => (`<div class="red">${headerName} is not unique at the <strong>${rowNumber} row</strong></div>`)
const CSVConfig = {
headers: [
{ name: 'First Name', inputName: 'firstName', required: true, requiredError },
{ name: 'Last Name', inputName: 'lastName', required: true, requiredError },
{ name: 'Email', inputName: 'email', required: true, requiredError, unique: true, uniqueError, validate: isEmailValid, validateError },
{ name: 'Password', inputName: 'password', required: true, requiredError, validate: isPasswordValid, validateError },
{ name: 'Roles', inputName: 'roles', required: true, requiredError, isArray: true },
{ name: 'Country', inputName: 'country', optional: true, dependentValidate: isRoleForCountryValid, dependentValidateError }
]
}
const CSVHeader = CSVConfig.headers.map(i => i.name).join(';');
const CSVConfig_1 = {
...CSVConfig,
parserConfig: {
dynamicTyping: true
},
isColumnIndexAlphabetic: true
}
const CSVInvalidFile = [
CSVHeader,
'Vasyl;Stokolosa;v.stokol@gmail.com;123;admin,manager;',
'Vasyl_2;"";v.stokol@gmail.com;123123123;user;',
].join('\n');
const CSVValidFile = [
CSVHeader,
'Vasyl;Stokolosa;v.stokol@gmail.com;123123;user;Ukraine',
'Vasyl;Stokolosa;fake@test.com;123123123;user;Ukraine',
].join('\n');
const CSVValidFileWithoutHeaders = [
'Vasyl;Stokolosa;v.stokol@gmail.com;123123;admin,manager',
'Vasyl;Stokolosa;fake@test.com;123123123;user;Ukraine',
].join('\n');
const CSVInvalidFileWithDuplicates = [
CSVHeader,
'Vasyl;Stokolosa;fake@test.com;123123;admin,manager;',
'Vasyl;Stokolosa;fake@test.com;123123123;user;Ukraine',
'Vasyl;Stokolosa;fake@test.com;123123123;user;Ukraine',
].join('\n');
const CSVInvalidFileTooManyFields = [
'First Name;',
'Vasyl;Stokolosa;',
].join('\n');
const CSVInvalidFileNotEnoughFields = [
'Country;First Name;Last Name;',
'"";Vasyl',
'"";Vasyl;Stokolosa',
].join('\n');
test('module should be a function', t => {
t.is(typeof CSVFileValidator, 'function');
});
test('should return the message "config headers are required"', async t => {
const csvData = await CSVFileValidator('');
t.is(typeof csvData, 'object');
t.deepEqual(csvData.inValidData, [{ message: 'config headers are required' }]);
t.deepEqual(csvData.data, []);
});
test('should return no data if the file is empty', async t => {
const csvData = await CSVFileValidator('', CSVConfig);
t.is(typeof csvData, 'object');
t.deepEqual(csvData.inValidData, []);
t.deepEqual(csvData.data, []);
});
test('should return invalid messages with data', async t => {
const csvData = await CSVFileValidator(CSVInvalidFile, CSVConfig);
t.is(csvData.inValidData.length, 5);
t.is(csvData.data.length, 2);
t.is(csvData.inValidData[3].message,
'<div class="red">Country is not valid. Country should be set to Ukraine. <strong>3 row</strong> / <strong>6 column</strong></div>'
);
});
test('should return data, the file is valid', async t => {
const csvData = await CSVFileValidator(CSVValidFile, CSVConfig);
t.is(csvData.inValidData.length, 2);
t.is(csvData.data.length, 2);
});
test('file without headers, the file is valid and headers are optional', async t => {
const csvData = await CSVFileValidator(CSVValidFileWithoutHeaders, { ...CSVConfig, isHeaderNameOptional: true });
t.is(csvData.inValidData.length, 1);
t.is(csvData.data.length, 2);
});
test('file with headers, the file is valid and headers are optional', async t => {
const csvData = await CSVFileValidator(CSVValidFile, { ...CSVConfig, isHeaderNameOptional: true });
t.is(csvData.inValidData.length, 2);
t.is(csvData.data.length, 2);
});
test('file is valid and headers are missed', async t => {
const csvData = await CSVFileValidator(CSVValidFileWithoutHeaders, CSVConfig);
t.is(csvData.inValidData.length, 12);
t.is(csvData.data.length, 1);
});
test('should return alphabetic columns', async t => {
const csvData = await CSVFileValidator(CSVInvalidFile, CSVConfig_1);
t.is(csvData.inValidData.length, 6);
t.is(csvData.data.length, 2);
t.is(csvData.inValidData[0].message,
'<div class="red">Password is not valid in the <strong>2 row</strong> / <strong>D column</strong></div>'
);
});
test('should return optional column', async t => {
const csvData = await CSVFileValidator(CSVValidFile, CSVConfig);
t.is(csvData.data[1].country, 'Ukraine');
});
test('file is valid and Email is not unique at the ... row', async t => {
const csvData = await CSVFileValidator(CSVInvalidFileWithDuplicates, CSVConfig);
t.is(csvData.inValidData.length, 5);
t.is(csvData.data.length, 3);
});
test('fields are mismatch: too many fields', async t => {
const csvData = await CSVFileValidator(CSVInvalidFileTooManyFields, { headers: [CSVConfig.headers[0]] });
t.is(csvData.inValidData.length, 1);
t.is(csvData.inValidData[0].message, 'Number of fields mismatch: expected 1 fields but parsed 3. In the row 1')
t.is(csvData.inValidData[0].rowIndex, 1)
t.is(csvData.data.length, 1);
});
test('fields are mismatch: not enough fields', async t => {
const csvData = await CSVFileValidator(CSVInvalidFileNotEnoughFields, { headers: [CSVConfig.headers[5], CSVConfig.headers[0], CSVConfig.headers[1]] });
t.is(csvData.inValidData.length, 3);
t.is(csvData.inValidData[0].message, 'Number of fields mismatch: expected 3 fields but parsed 2. In the row 1');
t.is(csvData.inValidData[0].rowIndex, 1)
t.is(csvData.data.length, 2);
});