-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertHelper.test.js
More file actions
203 lines (192 loc) · 6.35 KB
/
convertHelper.test.js
File metadata and controls
203 lines (192 loc) · 6.35 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const { Converter } = require("./convertHelper");
let converter = {}
let bind__functionTest = null
let bindMap1 = {}
let bindMap2 = {}
let bindMap3 = {}
let bindMap4 = {}
let bindMap2SkipWrong = {}
let sourceHeader = {}
let sourceObject1 = {}
let sourceObject2 = {}
let sourceObject3 = {}
let sourceObject4 = {}
let sourceFooter = {}
let objectList = []
let objectListWithHeaderAndFooter = []
let convertedObject = {}
let convertedObjectWithHeaderAndFooter = {}
beforeEach(() => {
converter = new Converter()
bind__functionTest = (currentObject) => {
return 'functionValue'
}
bindMap1 = {
bindings: [
{ destination: 'rec', source: 'numrec', type: 'copy' },
{ destination: 'act', source: 'codact', type: 'copy' },
{ destination: 'type', type: 'fixed', value: 'warehouse' },
{ destination: 'hhh', type: 'header', source: 'headerValue' },
{ destination: 'fff', type: 'footer', source: 'footerValue' },
{ destination: 'func', type: 'function', value: bind__functionTest }
]
}
bindMap2 = {
processObjectOnlyIf: new Map([
['numrec', ['80']]
]),
skipObjectIf: new Map([
['numrec', ['90']]
]),
bindings: [
{ destination: 'rec', source: 'numrec', type: 'copy' },
{ destination: 'act', source: 'codact', type: 'copy' },
{ destination: 'type', type: 'fixed', value: '80' }
]
}
bindMap3 = {
processObjectOnlyIf: new Map([
['numrec', ['90']]
]),
bindings: [
{ destination: 'rec', source: 'numrec', type: 'copy' },
{ destination: 'act', source: 'codact', type: 'copy' },
{ destination: 'type', type: 'fixed', value: '90' },
{ destination: 'hhh', type: 'header', source: 'headerValue' },
{ destination: 'fff', type: 'footer', source: 'footerValue' },
{ destination: 'func', type: 'function', value: bind__functionTest }
]
}
bindMap4 = {
bindings: [
{ destination: 'rec', source: 'numrec', type: 'copy' },
{ destination: 'err', type: 'error' }
]
}
bindMap2SkipWrong = {
processObjectOnlyIf: new Map([
['numrec', ['80']]
]),
skipObjectIf: new Map([
['WRONG-ATTRIBUTE', ['90']]
]),
bindings: [
{ destination: 'rec', source: 'numrec', type: 'copy' },
{ destination: 'act', source: 'codact', type: 'copy' },
{ destination: 'type', type: 'fixed', value: '80' }
]
}
sourceHeader = {
headerValue: '0'
}
sourceObject1 = {
numrec: '80',
codact: 'A'
}
sourceObject2 = {
numrec: '90',
codact: 'A'
}
sourceObject3 = {
numrec: 'NON-MATCHING-VALUE',
codact: 'A'
}
sourceObject4 = {
codact: 'A'
}
sourceFooter = {
footerValue: '99'
}
objectList = [sourceObject1]
objectListWithHeaderAndFooter = [sourceHeader, sourceObject2, sourceFooter]
convertedObject = {
rec: '80',
act: 'A',
type: '80'
}
convertedObjectWithHeaderAndFooter = {
rec: '90',
act: 'A',
type: '90',
hhh: '0',
fff: '99',
func: 'functionValue'
}
})
afterEach(() => {
jest.restoreAllMocks()
})
describe('ConvertHelper.convert()', () => {
test('ConvertHelper.convert() - converting without header and footer - opts not present', () => {
expect(converter.convert(objectList, [bindMap2, bindMap3]))
.toEqual([convertedObject])
})
test('ConvertHelper.convert() - converting without header and footer - opts without attributes', () => {
expect(converter.convert(objectList, [bindMap2, bindMap3], {}))
.toEqual([convertedObject])
})
test('ConvertHelper.convert() - converting without header and footer - opts with attributes null', () => {
expect(converter.convert(objectList, [bindMap2, bindMap3], { header: null, footer: null }))
.toEqual([convertedObject])
})
test('ConvertHelper.convert() - converting WITH header and footer', () => {
expect(converter.convert(objectListWithHeaderAndFooter, [bindMap2, bindMap3], { header: 0, footer: -1 }))
.toEqual([convertedObjectWithHeaderAndFooter])
})
test('ConvertHelper.convert() - without bindMap.length==0 must throw', () => {
expect(() => { converter.convert(objectListWithHeaderAndFooter, []) }).toThrow()
})
test('ConvertHelper.convert() - without bindMap must throw', () => {
expect(() => { converter.convert(objectListWithHeaderAndFooter) }).toThrow()
})
test('ConvertHelper.convert() - using skipObjectIf to ignore a line', () => {
expect(converter.convert([sourceObject1, sourceObject2], [bindMap2]))
.toEqual([convertedObject])
})
test('ConvertHelper.convert() - using skipObjectIf with wrong source attribute must throw', () => {
expect(() => {
converter.convert([sourceObject1, sourceObject2], [bindMap2SkipWrong])
}).toThrow()
})
test('ConvertHelper.convert() - using bind type not implemented throws error', () => {
expect(() => { converter.convert([sourceObject1], [bindMap4]) }).toThrow()
})
})
describe('ConvertHelper.getProperBindMap()', () => {
test('ConvertHelper.getProperBindMap() - sending 1 bindMap must return the same bindMap', () => {
expect(converter.getProperBindMap([bindMap1], {}))
.toEqual(bindMap1)
})
test('ConvertHelper.getProperBindMap() - sending 2 bindMaps must return the right bindMap', () => {
expect(converter.getProperBindMap([bindMap2, bindMap3], sourceObject1))
.toEqual(bindMap2)
})
test('ConvertHelper.getProperBindMap() - sending 2 bindMaps must return the right bindMap', () => {
expect(() => {
const test = converter.getProperBindMap([bindMap2, bindMap3], sourceObject2)
console.log(test);
return test
})
.toEqual(bindMap3)
})
test('ConvertHelper.getProperBindMap() - sending 2 bindMaps without "processObjectOnlyIf" attribute must throw', () => {
expect(() => {
converter.getProperBindMap([bindMap1, bindMap1], sourceObject2)
}).toThrow()
})
test('ConvertHelper.getProperBindMap() - sending 2 bindMaps with non matching "processObjectOnlyIf" attribute must throw', () => {
expect(() => {
converter.getProperBindMap([bindMap2, bindMap3], sourceObject3)
}).toThrow()
})
test('ConvertHelper.getProperBindMap() - sending 2 bindMaps with object without matching attribute must throw', () => {
expect(() => {
try {
converter.getProperBindMap([bindMap2, bindMap3], sourceObject4)
} catch (err) {
console.log(err);
throw err
}
}).toThrow()
})
})