-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.js
124 lines (109 loc) · 3.84 KB
/
index.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
const { expect } = require('chai');
const index = require('./index');
const { decode } = index;
describe('Address encoder', () => {
describe('[POSITIVE] Should decode properly', () => {
let results;
let error;
const requiredFields = ['country', 'city', 'zipCode', 'streetName', 'streetNumber'];
const positive = [
{
// Normal
text: 'country:Finland|state:Uusimaa|city:Helsinki|zipCode:00100|streetName:Ludviginkatu|streetNumber:6',
fields: ['country', 'state', 'city', 'zipCode', 'streetName', 'streetNumber'],
},
{
// Interchangeable fields
text: 'city:Helsinki|state:Uusimaa|country:Finland|zipCode:00100|streetName:Ludviginkatu|streetNumber:6',
fields: ['country', 'state', 'city', 'zipCode', 'streetName', 'streetNumber'],
},
{
// Spaces in-between
text: 'country:New Zealand|state:Bay of Plenty|city:White Pine Bush|zipCode:3191|streetName:White Pine Bush Road|streetNumber:479',
fields: ['country', 'state', 'city', 'zipCode', 'streetName', 'streetNumber'],
},
{
// Other interesting special characters
text: "country:Aäöم武кв.-`''´`|state:Aäöم武кв.-`''´`|city:Aäöم武кв.-`''´`|zipCode:3191|streetName:Aäöم武кв.-`''´`|streetNumber:479",
fields: ['country', 'state', 'city', 'zipCode', 'streetName', 'streetNumber'],
},
];
try {
results = positive.map((testCase) => {
return {
decoded: decode(testCase.text, requiredFields),
fields: testCase.fields,
};
});
} catch (e) {
error = e;
}
it('Should not return any error for all cases', () => {
expect(error).to.be.undefined;
});
it('Should return all fields in the string', () => {
results.forEach((result) => {
expect(result.decoded).to.have.all.keys(result.fields);
});
});
});
describe('[NEGATIVE] Should fail to decode duplicate', () => {
let error;
const requiredFields = ['city'];
const negative = [
{
// duplicate
text: 'country:Finland|state:Uusimaa|city:Helsinki|city:Helsinki|zipCode:00100|streetName:Ludviginkatu|streetNumber:6',
fields: ['country', 'state', 'city', 'zipCode', 'streetName', 'streetNumber'],
},
];
try {
negative.map((testCase) => {
return {
decoded: decode(testCase.text, requiredFields),
fields: testCase.fields,
};
});
} catch (e) {
error = e;
}
it('Should throw error', () => {
expect(error.message).to.be.equal('Detect duplication of field city in fromAddress or toAddress');
});
});
describe('[NEGATIVE] Should fail to decode invalid required field', () => {
let error;
const requiredFields = ['invalid'];
const negative = [
{
// ok
text: 'country:Finland|state:Uusimaa|city:Helsinki|zipCode:00100|streetName:Ludviginkatu|streetNumber:6',
fields: ['country', 'state', 'city', 'zipCode', 'streetName', 'streetNumber'],
},
];
try {
negative.map((testCase) => {
return {
decoded: decode(testCase.text, requiredFields),
fields: testCase.fields,
};
});
} catch (e) {
error = e;
}
it('Should throw error', () => {
expect(error.message).to.be.equal('Missing invalid from input text');
});
});
describe('[NEGATIVE] Should fail to decode invalid required field', () => {
it('Should throw missing address input', () => {
expect(() => decode(undefined)).to.throw('Missing "address" input');
});
it('Should throw invalid value error', () => {
expect(() => decode('invalid')).to.throw('Invalid value undefined');
});
it('Should throw invalid key error', () => {
expect(() => decode('|')).to.throw('Invalid key');
});
});
});