This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils-spec.js
351 lines (322 loc) · 11.7 KB
/
utils-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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"use strict";
describe("Core - Utils", function() {
var utils;
beforeAll(function(done) {
require(["core/utils"], function(u) {
utils = u;
done();
});
});
describe("calculateLeftPad()", function(){
it("throws given invalid input", function(){
expect(function(){
expect(utils.calculateLeftPad());
}).toThrow();
expect(function(){
expect(utils.calculateLeftPad({}));
}).toThrow();
expect(function(){
expect(utils.calculateLeftPad(123));
}).toThrow();
expect(function(){
expect(utils.calculateLeftPad(null));
}).toThrow();
});
it("calculates the smallest left padding of multiline text", function(done){
expect(utils.calculateLeftPad("")).toEqual(0);
expect(utils.calculateLeftPad("\n \n ")).toEqual(2);
expect(utils.calculateLeftPad(" ")).toEqual(25);
expect(utils.calculateLeftPad(" a ")).toEqual(1);
expect(utils.calculateLeftPad(" \n a ")).toEqual(1);
expect(utils.calculateLeftPad(" \n a ")).toEqual(1);
expect(utils.calculateLeftPad("\n \n \n ")).toEqual(4);
expect(utils.calculateLeftPad("\n \n \n ")).toEqual(2);
expect(utils.calculateLeftPad("\n \n \n \n ")).toEqual(2);
expect(utils.calculateLeftPad("\n\n\n\n\n\n\n\n\n\n")).toEqual(0);
expect(utils.calculateLeftPad(" \n\n\n\n\n \n\n\n\n\n ")).toEqual(2);
done();
});
});
describe("makeOwnerSwapper()", function() {
it("throws if passed something that is not a node", function(done) {
expect(function() {
utils.toESIterable(function() {});
}).not.toThrow();
expect(function() {
utils.toESIterable("");
}).toThrow();
expect(function() {
utils.toESIterable(null);
}).toThrow();
expect(function() {
utils.toESIterable([]);
}).toThrow();
expect(function() {
utils.toESIterable(undefined);
}).toThrow();
done();
});
it("returns a function", function() {
var testNode = document.createTextNode("test");
var testFunction = utils.makeOwnerSwapper(testNode);
expect(testFunction instanceof Function).toBe(true);
});
it("removes the original node from the its owner document", function() {
var testNode = document.createTextNode("test");
var swapTestNode = utils.makeOwnerSwapper(testNode);
var newDoc = document.implementation.createHTMLDocument("test");
document.body.appendChild(testNode);
expect(document.body.contains(testNode)).toBe(true);
swapTestNode(newDoc, newDoc.body);
expect(document.body.contains(testNode)).toBe(false);
expect(testNode.ownerDocument).toEqual(newDoc);
});
it("appends the node into a new document", function() {
var testNode = document.createElement("link");
var swapTestNode = utils.makeOwnerSwapper(testNode);
var newDoc = document.implementation.createHTMLDocument("test");
expect(document.head.contains(testNode)).toBe(false);
swapTestNode(newDoc, newDoc.head);
expect(newDoc.head.contains(testNode)).toBe(true);
expect(testNode.ownerDocument).toEqual(newDoc);
});
it("prepends the node into a new document at the right place", function() {
var testNode = document.createElement("link");
var swapTestNode = utils.makeOwnerSwapper(testNode);
var newDoc = document.implementation.createHTMLDocument("test");
var metaElem = newDoc.createElement("meta");
newDoc.head.appendChild(metaElem);
swapTestNode(newDoc, newDoc.head);
expect(newDoc.head.firstChild).toEqual(testNode);
expect(newDoc.head.lastChild).toEqual(metaElem);
});
});
describe("findFirstElementChild() method", function(){
it("doesn't find elements when there are none to find", function(done){
var textNode = document.createTextNode("text");
expect(utils.findFirstElementChild(textNode)).toBe(null);
done();
});
it("finds the first Element child amongst a list of nodes", function(done){
var div = document.createElement("div");
div.innerHTML = "text <span>pass</span> text <span>fail</span> text";
expect(utils.findFirstElementChild(div)).toBeTruthy();
expect(utils.findFirstElementChild(div).textContent).toEqual("pass");
done();
});
});
describe("toESIterable() method", function() {
it("throws if passed something that is not a function", function(done) {
expect(function(){
utils.toESIterable(function(){});
}).not.toThrow();
expect(function(){
utils.toESIterable("");
}).toThrow();
expect(function(){
utils.toESIterable(null);
}).toThrow();
expect(function(){
utils.toESIterable([]);
}).toThrow();
expect(function(){
utils.toESIterable(undefined);
}).toThrow();
done();
});
it("creates an object that conforms to the ES iterator protocol", function(done) {
var genericObject = {
values: [1, 2, 3, 4],
current: 0,
nextValue: function() {
return this.values[this.current++] || null;
}
};
var iterable = utils.toESIterable(genericObject.nextValue.bind(genericObject));
expect(Object.getOwnPropertySymbols(genericObject)).not.toContain(Symbol.iterator);
expect(Object.getOwnPropertySymbols(iterable)).toContain(Symbol.iterator);
expect(Array.from(iterable)).toEqual([1, 2, 3, 4]);
done();
});
});
describe("normalizePadding() method", function() {
it("throws given an argument that is not a string", function(done) {
expect(function() {
utils.normalizePadding({});
}).toThrow();
expect(function() {
utils.normalizePadding([]);
}).toThrow();
expect(function() {
utils.normalizePadding(123);
}).toThrow();
done();
});
it("should return the empty string given falsy values", function(done) {
expect(utils.normalizePadding()).toEqual("");
expect(utils.normalizePadding("")).toEqual("");
expect(utils.normalizePadding(null)).toEqual("");
done();
});
it("should normalise whitespace, but ignore white with pre tags", function(done) {
var str = " trim start\n * trim 3 from start \n <pre>trim 1\n if(x){\n\t party()</pre>\n foo \n bar";
var testStrings = utils
.normalizePadding(str)
.split("\n");
expect(testStrings[0]).toEqual("trim start");
expect(testStrings[1]).toEqual(" * trim 3 from start ");
expect(testStrings[2]).toEqual(" <pre>trim 1");
expect(testStrings[3]).toEqual(" if(x){");
expect(testStrings[4]).toEqual("\t party()</pre>");
expect(testStrings[5]).toEqual("foo ");
expect(testStrings[6]).toEqual(" bar");
done();
});
});
// linkCSS()
it("should add a link element", function(done) {
utils.linkCSS(document, "BOGUS");
expect($("link[href='BOGUS']").length).toEqual(1);
$("link[href='BOGUS']").remove();
done();
});
it("should add several link elements", function(done) {
utils.linkCSS(document, ["BOGUS", "BOGUS", "BOGUS"]);
expect($("link[href='BOGUS']").length).toEqual(3);
$("link[href='BOGUS']").remove();
done();
});
// $.renameElement()
it("should rename the element", function(done) {
var $div = $("<div><p><a></a></p><b>some text</b></div>").appendTo($("body"));
$div.find("p").renameElement("span");
$div.find("b").renameElement("i");
expect($div.find("span").length).toEqual(1);
expect($div.find("i").text()).toEqual("some text");
$div.remove();
done();
});
// lead0
it("should prepend 0 only when needed", function(done) {
expect(utils.lead0("1")).toEqual("01");
expect(utils.lead0("01")).toEqual("01");
done();
});
// concatDate
it("should format the date as needed", function(done) {
var d = new Date();
d.setFullYear(1977);
d.setMonth(2);
d.setDate(15);
expect(utils.concatDate(d)).toEqual("19770315");
expect(utils.concatDate(d, "-")).toEqual("1977-03-15");
done();
});
// parseSimpleDate
it("should parse a simple date", function(done) {
var d = utils.parseSimpleDate("1977-03-15");
expect(d.getFullYear()).toEqual(1977);
expect(d.getMonth()).toEqual(2);
expect(d.getDate()).toEqual(15);
done();
});
// parseLastModified
it("should parse a date in lastModified format", function(done) {
var d = utils.parseLastModified("03/15/1977 13:05:42");
expect(d.getFullYear()).toEqual(1977);
expect(d.getMonth()).toEqual(2);
expect(d.getDate()).toEqual(15);
done();
});
// humanDate
it("should produce a human date", function(done) {
expect(utils.humanDate("1977-03-15")).toEqual("15 March 1977");
var d = new Date();
d.setFullYear(1977);
d.setMonth(2);
d.setDate(15);
expect(utils.humanDate(d)).toEqual("15 March 1977");
done();
});
// isoDate
it("should produce an ISO date", function(done) {
expect(utils.isoDate("2013-06-25")).toMatch(/2013-06-2[45]T/);
var d = new Date();
d.setFullYear(2013);
d.setDate(25);
d.setMonth(8);
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
expect(utils.isoDate(d)).toMatch(/2013-09-2[45]T/);
done();
});
// joinAnd
it("should join with proper commas and 'and'", function(done) {
expect(utils.joinAnd([])).toEqual("");
expect(utils.joinAnd(["x"])).toEqual("x");
expect(utils.joinAnd(["x", "x"])).toEqual("x and x");
expect(utils.joinAnd(["x", "x", "x"])).toEqual("x, x, and x");
expect(utils.joinAnd(["x", "x", "x", "x"])).toEqual("x, x, x, and x");
expect(utils.joinAnd(["x", "x", "x", "x"], function(str) {
return str.toUpperCase();
})).toEqual("X, X, X, and X");
done();
});
// xmlEscape
it("should escape properly", function(done) {
expect(utils.xmlEscape("&<>\"")).toEqual("&<>"");
done();
});
// norm
it("should normalise text", function(done) {
expect(utils.norm(" a b ")).toEqual("a b");
done();
});
// toKeyValuePairs
it("should convert objects to key values pairs", function(done) {
var obj = {
editors: [{
"name": "Person Name"
}],
specStatus: "ED",
edDraftURI: "http://foo.com",
shortName: "Foo"
};
var expected = "editors=[{\"name\":\"Person Name\"}], specStatus=\"ED\", " +
"edDraftURI=\"http://foo.com\", shortName=\"Foo\"";
expect(utils.toKeyValuePairs(obj)).toEqual(expected);
done();
});
it("should convert objects to key values pairs with different separator", function(done) {
var obj = {
editors: [{
"name": "Person Name"
}],
specStatus: "ED",
edDraftURI: "http://foo.com",
shortName: "Foo"
};
var expected = "editors=[{\"name\":\"Person Name\"}]|||specStatus=\"ED\"|||" +
"edDraftURI=\"http://foo.com\"|||shortName=\"Foo\"";
expect(utils.toKeyValuePairs(obj, "|||")).toEqual(expected);
done();
});
it("should convert objects to key values pairs with different separator and delimiter", function(done) {
var obj = {
editors: [{
"name": "Person Name"
}],
specStatus: "ED",
edDraftURI: "http://foo.com",
shortName: "Foo"
};
var expected = "editors;[{\"name\":\"Person Name\"}], specStatus;\"ED\", " +
"edDraftURI;\"http://foo.com\", shortName;\"Foo\"";
expect(utils.toKeyValuePairs(obj, null, ";")).toEqual(expected);
expected = "editors^[{\"name\":\"Person Name\"}] % specStatus^\"ED\" % " +
"edDraftURI^\"http://foo.com\" % shortName^\"Foo\"";
expect(utils.toKeyValuePairs(obj, " % ", "^")).toEqual(expected);
done();
});
});