2023-06-21 11:56:59 +03:00
|
|
|
const assert = require('assert/strict');
|
2022-10-13 06:37:54 +03:00
|
|
|
const {unparse} = require('../index');
|
2020-07-08 10:40:48 +03:00
|
|
|
|
|
|
|
describe('unparse', function () {
|
2022-10-21 11:18:12 +03:00
|
|
|
it('serializes json to CSV and adds standard members fields with no explicit columns parameter', async function () {
|
2020-07-08 11:08:00 +03:00
|
|
|
const json = [{
|
|
|
|
email: 'email@example.com',
|
|
|
|
name: 'Sam Memberino',
|
|
|
|
note: 'Early supporter'
|
|
|
|
}];
|
2020-07-08 10:40:48 +03:00
|
|
|
|
|
|
|
const result = unparse(json);
|
|
|
|
|
2022-10-21 11:24:39 +03:00
|
|
|
assert.ok(result);
|
2020-07-08 10:40:48 +03:00
|
|
|
|
2024-02-06 21:19:16 +03:00
|
|
|
const expected = `id,email,name,note,subscribed_to_emails,complimentary_plan,stripe_customer_id,created_at,deleted_at,labels,tiers\r\n,email@example.com,Sam Memberino,Early supporter,,,,,,,`;
|
2022-10-21 11:24:39 +03:00
|
|
|
assert.equal(result, expected);
|
2020-07-08 10:40:48 +03:00
|
|
|
});
|
2022-01-11 17:26:11 +03:00
|
|
|
|
|
|
|
it('maps the subscribed property to subscribed_to_emails', function () {
|
|
|
|
const json = [{
|
|
|
|
email: 'do-not-email-me@email.com',
|
2023-02-28 00:25:20 +03:00
|
|
|
subscribed_to_emails: false
|
2022-01-11 17:26:11 +03:00
|
|
|
}];
|
|
|
|
|
2022-10-21 11:18:12 +03:00
|
|
|
const columns = [
|
|
|
|
'email', 'subscribed'
|
|
|
|
];
|
2022-01-11 17:26:11 +03:00
|
|
|
|
|
|
|
const result = unparse(json, columns);
|
|
|
|
|
|
|
|
const expected = `email,subscribed_to_emails\r\ndo-not-email-me@email.com,false`;
|
|
|
|
|
2022-10-21 11:24:39 +03:00
|
|
|
assert.equal(result, expected);
|
2022-01-11 17:26:11 +03:00
|
|
|
});
|
2022-10-21 11:46:09 +03:00
|
|
|
|
|
|
|
it('adds an error column to serialized CSV when present in columns and as a property', function () {
|
|
|
|
const json = [{
|
|
|
|
email: 'member-email@email.com',
|
|
|
|
error: 'things went south here!'
|
|
|
|
}];
|
|
|
|
const columns = [
|
|
|
|
'email', 'error'
|
|
|
|
];
|
|
|
|
|
|
|
|
const result = unparse(json, columns);
|
|
|
|
const expected = `email,error\r\nmember-email@email.com,things went south here!`;
|
|
|
|
assert.equal(result, expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('adds an error column automatically even if not present in columns', function () {
|
|
|
|
const json = [{
|
|
|
|
email: 'member-email@email.com',
|
|
|
|
error: 'things went south here!'
|
|
|
|
}];
|
|
|
|
const columns = [
|
|
|
|
'email'
|
|
|
|
];
|
|
|
|
|
|
|
|
const result = unparse(json, columns);
|
|
|
|
const expected = `email,error\r\nmember-email@email.com,things went south here!`;
|
|
|
|
assert.equal(result, expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles labels as strings and as objects', function () {
|
|
|
|
const json = [{
|
|
|
|
email: 'member-email@email.com',
|
|
|
|
labels: 'member-email-label'
|
|
|
|
}, {
|
|
|
|
email: 'second-member-email@email.com',
|
|
|
|
labels: [{
|
|
|
|
name: 'second member label'
|
|
|
|
}]
|
|
|
|
}, {
|
|
|
|
email: 'third-member-email@email.com',
|
|
|
|
labels: ['banana, avocado']
|
|
|
|
}];
|
|
|
|
const columns = [
|
|
|
|
'email', 'labels'
|
|
|
|
];
|
|
|
|
|
|
|
|
const result = unparse(json, columns);
|
|
|
|
const expected = `email,labels\r
|
|
|
|
member-email@email.com,member-email-label\r
|
|
|
|
second-member-email@email.com,second member label\r
|
|
|
|
third-member-email@email.com,"banana, avocado"`;
|
|
|
|
assert.equal(result, expected);
|
|
|
|
});
|
|
|
|
|
2022-10-24 13:03:44 +03:00
|
|
|
it('handles the tiers property serialization', function () {
|
2022-10-21 11:46:09 +03:00
|
|
|
const json = [{
|
|
|
|
email: 'member-email@email.com',
|
|
|
|
tiers: [{
|
|
|
|
name: 'Bronze Level'
|
|
|
|
}]
|
|
|
|
}];
|
|
|
|
|
|
|
|
const columns = [
|
2022-10-21 12:45:02 +03:00
|
|
|
'email', 'tiers'
|
2022-10-21 11:46:09 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
const result = unparse(json, columns);
|
2022-10-21 12:45:02 +03:00
|
|
|
const expected = `email,tiers\r\nmember-email@email.com,Bronze Level`;
|
2022-10-21 11:46:09 +03:00
|
|
|
assert.equal(result, expected);
|
|
|
|
});
|
2024-04-02 10:44:06 +03:00
|
|
|
|
|
|
|
it('escapes fields starting with CSV injection characters', async function () {
|
|
|
|
const json = [{
|
|
|
|
email: 'email@example.com',
|
|
|
|
name: '=1+2',
|
|
|
|
note: 'Early supporter'
|
|
|
|
}];
|
|
|
|
|
|
|
|
const result = unparse(json);
|
|
|
|
assert.ok(result);
|
|
|
|
|
|
|
|
const expected = `id,email,name,note,subscribed_to_emails,complimentary_plan,stripe_customer_id,created_at,deleted_at,labels,tiers\r\n,email@example.com,"'=1+2",Early supporter,,,,,,,`;
|
|
|
|
assert.equal(result, expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('escapes fields with CSV injection characters and quotes', async function () {
|
|
|
|
const json = [{
|
|
|
|
email: 'email@example.com',
|
|
|
|
name: `=1+2'" `,
|
|
|
|
note: 'Early supporter'
|
|
|
|
}];
|
|
|
|
|
|
|
|
const result = unparse(json);
|
|
|
|
assert.ok(result);
|
|
|
|
|
|
|
|
const expected = `id,email,name,note,subscribed_to_emails,complimentary_plan,stripe_customer_id,created_at,deleted_at,labels,tiers\r\n,email@example.com,"'=1+2'"" ",Early supporter,,,,,,,`;
|
|
|
|
assert.equal(result, expected);
|
|
|
|
});
|
2020-07-08 10:40:48 +03:00
|
|
|
});
|