-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathsaveTable.js
More file actions
142 lines (127 loc) · 3.84 KB
/
saveTable.js
File metadata and controls
142 lines (127 loc) · 3.84 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
suite('saveTable', function() {
let validFile = 'unit/assets/csv.csv';
let myp5;
let myTable;
setup(function(done) {
new p5(function(p) {
p.setup = function() {
myp5 = p;
done();
};
});
});
teardown(function() {
myp5.remove();
});
setup(function disableFileLoadError() {
sinon.stub(p5, '_friendlyFileLoadError');
});
teardown(function restoreFileLoadError() {
p5._friendlyFileLoadError.restore();
});
setup(function loadMyTable(done) {
myp5.loadTable(validFile, 'csv', 'header', function(table) {
myTable = table;
done();
});
});
test('should be a function', function() {
assert.ok(myp5.saveTable);
assert.typeOf(myp5.saveTable, 'function');
});
test('no friendly-err-msg I', function() {
assert.doesNotThrow(
function() {
myp5.saveTable(myTable, 'myfile');
},
Error,
'got unwanted exception'
);
});
test('no friendly-err-msg II', function() {
assert.doesNotThrow(
function() {
myp5.saveTable(myTable, 'myfile', 'csv');
},
Error,
'got unwanted exception'
);
});
testUnMinified('missing param #1', function() {
assert.validationError(function() {
myp5.saveTable(myTable);
});
});
testUnMinified('wrong param type #0', function() {
assert.validationError(function() {
myp5.saveTable('myTable', 'myfile');
});
});
testUnMinified('wrong param type #1', function() {
assert.validationError(function() {
myp5.saveTable(myTable, 2);
});
});
testUnMinified('wrong param type #2', function() {
assert.validationError(function() {
myp5.saveTable(myTable, 'myfile', 2);
});
});
testWithDownload(
'should download a file with expected contents',
async function(blobContainer) {
myp5.saveTable(myTable, 'filename');
let myBlob = blobContainer.blob;
let text = await myBlob.text();
let expected =
'name,age,height\n' +
'David,31,80\n' +
'"David, Jr.",11,61.5\n' +
'"David,\nSr. ""the boss""",95,88\n';
assert.strictEqual(text, expected);
},
true
);
testWithDownload(
'should download a file with expected contents (tsv)',
async function(blobContainer) {
myp5.saveTable(myTable, 'filename', 'tsv');
let myBlob = blobContainer.blob;
let text = await myBlob.text();
let myTableStr = myTable.columns.join('\t') + '\n';
for (let i = 0; i < myTable.rows.length; i++) {
myTableStr += myTable.rows[i].arr.join('\t') + '\n';
}
assert.strictEqual(text, myTableStr);
},
true
);
testWithDownload(
'should download a file with expected contents (html)',
async function(blobContainer) {
myp5.saveTable(myTable, 'filename', 'html');
let myBlob = blobContainer.blob;
let text = await myBlob.text();
let domparser = new DOMParser();
let htmldom = domparser.parseFromString(text, 'text/html');
let trs = htmldom.querySelectorAll('tr');
for (let i = 0; i < trs.length; i++) {
let tds = trs[i].querySelectorAll('td');
for (let j = 0; j < tds.length; j++) {
// saveTable generates an HTML file with indentation spaces and line-breaks. The browser ignores these
// while displaying. But they will still remain a part of the parsed DOM and hence must be removed.
// More info at: https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Whitespace
let tdText = tds[j].innerHTML.trim().replace(/\n/g, '');
let tbText;
if (i === 0) {
tbText = myTable.columns[j].trim().replace(/\n/g, '');
} else {
tbText = myTable.rows[i - 1].arr[j].trim().replace(/\n/g, '');
}
assert.strictEqual(tdText, tbText);
}
}
},
true
);
});