forked from alexcaza/export-to-csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-to-csv.spec.ts
125 lines (105 loc) · 3.68 KB
/
export-to-csv.spec.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { ExportToCsv, Options } from './export-to-csv';
const mockData = [
{
name: "Test 1",
age: 13,
average: 8.2,
approved: true,
description: "Test 1 description"
},
{
name: 'Test 2',
age: 11,
average: 8.2,
approved: true,
description: "Test 2 description"
},
{
name: 'Test 4',
age: 10,
average: 8.2,
approved: true,
description: "Test 3 description"
},
];
describe('ExportToCsv', () => {
it('should create a comma seperated string', () => {
const options: Options = {
title: "Test Csv",
useBom: true,
useKeysAsHeaders: true,
}
const exportToCsvInstance = new ExportToCsv(options);
const string = exportToCsvInstance.generateCsv(mockData, true);
expect(string).toBeTruthy(typeof string === 'string');
});
it('should use keys of first object in collection as headers', () => {
const options: Options = {
title: "Test Csv",
useBom: true,
useKeysAsHeaders: true,
};
const exportToCsvInstance = new ExportToCsv(options);
const string = exportToCsvInstance.generateCsv(mockData, true);
const firstLine = string.split('\n')[0];
const keys = firstLine.split(',').map((s: string) => s.trim());
const mockDataKeys = Object.keys(mockData[0]);
expect(keys).toEqual(mockDataKeys);
});
// it('should properly overwrite default options through contructor', () => {
// const exportToCsvInstance = new ExportToCsv();
// const defaults = { ...exportToCsvInstance.options };
// });
it('should initiate download through spawned browser', () => {
if (!window) {
pending('it should only initiate download when run in browser context');
}
const options: Options = {
title: "Test Csv",
useBom: true,
useKeysAsHeaders: true
}
const exportToCsvInstance = new ExportToCsv(options);
exportToCsvInstance.generateCsv(mockData);
});
});
describe('ExportToCsv As A Text File', () => {
it('should create a comma seperated string', () => {
const options: Options = {
title: "Test Csv 1",
useTextFile: true,
useBom: true,
useKeysAsHeaders: true,
};
const exportToCsvInstance = new ExportToCsv(options);
const string = exportToCsvInstance.generateCsv(mockData, true);
expect(string).toBeTruthy(typeof string === 'string');
});
it('should use keys of first object in collection as headers', () => {
const options: Options = {
filename: "Test Csv 2",
useTextFile: true,
useBom: true,
useKeysAsHeaders: true,
};
const exportToCsvInstance = new ExportToCsv(options);
const string = exportToCsvInstance.generateCsv(mockData, true);
const firstLine = string.split('\n')[0];
const keys = firstLine.split(',').map((s: string) => s.trim());
const mockDataKeys = Object.keys(mockData[0]);
expect(keys).toEqual(mockDataKeys);
});
it('should initiate download through spawned browser', () => {
if (!window) {
pending('it should only initiate download when run in browser context');
}
const options: Options = {
filename: "Test Csv 3",
useTextFile: true,
useBom: true,
useKeysAsHeaders: true
};
const exportToCsvInstance = new ExportToCsv(options);
exportToCsvInstance.generateCsv(mockData);
});
});