-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.js
230 lines (169 loc) · 8.05 KB
/
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
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
/* global describe, it */
var assert = require("assert");
var manipulator = require("./index");
var list = manipulator.list;
function dummy (classList) {
var attributes = {
"class": classList || ""
};
return {
setAttribute: function (name, value) { attributes[name] = value; },
getAttribute: function (name) { return attributes[name]; }
};
}
function expectThrow (fn, msg) {
try {
fn();
assert(false, msg);
}
catch (e) {
// ...
}
}
function startsWithBa (str) {
return !!str.match(/^ba/);
}
function not (fn) {
return function () { return !fn.apply(null, arguments); };
}
function longerThanThree (str) {
return str.length > 3;
}
describe("list", function () {
describe("list(classes)", function () {
it("creates a class list wrapper given a string", function () {
assert.equal(list("foo bar").toString(), "foo bar");
});
it("creates a class list wrapper given a DOM Element", function () {
assert.equal(list(dummy("foo bar")).toString(), "foo bar");
});
it("accepts only objects and strings as input", function () {
assert.equal(list("foo [bla] bawe23ur0u2!").toString(), "foo [bla] bawe23ur0u2!");
assert.equal(list(dummy("foo bar")).toString(), "foo bar");
[undefined, null, 0, 1, 2, 100, -50, /foo/, NaN, Infinity].forEach(function (input) {
expectThrow(function () { list(input); }, "Must throw on: " + input);
});
});
});
describe(".add(name)", function () {
it("adds a class name to the list", function () {
assert.equal(list("foo bar").add("baz").toString(), "foo bar baz");
});
it("adds a class name to the list only once", function () {
assert.equal(list("foo bar").add("baz").add("baz").toString(), "foo bar baz");
});
it("adds many class names to the list given a class string with spaces", function () {
assert.equal(list("foo bar").add("baz nub").toString(), "foo bar baz nub");
});
});
describe(".addMany(names)", function () {
it("works for arrays as input", function () {
assert.equal(list("foo").addMany(["bar", "baz", "nub"]).toString(), "foo bar baz nub");
});
it("works for class strings as input", function () {
assert.equal(list("foo").addMany("bar baz nub").toString(), "foo bar baz nub");
});
});
describe(".remove(name)", function () {
it("removes a class name from the list", function () {
assert.equal(list("foo bar baz").remove("bar").toString(), "foo baz");
});
it("removes many class names from the list given a class string with spaces", function () {
assert.equal(list("foo bar baz").remove("foo baz").toString(), "bar");
});
});
describe(".removeMany(names)", function () {
it("removes class names from the list given an array of names", function () {
assert.equal(list("foo bar baz").removeMany(["bar", "baz"]).toString(), "foo");
});
it("removes class names from the list given a class string with spaces", function () {
assert.equal(list("foo bar baz").removeMany("bar baz").toString(), "foo");
});
});
describe(".has(name)", function () {
it("returns true when given an existing name, false otherwise", function () {
assert.equal(list("foo bar baz").has("bar"), true);
assert.equal(list("foo bar baz").has("nub"), false);
});
it("works for class strings with spaces as input as well", function () {
assert.equal(list("foo bar baz").has("bar baz"), true);
assert.equal(list("foo bar baz").has("nub bar"), false);
});
});
describe(".hasAll(names)", function () {
it("returns true when given only existing names, false otherwise", function () {
assert.equal(list("foo bar baz").hasAll(["bar", "foo"]), true);
assert.equal(list("foo bar baz").hasAll(["nub", "baz"]), false);
});
it("works for class strings as well", function () {
assert.equal(list("foo bar baz").hasAll("bar foo"), true);
assert.equal(list("foo bar baz").hasAll("nub baz"), false);
});
});
describe(".hasSome(name)", function () {
it("returns true when at least one name exists, false otherwise", function () {
assert.equal(list("foo bar baz").hasSome(["bar", "foo"]), true);
assert.equal(list("foo bar baz").hasSome(["nub", "baz"]), true);
assert.equal(list("foo bar baz").hasSome(["nub", "biz"]), false);
});
it("works for class strings as well", function () {
assert.equal(list("foo bar baz").hasSome("bar nub foo"), true);
assert.equal(list("foo bar baz").hasSome("nub biz"), false);
});
});
describe(".toggle(name)", function () {
it("removes a name if it exists, adds it otherwise", function () {
assert.equal(list("foo bar baz").toggle("bar").toString(), "foo baz");
assert.equal(list("foo baz").toggle("bar").toString(), "foo baz bar");
});
it("works for class strings with spaces, too", function () {
assert.equal(list("foo bar baz").toggle("bar foo").toString(), "baz");
assert.equal(list("foo baz bar").toggle("baz nub").toString(), "foo bar nub");
});
});
describe(".toggleMany(names)", function () {
it("removes names if they exists, adds them otherwise", function () {
assert.equal(list("foo bar baz").toggleMany(["bar", "baz"]).toString(), "foo");
assert.equal(list("foo baz").toggleMany(["bar", "baz"]).toString(), "foo bar");
});
it("works for class strings with spaces, too", function () {
assert.equal(list("foo bar baz").toggleMany("bar foo").toString(), "baz");
assert.equal(list("foo baz bar").toggleMany("baz nub").toString(), "foo bar nub");
});
});
describe(".clear()", function () {
it("removes all names", function () {
assert.equal(list("foo bar baz").clear().toString(), "");
assert.equal(list("foo baz").clear().toString(), "");
});
});
describe(".sort()", function () {
it("must sort the names alphabetically", function () {
assert.equal(list("foo bar baz").sort().toString(), "bar baz foo");
assert.equal(list("b0 b1 a4 x10 y").sort().toString(), "a4 b0 b1 x10 y");
});
});
describe(".filter(fn)", function () {
it("must remove all names not matched by a given predicate function", function () {
assert.equal(list("foo bar baz").filter(startsWithBa).toString(), "bar baz");
assert.equal(list("footer bar mana").filter(not(longerThanThree)).toString(), "bar");
});
});
describe(".apply()", function () {
it("changes must only be written to the element on .apply()", function () {
var el = dummy("foo bar baz");
var l = list(el);
l.remove("bar").add("nub");
assert.equal(el.getAttribute("class"), "foo bar baz");
l.apply();
assert.equal(el.getAttribute("class"), "foo baz nub");
});
});
describe(".copyTo(otherElement)", function () {
it("must create a new class list wrapper with the same classes as the source", function () {
var el = dummy();
list("foo bar baz").remove("bar").add("nub").copyTo(el).apply();
assert.equal(el.getAttribute("class"), "foo baz nub");
});
});
});