This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
forked from shystruk/csv-file-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
71 lines (55 loc) · 2.64 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
import test from 'ava';
import CSVFileValidator from './src/csv-file-validator';
const CSVInvalidFile = `
Vasyl;Stokolosa;[email protected];123;admin,manager\n
Vasyl_2;"";[email protected];123123123;user
`;
const CSVValidFile = `
Vasyl;Stokolosa;[email protected];123123;admin,manager\n
Vasyl;Stokolosa;[email protected];123123123;user;Ukraine
`;
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 isEmailValid = (email) => {
const reqExp = /[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$/
return reqExp.test(email)
}
const isPasswordValid = (password) => (password.length >= 4)
const uniqueError = (headerName) => (`<div class="red">${headerName} is not unique</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 }
]
}
test('module should be a function', t => {
t.is(typeof CSVFileValidator, 'function');
});
test('should return an object with empty inValidMessages/data keys', async t => {
const csvData = await CSVFileValidator('', {});
t.is(typeof csvData, 'object');
t.deepEqual(csvData.inValidMessages, []);
t.deepEqual(csvData.data, []);
});
test('should validate .csv file and return invalid messages with data', async t => {
const csvData = await CSVFileValidator(CSVInvalidFile, CSVConfig);
t.is(csvData.inValidMessages.length, 3);
t.is(csvData.data.length, 2);
});
test('should validate .csv file and return data, file is valid', async t => {
const csvData = await CSVFileValidator(CSVValidFile, CSVConfig);
t.is(csvData.inValidMessages.length, 0);
t.is(csvData.data.length, 2);
});
test('should return optional column', async t => {
const csvData = await CSVFileValidator(CSVValidFile, CSVConfig);
t.is(csvData.data[1].country, 'Ukraine');
});