-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsv-tests.js
187 lines (150 loc) · 4.69 KB
/
csv-tests.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
(function(ut, Csv, window) {
"use strict";
var engine = new ut.Engine();
engine.add("Csv constructor should save properties order",
function(testContext, the) {
// Arrange
var properties = ["a", "b"];
// Act
var csv = new Csv(properties);
// Assert
the("propertyOrder").propertyOf(csv).shouldBeSameArrayAs(properties);
});
engine.add("Csv constructor should initiate the items property",
function(testContext, the) {
// Arrange
var properties = ["a", "b"];
// Act
var csv = new Csv(properties);
// Assert
the("items").propertyOf(csv).shouldBeArray();
the("length").propertyOf(csv.items).shouldBeExactly(0);
});
engine.add("Csv.add should add one item",
function(testContext, the) {
// Arrange
var properties = ["a", "b"];
var csv = new Csv(properties);
// Act
csv.add({"a" : 1, "b" : 2});
// Assert
the("length").propertyOf(csv.items).shouldBeExactly(1);
the("a").propertyOf(csv.items[0]).shouldBeExactly(1);
the("b").propertyOf(csv.items[0]).shouldBeExactly(2);
});
engine.add("Csv.getFileContents should create file correctly",
function(testContext, the) {
// Arrange
var properties = ["a", "b"];
var csv = new Csv(properties);
csv.add({"a" : "Abc", "b" : "Def"});
csv.add({"a" : "Ghi", "b" : "Jkl"});
// Act
var file = csv.getFileContents();
// Assert
the(file).shouldBeSameAs("Abc,Def\r\nGhi,Jkl");
});
engine.add("Csv.getFileContents(separator) should create file correctly",
function(testContext, the) {
// Arrange
var properties = ["a", "b"];
var csv = new Csv(properties);
csv.add({"a" : "Abc", "b" : "Def"});
csv.add({"a" : "Ghi", "b" : "Jkl"});
// Act
var file = csv.getFileContents(";");
// Assert
the(file).shouldBeSameAs("Abc;Def\r\nGhi;Jkl");
});
engine.add("Csv.getFileContents(separator, true) should create file correctly",
function(testContext, the) {
// Arrange
var properties = ["a", "b"];
var csv = new Csv(properties);
csv.add({"a" : "Abc", "b" : "Def"});
csv.add({"a" : "Ghi", "b" : "Jkl"});
// Act
var file = csv.getFileContents(";", true);
// Assert
the(file).shouldBeSameAs("a;b\r\nAbc;Def\r\nGhi;Jkl");
});
engine.add("Csv.getFileContents should quote fields containing the separator",
function(testContext, the) {
// Arrange
var properties = ["name", "age"];
var csv = new Csv(properties);
csv.add({"name" : "Doe, John", "age" : "Unknown"});
csv.add({"name" : "Bunny", "age" : "2"});
// Act
var file = csv.getFileContents();
// Assert
the(file).shouldBeSameAs("\"Doe, John\",Unknown\r\nBunny,2");
});
engine.add("Csv.getFileContents should quote and escape fields containing double quotes",
function(testContext, the) {
// Arrange
var properties = ["model", "size"];
var csv = new Csv(properties);
csv.add({"model" : "Punysonic", "size" : "28\""});
csv.add({"model" : "Philip", "size" : "42\""});
// Act
var file = csv.getFileContents();
// Assert
the(file).shouldBeSameAs('Punysonic,"28"""\r\nPhilip,"42"""');
});
engine.add("Csv.saveAs should call window.saveAs correctly",
function(testContext, the) {
// Arrange
var properties = ["a", "b"];
var csv = new Csv(properties);
csv.add({"a" : "Abc", "b" : "Def"});
csv.add({"a" : "Ghi", "b" : "Jkl"});
var saveAsCalled = false, savedBlob, savedFilename;
var mockSaveAs = function(blob, filename) {
saveAsCalled = true;
savedBlob = blob;
savedFilename = filename;
};
var oldSaveAs = window.saveAs;
window.saveAs = mockSaveAs;
// Act
csv.saveAs("output.csv", ";", false);
// Cleanup
window.saveAs = oldSaveAs;
// Assert
the(saveAsCalled).shouldBeTrue();
the(savedBlob).shouldBeInstanceOf(window.Blob);
the(savedFilename).shouldBeSameAs("output.csv");
});
engine.add("Csv.saveAs should add UTF-8 Byte Order Mark to beginning of file",
function(testContext, the) {
// Arrange
var properties = ["a", "b"];
var csv = new Csv(properties);
csv.add({"a" : "Abc", "b" : "Def"});
csv.add({"a" : "Ghi", "b" : "Jkl"});
var savedBlob;
var mockSaveAs = function(blob, filename) {
savedBlob = blob;
};
var oldSaveAs = window.saveAs;
window.saveAs = mockSaveAs;
// Act
csv.saveAs("output.csv", ";", false);
// Cleanup
window.saveAs = oldSaveAs;
// Assert
var bom;
testContext.actAndWait(1000, function(testContext, the) {
var reader = new window.FileReader();
reader.addEventListener("loadend", function() {
bom = reader.result;
testContext.actDone();
});
var firstThreeBytes = savedBlob.slice(0, 3);
reader.readAsArrayBuffer(firstThreeBytes);
}).thenAssert(function(testContext, the) {
the(bom).shouldBeSameArrayAs([0xef, 0xbb, 0xbf]);
});
});
})(ut, Csv, window);