This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
/
cell.test.js
89 lines (81 loc) · 3.33 KB
/
cell.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
83
84
85
86
87
88
89
const test = require('tape');
const DOMParser = require('xmldom').DOMParser;
const xl = require('../source/index');
test('Cell coverage', (t) => {
t.plan(1);
let wb = new xl.Workbook();
let ws = wb.addWorksheet('test');
let cellAccessor = ws.cell(1, 1);
t.ok(cellAccessor, 'Correctly generated cellAccessor object');
});
test('Cell returns correct number of cell references', (t) => {
t.plan(1);
let wb = new xl.Workbook();
let ws = wb.addWorksheet('test');
let cellAccessor = ws.cell(1, 1, 5, 2);
t.ok(cellAccessor.excelRefs.length === 10, 'cellAccessor returns correct number of cellRefs');
});
test('Add String to cell', (t) => {
t.plan(3);
let wb = new xl.Workbook();
let ws = wb.addWorksheet('test');
let cell = ws.cell(1, 1).string('my test string');
let thisCell = ws.cells[cell.excelRefs[0]];
t.ok(thisCell.t === 's', 'cellType set to sharedString');
t.ok(typeof (thisCell.v) === 'number', 'cell Value is a number');
t.ok(wb.sharedStrings[thisCell.v] === 'my test string', 'Cell sharedString value is correct');
});
test('Replace null or undefined value with empty string', (t) => {
t.plan(3);
let wb = new xl.Workbook();
let ws = wb.addWorksheet('test');
let cell = ws.cell(1, 1).string(null);
let thisCell = ws.cells[cell.excelRefs[0]];
t.ok(thisCell.t === 's', 'cellType set to sharedString');
t.ok(typeof (thisCell.v) === 'number', 'cell Value is a number');
t.ok(wb.sharedStrings[thisCell.v] === '', 'Cell is empty string');
});
test('Add Number to cell', (t) => {
t.plan(3);
let wb = new xl.Workbook();
let ws = wb.addWorksheet('test');
let cell = ws.cell(1, 1).number(10);
let thisCell = ws.cells[cell.excelRefs[0]];
t.ok(thisCell.t === 'n', 'cellType set to number');
t.ok(typeof (thisCell.v) === 'number', 'cell Value is a number');
t.ok(thisCell.v === 10, 'Cell value value is correct');
});
test('Add Boolean to cell', (t) => {
t.plan(3);
let wb = new xl.Workbook();
let ws = wb.addWorksheet('test');
let cell = ws.cell(1, 1).bool(true);
let thisCell = ws.cells[cell.excelRefs[0]];
t.ok(thisCell.t === 'b', 'cellType set to boolean');
t.ok(typeof (thisCell.v) === 'string', 'cell Value is a string');
t.ok(thisCell.v === 'true' || thisCell.v === 'false', 'Cell value value is correct');
});
test('Add Formula to cell', (t) => {
t.plan(4);
let wb = new xl.Workbook();
let ws = wb.addWorksheet('test');
let cell = ws.cell(1, 1).formula('SUM(A1:A10)');
let thisCell = ws.cells[cell.excelRefs[0]];
t.ok(thisCell.t === null, 'cellType is not set');
t.ok(thisCell.v === null, 'cellValue is not set');
t.ok(typeof (thisCell.f) === 'string', 'cell Formula is a string');
t.ok(thisCell.f === 'SUM(A1:A10)', 'Cell value value is correct');
});
test('Add Comment to cell', (t) => {
let wb = new xl.Workbook();
let ws = wb.addWorksheet('test');
let cell = ws.cell(1, 1).comment('My test comment');
let ref = cell.excelRefs[0];
t.ok(ws.comments[ref].comment === 'My test comment');
ws.generateCommentsXML().then((XML) => {
let doc = new DOMParser().parseFromString(XML);
let testComment = doc.getElementsByTagName('commentList')[0];
t.ok(testComment.textContent === 'My test comment', 'Verify comment text is correct');
t.end()
});
});