1 | import {assertFile, testFilePath} from './helper';
|
2 | import {CsvWriter} from '../lib/csv-writer';
|
3 | import {writeFileSync} from 'fs';
|
4 | import {createArrayCsvWriter} from '../index';
|
5 |
|
6 | describe('Write array records into CSV', () => {
|
7 |
|
8 | const makeFilePath = (id: string) => testFilePath(`array-${id}`);
|
9 | const records = [
|
10 | ['Bob', 'French'],
|
11 | ['Mary', 'English']
|
12 | ];
|
13 |
|
14 | describe('When only path is specified', () => {
|
15 | const filePath = makeFilePath('minimum');
|
16 | let writer: CsvWriter<string[]>;
|
17 |
|
18 | beforeEach(() => {
|
19 | writer = createArrayCsvWriter({path: filePath});
|
20 | });
|
21 |
|
22 | it('writes records to a new file', async () => {
|
23 | await writer.writeRecords(records);
|
24 | assertFile(filePath, 'Bob,French\nMary,English\n');
|
25 | });
|
26 |
|
27 | it('appends records when requested to write to the same file', async () => {
|
28 | await writer.writeRecords([records[0]]);
|
29 | await writer.writeRecords([records[1]]);
|
30 | assertFile(filePath, 'Bob,French\nMary,English\n');
|
31 | });
|
32 | });
|
33 |
|
34 | describe('When field header is given', () => {
|
35 | const filePath = makeFilePath('header');
|
36 | let writer: CsvWriter<string[]>;
|
37 |
|
38 | beforeEach(() => {
|
39 | writer = createArrayCsvWriter({
|
40 | path: filePath,
|
41 | header: ['NAME', 'LANGUAGE']
|
42 | });
|
43 | });
|
44 |
|
45 | it('writes a header', async () => {
|
46 | await writer.writeRecords(records);
|
47 | assertFile(filePath, 'NAME,LANGUAGE\nBob,French\nMary,English\n');
|
48 | });
|
49 |
|
50 | it('appends records without headers', async () => {
|
51 | await writer.writeRecords([records[0]]);
|
52 | await writer.writeRecords([records[1]]);
|
53 | assertFile(filePath, 'NAME,LANGUAGE\nBob,French\nMary,English\n');
|
54 | });
|
55 | });
|
56 |
|
57 | describe('When `append` flag is specified', () => {
|
58 | const filePath = makeFilePath('append');
|
59 | writeFileSync(filePath, 'Mike,German\n', 'utf8');
|
60 | const writer = createArrayCsvWriter({
|
61 | path: filePath,
|
62 | append: true
|
63 | });
|
64 |
|
65 | it('do not overwrite the existing contents and appends records to them', async () => {
|
66 | await writer.writeRecords([records[1]]);
|
67 | assertFile(filePath, 'Mike,German\nMary,English\n');
|
68 | });
|
69 | });
|
70 |
|
71 | describe('When encoding is specified', () => {
|
72 | const filePath = makeFilePath('encoding');
|
73 | const writer = createArrayCsvWriter({
|
74 | path: filePath,
|
75 | encoding: 'utf16le'
|
76 | });
|
77 |
|
78 | it('writes to a file with the specified encoding', async () => {
|
79 | await writer.writeRecords(records);
|
80 | assertFile(filePath, 'Bob,French\nMary,English\n', 'utf16le');
|
81 | });
|
82 | });
|
83 |
|
84 | describe('When semicolon is specified as a field delimiter', () => {
|
85 | const filePath = makeFilePath('field-delimiter');
|
86 | const writer = createArrayCsvWriter({
|
87 | path: filePath,
|
88 | header: ['NAME', 'LANGUAGE'],
|
89 | fieldDelimiter: ';'
|
90 | });
|
91 |
|
92 | it('uses semicolon instead of comma to separate fields', async () => {
|
93 | await writer.writeRecords(records);
|
94 | assertFile(filePath, 'NAME;LANGUAGE\nBob;French\nMary;English\n');
|
95 | });
|
96 | });
|
97 |
|
98 | describe('When newline is specified', () => {
|
99 | const filePath = makeFilePath('newline');
|
100 | const writer = createArrayCsvWriter({
|
101 | path: filePath,
|
102 | recordDelimiter: '\r\n'
|
103 | });
|
104 |
|
105 | it('writes to a file with the specified newline character', async () => {
|
106 | await writer.writeRecords(records);
|
107 | assertFile(filePath, 'Bob,French\r\nMary,English\r\n');
|
108 | });
|
109 | });
|
110 |
|
111 | describe('When `alwaysQuote` flag is set', () => {
|
112 | const filePath = makeFilePath('always-quote');
|
113 | const writer = createArrayCsvWriter({
|
114 | path: filePath,
|
115 | header: ['NAME', 'LANGUAGE'],
|
116 | alwaysQuote: true
|
117 | });
|
118 |
|
119 | it('quotes all fields', async () => {
|
120 | await writer.writeRecords(records);
|
121 | assertFile(filePath, '"NAME","LANGUAGE"\n"Bob","French"\n"Mary","English"\n');
|
122 | });
|
123 | });
|
124 | });
|