-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
utils-spec.js
198 lines (181 loc) · 7.07 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
describe("Core - Utils", function () {
var MAXOUT = 5000, utils, $;
beforeEach(function () {
runs(function () {
require.config({ baseUrl: "../js/" });
require(["jquery", "core/utils"], function (jq, u) {
$ = jq;
utils = u;
});
});
waitsFor(function () { return utils; }, MAXOUT);
});
// linkCSS()
it("should add a link element", function () {
runs(function () {
utils.linkCSS(document, "BOGUS");
expect($("link[href='BOGUS']").length == 1).toBeTruthy();
$("link[href='BOGUS']").remove();
});
});
it("should add several link elements", function () {
runs(function () {
utils.linkCSS(document, ["BOGUS", "BOGUS", "BOGUS"]);
expect($("link[href='BOGUS']").length == 3).toBeTruthy();
$("link[href='BOGUS']").remove();
});
});
// $.renameElement()
it("should rename the element", function () {
runs(function () {
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();
});
});
// lead0
it("should prepend 0 only when needed", function () {
runs(function () {
expect(utils.lead0("1")).toEqual("01");
expect(utils.lead0("01")).toEqual("01");
});
});
// concatDate
it("should format the date as needed", function () {
runs(function () {
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");
});
});
// parseSimpleDate
it("should parse a simple date", function () {
runs(function () {
var d = utils.parseSimpleDate("1977-03-15");
expect(d.getFullYear()).toEqual(1977);
expect(d.getMonth()).toEqual(2);
expect(d.getDate()).toEqual(15);
});
});
// parseLastModified
it("should parse a date in lastModified format", function () {
runs(function () {
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);
});
});
// humanDate
it("should produce a human date", function () {
runs(function () {
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");
});
});
// isoDate
it("should produce an ISO date", function () {
runs(function () {
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/);
});
});
// joinAnd
it("should join with proper commas and 'and'", function () {
runs(function () {
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");
});
});
// xmlEscape
it("should escape properly", function () {
runs(function () {
expect(utils.xmlEscape("&<>\"")).toEqual("&<>"");
});
});
// norm
it("should normalise text", function () {
runs(function () {
expect(utils.norm(" a b ")).toEqual("a b");
});
});
// $.getDfnTitles()
it("should find the definition title", function () {
runs(function () {
var $dfn = $("<dfn lt='DFN|DFN2|DFN3'><abbr title='ABBR'>TEXT</abbr></dfn>").appendTo($("body"));
var titles = $dfn.getDfnTitles( { isDefinition: true } );
expect(titles[0]).toEqual("dfn");
expect(titles[1]).toEqual("dfn2");
expect(titles[2]).toEqual("dfn3");
$dfn.removeAttr("data-lt");
expect($dfn.getDfnTitles()[0]).toEqual("abbr");
$dfn.find("abbr").removeAttr("title");
expect($dfn.getDfnTitles()[0]).toEqual("text");
$dfn.remove();
});
});
// $.getDfnTitles()
it("should return list of terms when called a second time", function () {
runs(function () {
var $dfn = $("<dfn lt='DFN|DFN2|DFN3'>TEXT</dfn>").appendTo($("body"));
var titles = $dfn.getDfnTitles( { isDefinition: true } );
expect(titles[0]).toEqual("dfn");
expect(titles[1]).toEqual("dfn2");
expect(titles[2]).toEqual("dfn3");
expect($dfn.attr("data-lt")).toEqual('dfn|dfn2|dfn3');
expect($dfn.getDfnTitles()[0]).toEqual("dfn");
$dfn.remove();
});
});
// $.makeID()
it("should create the proper ID", function () {
runs(function () {
expect($("<p id='ID'></p>").makeID()).toEqual("ID");
expect($("<p title='TITLE'></p>").makeID()).toEqual("title");
expect($("<p>TEXT</p>").makeID()).toEqual("text");
expect($("<p></p>").makeID(null, "PASSED")).toEqual("passed");
expect($("<p></p>").makeID("PFX", "PASSED")).toEqual("PFX-passed");
expect($("<p>TEXT</p>").makeID("PFX")).toEqual("PFX-text");
var $p = $("<p>TEXT</p>");
$p.makeID();
expect($p.attr("id")).toEqual("text");
expect($("<p> A--Bé9\n C</p>").makeID()).toEqual("a--b-9-c");
expect($("<p></p>").makeID()).toEqual("generatedID");
expect($("<p>2017</p>").makeID()).toEqual("x2017");
var $div = $("<div><p id='a'></p><p id='a-1'></p><span>A</span></div>").appendTo($("body"));
expect($div.find("span").makeID()).toEqual("a-2");
$div.remove();
});
});
// $.allTextNodes()
it("should find all the text nodes", function () {
runs(function () {
var tns = $("<div>aa<span>bb</span><p>cc<i>dd</i></p><pre>nope</pre></div>").allTextNodes(["pre"]);
expect(tns.length).toEqual(4);
var str = "";
for (var i = 0, n = tns.length; i < n; i++) str += tns[i].nodeValue;
expect(str).toEqual("aabbccdd");
});
});
});