-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.js
582 lines (485 loc) · 12.5 KB
/
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
describe("Morbid puts M() in global namespace and does following: ", function() {
beforeEach(() => {
M.wipe();
$('body').append('<div id=MorbidBase/>');
$('#MorbidBase').html(
`
<div id="host">
<span id=app class="child1 childall"/>
<span id=manapp class="child2 childall"/>
</div>
`
).hide();
M.rein(document.getElementById('MorbidBase'));
});
afterEach(() => {
$('#MorbidBase').remove();
});
it("Morbid exists in a global namespace under letter M", () => {
expect(M).to.be.ok();
});
it("It's api is rather small and contains as little as four static methods, including M itself"
, () => {
expect(M.rein).to.be.ok();
expect(M.bulk).to.be.ok();
expect(M.wipe).to.be.ok();
expect(Object.getOwnPropertyNames(M).length == 8).to.be.ok(); // that is our 3 plus usual ones
//["length", "name", "arguments", "caller", "prototype", "bulk", "wipe", "rein"]
});
it(
"It acts as a wrapper over good old jQuery, but you won't regret reading further. I promise you that."
, () => {
expect(M('span').length).to.be.equal(2);
expect(M('span').closest('#host')).to.be.ok();
});
it("It manages pre-existing DOM", () => {
expect(M('#host')[0].children.length).to.equal(2);
});
it(
"applies behaviours to pre-existing DOM using JQuery selectors. We call pair (selector, {eventOrMethodName: function}) a lute."
, () => {
expect(M.bulk).to.be.ok();
M.bulk({'.child1': {
test: () => {
return true;
}
}});
});
it("You can add lutes one by one or with bulk method.", () => {
var result = 0;
M.bulk({
'.child1': {
click: (event) => {
result = 1;
}
}
, '#host .child1': {
click: (event) => {
result = 2;
}
}
});
$('.child1').trigger('click');
expect(result).to.be.equal(2);
});
it(
"Applied methods can be invoked like this: M(selector).methodName() . M(selector) is, um, polymorphic decorator"
, () => {
M.bulk({'.child1': {
test: () => {
return true;
}
}});
var r = M('.child1').test();
expect(r).to.equal(true);
});
it(
"You can call method simply (get any single return with collection) or, using .W property. If W is used, you get a full execution report"
, () => {
M.bulk({'#app': {
sound: () => {
return 'paramparamparam';
}
}, '.child2': {
sound: () => {
return true;
}
}});
expect(M('#app').sound() == 'paramparamparam').to.be.ok();
var o = M('span').W.sound();
expect(o.length).to.equal(2);
});
it(
".W full report looks like array [{elementReference, returnValue}, ..] .W stands for nothing, it's just M upwards down."
, () => {
M.bulk({'#app': {
sound: () => {
return 'paramparamparam';
}
}, '.child2': {
sound: () => {
return true;
}
}});
var o = M('span').W.sound();
expect(o[0].returnValue).to.equal('paramparamparam');
expect(o[1].returnValue).to.equal(true);
});
it(
"M(selector), just as $(selector), may operate over a set of DOM elements. It has every method found in every DOM item of collection "
, () => {
M.bulk({'#app': {
sound: () => {
return 'paramparamparam';
}
, mute: () => {
return 'mute';
}
},
'.child1' : {
sound: () => {
return true;
}
},
'#app.child1': {
sound: () => {
return "app";
}
}});
var s = M('#app').W.sound();
var m = M('#app').W.mute();
expect(s[0].returnValue).to.equal('app');
expect(m[0].returnValue).to.equal('mute');
});
it(
"M(selector).nonExistingMethod() does not results in exception. It's perfectly fine."
, () => {
M.bulk({'.child1': {
test: () => {
return true;
}
}});
var fine = undefined;
try {
M('.child1').explode();
fine = true;
} catch (e) {
fine = false;
}
expect(fine).to.equal(true);
});
it("lute allows to add rule with comma-separated selectors", () => {
var result = 0;
M.bulk({'.child1, #host .child2': {
click: (event) => {
result++;
}
}});
$('.child1').trigger('click');
$('.child2').trigger('click');
expect(result).to.be.equal(2);
});
it("Morbid effectively merges two consequent lutes with same selectors ", () => {
var clickWork = false;
M.bulk({
'#app': {
sound: (event) => {
return 2;
},
'click': (event) => {
clickWork = true;
}
}});
M('#app').trigger('click');
expect(M('#app').sound() == 2).to.be.ok();
expect(clickWork).to.be.ok();
});
it(
"Morbid overrides first added method if same methods are consequently added to system. Methods are not summed up, the latter overwrites the former"
, () => {
var clickWork = false;
var firstNotCalled = true;
M.bulk({'#app': {
sound: (event) => {
return 2;
firstNotCalled = false;
}
}, '#app': {
sound: (event) => {
return 3;
}
}});
expect(firstNotCalled).to.be.ok();
expect(M('#app').sound() == 3).to.be.ok();
});
it(
"Morbid overrides first added method if selectors are different but specificty is same, part 1 "
, () => {
var clickWork = false;
M.bulk({'.child1': {
sound: (event) => {
return 2;
}
},'.childall': {
sound: (event) => {
return 3;
}
}});
expect(M('#app').sound() == 3).to.be.ok();
});
it(
"Morbid overrides first added method if selectors are different but specificty is same, part 2 "
, () => {
var clickWork = false;
M.bulk({'.childall': {
sound: (event) => {
return 3;
}
}, '.child1': {
sound: (event) => {
return 2;
}
}});
expect(M('#app').sound() == 2).to.be.ok();
});
});
describe("Handling events with Morbid ", () => {
beforeEach(() => {
M.wipe();
$('body').append('<div id=MorbidBase/>');
$('#MorbidBase').html(
`
<div id="host">
<span id=app class="child1 childall"/>
<span id=manapp class="child2 childall"/>
</div>
`
).hide();
M.rein(document.getElementById('MorbidBase'));
});
afterEach(() => {
$('#MorbidBase').remove();
});
it("Event listener can be added with Morbid as well as method.. ", () => {
var itHasChanged = false;
M.bulk({'.child1': {
click: () => {
itHasChanged = true;
return true;
}
}});
$('.child1').trigger('click');
expect(itHasChanged).to.be.ok();
});
it("..and for event as well.", () => {
M.bulk({'.child1': {
click: (event) => {
expect(event.type).to.be.equal('click');
}
}});
$('.child1').trigger('click');
});
it(
"an event listener described in lute with higher specificity, overrides the one lower. They are not added up. e.stopImmediatePropagation in Morbid is same as e.stopPropagation()."
, () => {
var result = 0;
M.bulk({'.child1' : {
click: (event) => {
result = 1;
}
}, '#host .child1': {
click: (event) => {
result = 2;
}
}});
$('.child1').trigger('click');
expect(result).to.be.equal(2);
});
it("In rule you can specify multiple events as space-separated string", () => {
var result = 0;
M.bulk({
'.child1': {
'click keyup': (event) => {
result++;
}
}
});
$('.child1').trigger('click').trigger('keyup');
expect(result).to.be.equal(2);
});
it(
"if there is an event names conflict, Morbid will be unable to decide which one of listeners takes precedence, and will throw an exception. Good thing is that you get that one instantly after M.lute/M.bulk."
, () => {
var a1 = false
, a2 = false;
try {
M.bulk({'.child1': {
'click': (event) => {}
, 'click onkeyup': (event) => {}
}});
} catch (e) {
var res = e;
}
expect(res).to.be.ok();
});
});
describe("Dom traversal and manipulation ", () => {
beforeEach(() => {
M.wipe();
$('body').append('<div id=MorbidBase/>');
$('#MorbidBase').html(
`
<div id="host">
<span id=app class="child1 childall"/>
<span id=manapp class="child2 childall"/>
</div>
`
).hide();
M.rein(document.getElementById('MorbidBase'));
});
afterEach(() => {
$('#MorbidBase').remove();
});
it("You can use Morbid as JQuery wrapper. It exposes all of JQuery methods"
, () => {
var clickWork = false;
M.bulk({'#MorbidBase': {
fun: (event) => {
return 'fun';
}
}});
expect(M('#app').closest).to.be.ok();
expect(M('#app').closest('#MorbidBase')).to.be.ok();
expect(M('#app').closest('#MorbidBase').fun() == "fun").to.be.ok();
});
it(
"if JQuery method is dom manipulation or traverse method, it is wrapped in Morbid too. Chain does not breaks up until simple value is returned, it is always JQuery and then M while you are calling traverse methods."
, () => {
var clickWork = false;
M.bulk({'.childall': {
fun: (event) => {
return 'fun';
}
}});
expect(M('#app').closest).to.be.ok();
var a = M('#app').closest('#MorbidBase').find('span').fun();
expect(M('#app').closest('#MorbidBase').find('span').length).to.be.ok();
expect(a).to.be.ok();
});
it("Morbid correctly exposes jquery val()", () => {
M.wipe();
$('body').append('<div id=MorbidBase/>');
$('#MorbidBase').html(
`
<div id="host">
<input id=app class="child1 childall" value="pro100"/>
<input id=app2 class="child1 childall" value="pro100"/>
</div>
`
).hide();
M.rein(document.getElementById('MorbidBase'));
expect(M('#app').val() == "pro100").to.be.ok();
});
});
describe("This object calculation", function() {
beforeEach(() => {
M.wipe();
$('body').append('<div id=MorbidBase/>');
$('#MorbidBase').html(
`
<div id="host">
<input id=app class="child1 childall" value="pro100"/>
<input id=app2 class="child1 childall" value="pro100"/>
</div>
`
).hide();
M.rein(document.getElementById('MorbidBase'));
});
afterEach(() => {
$('#MorbidBase').remove();
});
it(
"Morbid binds this of method to M(specific instance) for Morbid method invocation "
, () => {
M.bulk({'#app': {
'fun': function() {
return this.fun ? true : false;
}
}});
expect(M('#app').fun()).to.be.ok();
});
it("for multiple method invocation through W as well as for usual ones", () => {
M.bulk({'#host input': {
'fun': function() {
return this.fun ? true : false;
}
}});
var allTrue = _.every(M('input').W.fun(), (i) => i.returnValue)
expect(allTrue).to.be.ok();
});
it(
"This object contains all JQuery methods. In fact, those of JQuery are returned instead of yours in case of name conflict. "
, () => {
M.bulk({'#app': {
'fun': function() {
return this.find ? true : false;
}
}});
expect(M('#app').fun()).to.be.ok();
});
it(
"JQuery.closest() is also exposed through this, and it especially nice to use "
, () => {
M.bulk({'#app': {
'fun': function() {
return this.closest ? true : false;
}
}});
expect(M('#app').fun()).to.be.ok();
});
it(
"All above works the same way if you pass function reference as method "
, () => {
function required() {
return this['validate'] ? true : false;
}
M.bulk({'#host': {
'validate': required
}});
expect(M('#host').validate()).to.be.ok();
});
it("For events, too ", () => {
var has = false;
function l() {
has = this['find'] ? true : false;
}
M.bulk({'#host': {
click: l
}});
M('#host').trigger('click');
expect(has).to.be.ok();
});
it("for space-separated event lists in strings - too", () => {
var count = 0;
function l() {
count += this['find'] ? true : false;
}
M.bulk({'input': {
'click onkeyup': l
}});
M('input').trigger('click');
expect(count == 2).to.be.ok();
});
});
describe("Upcoming tasks. These tests are supposed to fail", function() {
beforeEach(() => {
M.wipe();
$('body').append('<div id=MorbidBase/>');
$('#MorbidBase').html(
`
<div id="host">
<input id=app class="child1 childall" value="pro100"/>
<input id=app2 class="child1 childall" value="pro100"/>
</div>
`
).hide();
M.rein(document.getElementById('MorbidBase'));
});
afterEach(() => {
$('#MorbidBase').remove();
});
it(
"this object stays fresh all method long. If you change current dom element class in the middle, new methods become available, olds become unavailable"
, () => {
expect(false).to.be.ok();
});
it(
"full execution report contains useful methods like &&all or lodash chain applied"
, () => {
expect(false).to.be.ok();
});
it("there is absolutely no real problem Morbid solves described in this document. Everybody hates frameworks so we have to provide and advertise something truly unique to justifty our existence"
, () => {
expect(false).to.be.ok();
});
});