Skip to content

Commit 06e0c4b

Browse files
committed
test: add tests for wildcardMatch keys option
For #247
1 parent 3a094da commit 06e0c4b

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

test/config/testCsvFilesList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const csvFileConfig = [
5151
{key: 'nestedNotUnwoundObjects', file: '../data/csv/nestedNotUnwoundObjects.csv'},
5252
{key: 'newlineWithWrapDelimiters', file: '../data/csv/newlineWithWrapDelimiters.csv'},
5353
{key: 'excludeKeyPattern', file: '../data/csv/excludeKeyPattern.csv'},
54+
{key: 'wildcardMatch', file: '../data/csv/wildcardMatch.csv'},
5455
];
5556

5657
function readCsvFile(filePath: string) {

test/config/testJsonFilesList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ export default {
4444
falsyValues: require('../data/json/falsyValues.json'),
4545
newlineWithWrapDelimiters: require('../data/json/newlineWithWrapDelimiters'),
4646
excludeKeyPattern: require('../data/json/excludeKeyPattern'),
47+
wildcardMatch: require('../data/json/wildcardMatch.json'),
4748
};

test/data/csv/wildcardMatch.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo,bar,baz.a,baz.array
2+
foo,bar,a,c

test/data/json/wildcardMatch.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"foo": "foo",
4+
"bar": "bar",
5+
"baz": {
6+
"a": "a",
7+
"b": "b",
8+
"array": "c"
9+
}
10+
}
11+
]

test/json2csv.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,44 @@ export function runTests() {
540540
assert.equal(csv, csvTestData.nestedDotKeys.replace(/\\\./g, '.'));
541541
});
542542

543+
// Test case for #247
544+
it('should not escape nested dots in keys with nested dots in them if turned off via the option', () => {
545+
const csv = json2csv(jsonTestData.wildcardMatch, {
546+
keys: ['foo', 'bar', 'baz.a', 'baz.array'],
547+
});
548+
assert.equal(csv, csvTestData.wildcardMatch);
549+
});
550+
551+
// Test case for #247
552+
it('should not escape nested dots in keys with nested dots in them if turned off via the option', () => {
553+
const csv = json2csv(jsonTestData.wildcardMatch, {
554+
keys: ['foo', 'bar', { field: 'baz.a', wildcardMatch: true }],
555+
});
556+
assert.equal(csv, csvTestData.wildcardMatch);
557+
});
558+
559+
// Test case for #247
560+
it('should not escape nested dots in keys with nested dots in them if turned off via the option', () => {
561+
const updatedCsv = csvTestData.wildcardMatch.replace('baz.a,baz.array', 'baz.a,baz.b,baz.array')
562+
.replace('a,c', 'a,b,c');
563+
564+
const csv = json2csv(jsonTestData.wildcardMatch, {
565+
keys: ['foo', 'bar', { field: 'baz', wildcardMatch: true }],
566+
});
567+
assert.equal(csv, updatedCsv);
568+
});
569+
570+
// Test case for #247
571+
it('should not escape nested dots in keys with nested dots in them if turned off via the option', () => {
572+
const updatedCsv = csvTestData.wildcardMatch.replace('foo,bar,baz.a,baz.array', 'foo,baz.a,baz.array,bar')
573+
.replace('foo,bar,a,c', 'foo,a,c,bar');
574+
575+
const csv = json2csv(jsonTestData.wildcardMatch, {
576+
keys: ['foo', { field: 'baz.a', wildcardMatch: true }, 'bar'],
577+
});
578+
assert.equal(csv, updatedCsv);
579+
});
580+
543581
it('should use a custom value parser function when provided', () => {
544582
const updatedCsv = csvTestData.trimmedFields.split('\n');
545583
const textRow = 'Parsed Value,Parsed Value,Parsed Value,Parsed Value,Parsed Value';

0 commit comments

Comments
 (0)