forked from anton-bot/objects-to-csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
82 lines (72 loc) · 3.77 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
const ObjectsToCsv = require('./index');
const fs = require('fs');
const SAMPLE_ASCII = [
{ code: 'HK', name: 'Hong Kong' },
{ code: 'KLN', name: 'Kowloon' },
{ code: 'NT', name: 'New Territories' },
];
const SAMPLE_UNICODE = [
{ lang: 'Russian', text: 'Привет, как дела?' },
{ lang: 'Chinese', text: '冇問題' },
{ lang: 'Danish', text: 'Characters like Æ, Ø and Å' },
];
describe('Object to CSV converter', () => {
it('must generate a CSV string - normal ASCII text', async () => {
const csv = new ObjectsToCsv(SAMPLE_ASCII);
const result = await csv.toString();
expect(result).toEqual('code,name\nHK,Hong Kong\nKLN,Kowloon\nNT,New Territories\n');
});
it('must generate a CSV string - no table header', async () => {
const csv = new ObjectsToCsv(SAMPLE_ASCII);
const result = await csv.toString(false);
expect(result).toEqual('HK,Hong Kong\nKLN,Kowloon\nNT,New Territories\n');
});
it('must generate a CSV string - special alphabets', async () => {
const csv = new ObjectsToCsv(SAMPLE_UNICODE);
const result = await csv.toString();
expect(result).toEqual('lang,text\nRussian,"Привет, как дела?"\nChinese,冇問題\nDanish,"Characters like Æ, Ø and Å"\n');
});
it('must generate a CSV file - normal ASCII text', async () => {
const csv = new ObjectsToCsv(SAMPLE_ASCII);
const testFilename = `deleteme-test-${Date.now()}-1.csv`;
await csv.toDisk(testFilename);
const result = fs.readFileSync(testFilename, 'utf8');
fs.unlinkSync(testFilename);
expect(result).toEqual('code,name\nHK,Hong Kong\nKLN,Kowloon\nNT,New Territories\n');
});
it('must generate a CSV file - special alphabets', async () => {
const csv = new ObjectsToCsv(SAMPLE_UNICODE);
const testFilename = `deleteme-test-${Date.now()}-2.csv`;
await csv.toDisk(testFilename);
const result = fs.readFileSync(testFilename, 'utf8');
fs.unlinkSync(testFilename);
expect(result).toEqual('lang,text\nRussian,"Привет, как дела?"\nChinese,冇問題\nDanish,"Characters like Æ, Ø and Å"\n');
});
it('must generate a CSV file - special alphabets + BOM', async () => {
const csv = new ObjectsToCsv(SAMPLE_UNICODE);
const testFilename = `deleteme-test-${Date.now()}-3.csv`;
await csv.toDisk(testFilename, { bom: true });
const result = fs.readFileSync(testFilename, 'utf8');
fs.unlinkSync(testFilename);
expect(result).toEqual('\ufeff' + 'lang,text\nRussian,"Привет, как дела?"\nChinese,冇問題\nDanish,"Characters like Æ, Ø and Å"\n');
});
it('must append to CSV file', async () => {
const csv = new ObjectsToCsv(SAMPLE_ASCII);
const testFilename = `deleteme-test-${Date.now()}-4.csv`;
await csv.toDisk(testFilename, { append: true });
await csv.toDisk(testFilename, { append: true });
await csv.toDisk(testFilename, { append: true });
const result = fs.readFileSync(testFilename, 'utf8');
fs.unlinkSync(testFilename);
expect(result).toEqual('code,name\nHK,Hong Kong\nKLN,Kowloon\nNT,New Territories\nHK,Hong Kong\nKLN,Kowloon\nNT,New Territories\nHK,Hong Kong\nKLN,Kowloon\nNT,New Territories\n');
});
it('must append to CSV file + BOM', async () => {
const csv = new ObjectsToCsv(SAMPLE_UNICODE);
const testFilename = `deleteme-test-${Date.now()}-5.csv`;
await csv.toDisk(testFilename, { append: true, bom: true });
await csv.toDisk(testFilename, { append: true, bom: true });
const result = fs.readFileSync(testFilename, 'utf8');
fs.unlinkSync(testFilename);
expect(result).toEqual('\ufeff' + 'lang,text\nRussian,"Привет, как дела?"\nChinese,冇問題\nDanish,"Characters like Æ, Ø and Å"\nRussian,"Привет, как дела?"\nChinese,冇問題\nDanish,"Characters like Æ, Ø and Å"\n');
});
});