@@ -5,17 +5,22 @@ const {parse} = require('../index');
5
5
const csvPath = path . join ( __dirname , '/fixtures/' ) ;
6
6
7
7
describe ( 'parse' , function ( ) {
8
+ const DEFAULT_HEADER_MAPPING = {
9
+ email : 'email' ,
10
+ name : 'name'
11
+ } ;
12
+
8
13
it ( 'empty file' , async function ( ) {
9
14
const filePath = csvPath + 'empty.csv' ;
10
- const result = await parse ( filePath ) ;
15
+ const result = await parse ( filePath , DEFAULT_HEADER_MAPPING ) ;
11
16
12
17
should . exist ( result ) ;
13
18
result . length . should . eql ( 0 ) ;
14
19
} ) ;
15
20
16
21
it ( 'one column' , async function ( ) {
17
22
const filePath = csvPath + 'single-column-with-header.csv' ;
18
- const result = await parse ( filePath ) ;
23
+ const result = await parse ( filePath , DEFAULT_HEADER_MAPPING ) ;
19
24
20
25
should . exist ( result ) ;
21
26
result . length . should . eql ( 2 ) ;
@@ -33,7 +38,7 @@ describe('parse', function () {
33
38
34
39
it ( 'two columns, 1 filter' , async function ( ) {
35
40
const filePath = csvPath + 'two-columns-with-header.csv' ;
36
- const result = await parse ( filePath ) ;
41
+ const result = await parse ( filePath , DEFAULT_HEADER_MAPPING ) ;
37
42
38
43
should . exist ( result ) ;
39
44
result . length . should . eql ( 2 ) ;
@@ -44,6 +49,7 @@ describe('parse', function () {
44
49
it ( 'two columns, 2 filters' , async function ( ) {
45
50
const filePath = csvPath + 'two-columns-obscure-header.csv' ;
46
51
const mapping = {
52
+ id : 'id' ,
47
53
'Email Address' : 'email'
48
54
} ;
49
55
const result = await parse ( filePath , mapping ) ;
@@ -59,6 +65,7 @@ describe('parse', function () {
59
65
it ( 'two columns with mapping' , async function ( ) {
60
66
const filePath = csvPath + 'two-columns-mapping-header.csv' ;
61
67
const mapping = {
68
+ id : 'id' ,
62
69
correo_electronico : 'email' ,
63
70
nombre : 'name'
64
71
} ;
@@ -78,40 +85,23 @@ describe('parse', function () {
78
85
it ( 'two columns with partial mapping' , async function ( ) {
79
86
const filePath = csvPath + 'two-columns-mapping-header.csv' ;
80
87
const mapping = {
88
+ id : 'id' ,
81
89
correo_electronico : 'email'
82
90
} ;
83
91
const result = await parse ( filePath , mapping ) ;
84
92
85
93
should . exist ( result ) ;
86
94
result . length . should . eql ( 2 ) ;
87
95
result [ 0 ] . email . should . eql ( '[email protected] ' ) ;
88
- result [ 0 ] . nombre . should . eql ( 'joe' ) ;
89
96
result [ 0 ] . id . should . eql ( '1' ) ;
90
97
91
98
result [ 1 ] . email . should . eql ( '[email protected] ' ) ;
92
- result [ 1 ] . nombre . should . eql ( 'test' ) ;
93
- result [ 1 ] . id . should . eql ( '2' ) ;
94
- } ) ;
95
-
96
- it ( 'two columns with empty mapping' , async function ( ) {
97
- const filePath = csvPath + 'two-columns-mapping-header.csv' ;
98
- const mapping = { } ;
99
- const result = await parse ( filePath , mapping ) ;
100
-
101
- should . exist ( result ) ;
102
- result . length . should . eql ( 2 ) ;
103
- result [ 0 ] . correo_electronico . should . eql ( '[email protected] ' ) ;
104
- result [ 0 ] . nombre . should . eql ( 'joe' ) ;
105
- result [ 0 ] . id . should . eql ( '1' ) ;
106
-
107
- result [ 1 ] . correo_electronico . should . eql ( '[email protected] ' ) ;
108
- result [ 1 ] . nombre . should . eql ( 'test' ) ;
109
99
result [ 1 ] . id . should . eql ( '2' ) ;
110
100
} ) ;
111
101
112
102
it ( 'transforms empty values to nulls' , async function ( ) {
113
103
const filePath = csvPath + 'multiple-records-with-empty-values.csv' ;
114
- const result = await parse ( filePath ) ;
104
+ const result = await parse ( filePath , DEFAULT_HEADER_MAPPING ) ;
115
105
116
106
should . exist ( result ) ;
117
107
result . length . should . eql ( 2 ) ;
@@ -125,6 +115,7 @@ describe('parse', function () {
125
115
it ( ' transforms "subscribed_to_emails" column to "subscribed" property when the mapping is passed in' , async function ( ) {
126
116
const filePath = csvPath + 'subscribed-to-emails-header.csv' ;
127
117
const mapping = {
118
+ email : 'email' ,
128
119
subscribed_to_emails : 'subscribed'
129
120
} ;
130
121
const result = await parse ( filePath , mapping ) ;
@@ -140,14 +131,14 @@ describe('parse', function () {
140
131
141
132
it ( 'DOES NOT transforms "subscribed_to_emails" column to "subscribed" property when the WITHOUT mapping' , async function ( ) {
142
133
const filePath = csvPath + 'subscribed-to-emails-header.csv' ;
143
- const result = await parse ( filePath ) ;
134
+ const result = await parse ( filePath , DEFAULT_HEADER_MAPPING ) ;
144
135
145
136
assert . ok ( result ) ;
146
137
assert . equal ( result . length , 2 ) ;
147
138
assert . equal ( result [ 0 ] . email , '[email protected] ' ) ;
148
- assert . ok ( result [ 0 ] . subscribed_to_emails ) ;
139
+ assert . equal ( result [ 0 ] . subscribed_to_emails , undefined , 'property not present in the mapping should not be defined' ) ;
149
140
150
141
assert . equal ( result [ 1 ] . email , '[email protected] ' ) ;
151
- assert . equal ( result [ 1 ] . subscribed_to_emails , false ) ;
142
+ assert . equal ( result [ 1 ] . subscribed_to_emails , undefined , 'property not present in the mapping should not be defined' ) ;
152
143
} ) ;
153
144
} ) ;
0 commit comments