Added test coverage for 'subscribed' transform

refs https://github.com/TryGhost/Toolbox/issues/430

- Not having any extra logic in the mapper will allow to have a generalized "mapping" concept for CSV input serialization
- This is groundwork for stricter header value filtering on the parsing stage
This commit is contained in:
Naz 2022-10-13 12:10:44 +08:00
parent bc70835890
commit 1c746c0ca0
No known key found for this signature in database
3 changed files with 25 additions and 5 deletions

View File

@ -4,18 +4,20 @@ const papaparse = require('papaparse');
const fs = require('fs-extra');
module.exports = (path, mapping, defaultLabels = []) => {
const inputMapping = Object.assign({}, mapping, {
subscribed_to_emails: 'subscribed'
});
return new Promise(function (resolve, reject) {
const csvFileStream = fs.createReadStream(path);
const csvParserStream = papaparse.parse(papaparse.NODE_STREAM_INPUT, {
header: true,
transformHeader(_header) {
let header = _header;
if (mapping && Reflect.has(mapping, _header)) {
header = mapping[_header];
}
if (header === 'subscribed_to_emails') {
return 'subscribed';
if (inputMapping && Reflect.has(inputMapping, _header)) {
header = inputMapping[_header];
}
return header;
},
transform(value, header) {

View File

@ -0,0 +1,3 @@
email,subscribed_to_emails
jbloggs@example.com,true
test@example.com,false
1 email subscribed_to_emails
2 jbloggs@example.com true
3 test@example.com false

View File

@ -1,5 +1,6 @@
const should = require('should');
const path = require('path');
const assert = require('assert');
const {parse} = require('../index');
const csvPath = path.join(__dirname, '/fixtures/');
@ -122,4 +123,18 @@ describe('parse', function () {
result[1].email.should.eql('test@example.com');
should.equal(result[1].name, null);
});
it('read csv: transforms "subscribed_to_emails" column to "subscribed" property', async function () {
const result = await readCSV({
filePath: csvPath + 'subscribed-to-emails-header.csv'
});
assert.ok(result);
assert.equal(result.length, 2);
assert.equal(result[0].email, 'jbloggs@example.com');
assert.ok(result[0].subscribed);
assert.equal(result[1].email, 'test@example.com');
assert.equal(result[1].subscribed, false);
});
});