Skip to content

Commit

Permalink
test: add tests for wildcardMatch keys option
Browse files Browse the repository at this point in the history
For #247
  • Loading branch information
mrodrig committed Feb 24, 2024
1 parent 3a094da commit 06e0c4b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/config/testCsvFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const csvFileConfig = [
{key: 'nestedNotUnwoundObjects', file: '../data/csv/nestedNotUnwoundObjects.csv'},
{key: 'newlineWithWrapDelimiters', file: '../data/csv/newlineWithWrapDelimiters.csv'},
{key: 'excludeKeyPattern', file: '../data/csv/excludeKeyPattern.csv'},
{key: 'wildcardMatch', file: '../data/csv/wildcardMatch.csv'},
];

function readCsvFile(filePath: string) {
Expand Down
1 change: 1 addition & 0 deletions test/config/testJsonFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ export default {
falsyValues: require('../data/json/falsyValues.json'),
newlineWithWrapDelimiters: require('../data/json/newlineWithWrapDelimiters'),
excludeKeyPattern: require('../data/json/excludeKeyPattern'),
wildcardMatch: require('../data/json/wildcardMatch.json'),
};
2 changes: 2 additions & 0 deletions test/data/csv/wildcardMatch.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo,bar,baz.a,baz.array
foo,bar,a,c
11 changes: 11 additions & 0 deletions test/data/json/wildcardMatch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"foo": "foo",
"bar": "bar",
"baz": {
"a": "a",
"b": "b",
"array": "c"
}
}
]
38 changes: 38 additions & 0 deletions test/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,44 @@ export function runTests() {
assert.equal(csv, csvTestData.nestedDotKeys.replace(/\\\./g, '.'));
});

// Test case for #247
it('should not escape nested dots in keys with nested dots in them if turned off via the option', () => {
const csv = json2csv(jsonTestData.wildcardMatch, {
keys: ['foo', 'bar', 'baz.a', 'baz.array'],
});
assert.equal(csv, csvTestData.wildcardMatch);
});

// Test case for #247
it('should not escape nested dots in keys with nested dots in them if turned off via the option', () => {
const csv = json2csv(jsonTestData.wildcardMatch, {
keys: ['foo', 'bar', { field: 'baz.a', wildcardMatch: true }],
});
assert.equal(csv, csvTestData.wildcardMatch);
});

// Test case for #247
it('should not escape nested dots in keys with nested dots in them if turned off via the option', () => {
const updatedCsv = csvTestData.wildcardMatch.replace('baz.a,baz.array', 'baz.a,baz.b,baz.array')
.replace('a,c', 'a,b,c');

const csv = json2csv(jsonTestData.wildcardMatch, {
keys: ['foo', 'bar', { field: 'baz', wildcardMatch: true }],
});
assert.equal(csv, updatedCsv);
});

// Test case for #247
it('should not escape nested dots in keys with nested dots in them if turned off via the option', () => {
const updatedCsv = csvTestData.wildcardMatch.replace('foo,bar,baz.a,baz.array', 'foo,baz.a,baz.array,bar')
.replace('foo,bar,a,c', 'foo,a,c,bar');

const csv = json2csv(jsonTestData.wildcardMatch, {
keys: ['foo', { field: 'baz.a', wildcardMatch: true }, 'bar'],
});
assert.equal(csv, updatedCsv);
});

it('should use a custom value parser function when provided', () => {
const updatedCsv = csvTestData.trimmedFields.split('\n');
const textRow = 'Parsed Value,Parsed Value,Parsed Value,Parsed Value,Parsed Value';
Expand Down

0 comments on commit 06e0c4b

Please sign in to comment.