-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexceljs.spec.js
91 lines (80 loc) · 2.41 KB
/
exceljs.spec.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
83
84
85
86
87
88
89
90
91
/* global ExcelJS */
// ExcelJS is a global injected by `./dist/exceljs.js` during jasmine's setup
'use strict';
function unexpectedError(done) {
return function(error) {
// eslint-disable-next-line no-console
console.error('Error Caught', error.message, error.stack);
expect(true).toEqual(false);
done();
};
}
describe('ExcelJS', () => {
it('should read and write xlsx via binary buffer', done => {
const wb = new ExcelJS.Workbook();
const ws = wb.addWorksheet('blort');
ws.getCell('A1').value = 'Hello, World!';
ws.getCell('A2').value = 7;
wb.xlsx
.writeBuffer()
.then(buffer => {
const wb2 = new ExcelJS.Workbook();
return wb2.xlsx.load(buffer).then(() => {
const ws2 = wb2.getWorksheet('blort');
expect(ws2).toBeTruthy();
expect(ws2.getCell('A1').value).toEqual('Hello, World!');
expect(ws2.getCell('A2').value).toEqual(7);
done();
});
})
.catch(error => {
throw error;
})
.catch(unexpectedError(done));
});
it('should read and write xlsx via base64 buffer', done => {
const options = {
base64: true,
};
const wb = new ExcelJS.Workbook();
const ws = wb.addWorksheet('blort');
ws.getCell('A1').value = 'Hello, World!';
ws.getCell('A2').value = 7;
wb.xlsx
.writeBuffer(options)
.then(buffer => {
const wb2 = new ExcelJS.Workbook();
return wb2.xlsx.load(buffer.toString('base64'), options).then(() => {
const ws2 = wb2.getWorksheet('blort');
expect(ws2).toBeTruthy();
expect(ws2.getCell('A1').value).toEqual('Hello, World!');
expect(ws2.getCell('A2').value).toEqual(7);
done();
});
})
.catch(error => {
throw error;
})
.catch(unexpectedError(done));
});
it('should write csv via buffer', done => {
const wb = new ExcelJS.Workbook();
const ws = wb.addWorksheet('blort');
ws.getCell('A1').value = 'Hello, World!';
ws.getCell('B1').value = 'What time is it?';
ws.getCell('A2').value = 7;
ws.getCell('B2').value = '12pm';
wb.csv
.writeBuffer()
.then(buffer => {
expect(buffer.toString()).toEqual(
'"Hello, World!",What time is it?\n7,12pm'
);
done();
})
.catch(error => {
throw error;
})
.catch(unexpectedError(done));
});
});