-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.js
294 lines (229 loc) · 8.26 KB
/
test.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
var test = require('tape');
var dom = require("./");
var randomColor = require('random-color');
var HTML = (function(){/*
<textarea style="width:300px; height:200px"></textarea>
<ul data-foo="bar" class="fruits">
<li class="fruit">apple</li>
<li class="fruit">orange</li>
<li class="fruit">plum</li>
</ul>
*/}).toString().slice(17, -4);
reset();
test('returns an attr value', function (t) {
t.plan(1);
t.equal(dom('.fruits').attr('data-foo'), 'bar');
});
test('sets an attr value', function (t) {
t.plan(3);
dom('.fruit').attr('data-foo', 'bar').style('background-color', randomColor());
fruits().forEach(function(el){
t.equal(el.getAttribute('data-foo'), 'bar');
});
});
test('selects elements by css queries', function (t) {
t.plan(2);
var els = dom('.fruits .fruit');
var clone = document.querySelectorAll('.fruits .fruit');
t.equal(els[0], clone[0]);
t.equal(els[1], clone[1]);
});
test('hides els', function (t) {
t.plan(3);
dom('.fruit').hide();
fruits().forEach(function(el){
t.equal(el.style.display, 'none');
});
});
test('shows els', function (t) {
t.plan(3);
fruits().forEach(function(el){
el.style.display = 'none';
});
dom('.fruit').show();
fruits().forEach(function(el){
t.equal(el.style.display, '');
});
});
test('adds a class', function (t) {
t.plan(3);
dom('.fruit').addClass('foo');
fruits().forEach(function(el){
t.ok(el.classList.contains('foo'));
});
});
test('removes a class', function (t) {
t.plan(6);
dom('.fruit').addClass('foo').addClass('bar').removeClass('foo');
fruits().forEach(function(el){
t.ok(el.classList.contains('bar'));
t.notOk(el.classList.contains('foo'));
});
});
test('checks an element if it has a class', function (t) {
t.plan(9);
dom('.fruit').removeClass('foo').removeClass('bar');
fruits().forEach(function(el){
t.notOk(el.classList.contains('foo'));
t.notOk(el.classList.contains('bar'));
t.ok(el.classList.contains('fruit'));
});
});
test('toggles a class', function (t) {
t.plan(6);
dom('.fruit').addClass('foo').removeClass('bar').toggleClass('foo').toggleClass('bar');
fruits().forEach(function(el){
t.notOk(el.classList.contains('foo'));
t.ok(el.classList.contains('bar'));
});
});
test('styles an element', function (t) {
t.plan(9);
dom('.fruit').style({ color: 'red', 'background-color': 'yellow' }).style('border', '1px solid green');
fruits().forEach(function(el){
t.equal(el.style.color, 'red');
t.equal(el.style.backgroundColor, 'yellow');
t.equal(el.style.border, '1px solid green');
});
});
test('adds an event', function (t) {
dom('.fruit').click(function(event){
dom(event.target).style({ 'background-color': randomColor(), 'color': randomColor() });
});
dom('textarea').keydown(function(){
dom('textarea').style('background-color', randomColor());
});
t.end();
});
test('returns the value of an element', function (t) {
t.plan(1);
dom('textarea')[0].value = 'hello';
t.equal(dom('textarea').val(), 'hello');
});
test('sets the value of an element', function (t) {
t.plan(1);
dom('textarea').val('foobar').style('background-color', randomColor());
t.equal(dom('textarea').val(), 'foobar');
});
test('returns the text content of an element', function (t) {
t.plan(1);
dom('.fruit:first-child')[0].innerText = 'grape';
t.equal(dom('.fruit:first-child').text(), 'grape');
});
test('sets the text content of an element', function (t) {
t.plan(2);
dom('.fruit:first-child').text('a delicious {fruit}', { fruit: 'cherry' }).style('background-color', randomColor());
t.equal(dom('.fruit:first-child').text(), 'a delicious cherry');
dom('.fruit:first-child').text('tasty cherries').style('background-color', randomColor());
t.equal(dom('.fruit:first-child').text(), 'tasty cherries');
});
test('returns the html content of an element', function (t) {
t.plan(1);
dom('.fruit:first-child')[0].innerHTML = 'kiwi';
t.equal(dom('.fruit:first-child').html(), 'kiwi');
});
test('sets the html content of an element', function (t) {
t.plan(2);
dom('.fruit:first-child').html('a delicious {fruit}', { fruit: 'melon' }).style('background-color', randomColor());
t.equal(dom('.fruit:first-child').html(), 'a delicious melon');
dom('.fruit:first-child').html('a tasty melon').style('background-color', randomColor());
t.equal(dom('.fruit:first-child').html(), 'a tasty melon');
});
test('creates a new element', function (t) {
t.plan(2);
var parent = dom.create('div').addClass('parent');
var child1 = dom.create('<p>child 1</p>');
var child2 = dom.create('span').html('child 2');
var child3 = dom.create('<label>child 3</label>');
var child4 = dom.create('strong').html('child 4');
parent.add(child1).add(child2).replace(child1, child3).add(child4).remove('span');
t.equal(parent.html(), '<label>child 3</label><strong>child 4</strong>');
t.ok(parent.hasClass('parent'));
});
test('adds a child element', function (t) {
t.plan(1);
var child = dom(document.createElement('li')).addClass('fruit').addClass('new').html('yo');
dom('.fruits').add(child);
t.equal(dom('.fruit:last-child')[0], child[0]);
});
test('adds HTML', function (t) {
t.plan(6);
dom('.fruits').add('<li class="new fruit">a fresh {fruit}</li>', { fruit: 'watermelon' });
t.ok(dom('.fruit:last-child').hasClass('new'));
t.ok(dom('.fruit:last-child').hasClass('fruit'));
t.equal(dom('.fruit:last-child').text(), 'a fresh watermelon');
dom('.fruits').add('<li class="new fruit">a very fresh apple</li>');
t.ok(dom('.fruit:last-child').hasClass('new'));
t.ok(dom('.fruit:last-child').hasClass('fruit'));
t.equal(dom('.fruit:last-child').text(), 'a very fresh apple');
});
test('creates and inserts HTML', function (t) {
t.plan(4);
dom('<li class="new very-new fruit">very fresh {fruit}</li>', { fruit: 'peach' }).insert('.fruits');
t.ok(dom('.fruit:last-child').hasClass('new'));
t.ok(dom('.fruit:last-child').hasClass('very-new'));
t.ok(dom('.fruit:last-child').hasClass('fruit'));
t.equal(dom('.fruit:last-child').text(), 'very fresh peach');
});
test('removes itself', function (t) {
t.plan(2);
var last = dom('.fruit:last-child').text();
dom('<li class="fruit">to be removed</li>').insert('.fruits');
t.equal(dom('.fruit:last-child').text(), 'to be removed');
dom('.fruit:last-child').remove();
t.equal(dom('.fruit:last-child').text(), last);
});
test('selects the children', function (t) {
t.plan(1);
var newf = dom('.fruits').select('.new');
var clone = dom('.fruits .new');
t.equal(newf.length, clone.length);
});
test('selects children of multiple elements', function (t) {
var all = dom('body > *');
var children = all.select('*');
t.plan(1);
t.equal(children.length, dom('ul li').length);
});
test('initializes a chain with given elements', function (t) {
t.plan(7);
dom.apply(undefined, Array.prototype.slice.call(document.querySelectorAll('.fruit'))).removeClass('corge').addClass('corge');
fruits().forEach(function(el){
t.ok(el.classList.contains('corge'));
});
});
test('event delegation', function (t) {
t.plan(1);
dom('.fruits').on('click', 'li', function () {
t.ok(true);
});
dom('.fruits').add('<li id="delegation-test-el">yo</li>');
dom('#delegation-test-el')[0].click();
});
test('finding parents', function (t) {
t.plan(4);
t.equal(dom('li').parent().length, 1);
t.equal(dom('li').parent()[0], dom('ul.fruits')[0]);
t.equal(dom('li').parent('body')[0], dom('body')[0]);
t.equal(dom('li').parent('#foo').length, 0);
});
test('selecting siblings', function (t) {
t.plan(3);
dom('<li class="delicious fruit cherry">cherry</li>').insert('ul.fruits');
dom('<li class="delicious fruit watermelon">watermelon</li>').insert('ul.fruits');
dom('<li class="delicious fruit grapes">grapes!</li>').insert('ul.fruits');
dom('<li class="also-delicious fruit">carrot</li>').insert('ul.fruits');
var assert = ['cherry', 'watermelon', 'grapes!'];
dom('.also-delicious').siblings('.delicious.fruit').forEach(function (el, ind) {
t.equal(assert[ind], el.textContent);
});
});
function fruits () {
return Array.prototype.slice.call(document.querySelectorAll('.fruits .fruit'));
}
function reset (done){
document.body.innerHTML = HTML;
dom('textarea').onKey('alt space', function () {
dom('textarea').val('');
});
};