-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
/
Copy pathdirectives.js
715 lines (602 loc) · 23.3 KB
/
directives.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
describe('UNIT: Directives', function () {
describe('attr', function () {
var dir = mockDirective('attr')
dir.arg = 'href'
it('should set an attribute', function () {
var url = 'http://a.b.com'
dir.update(url)
assert.strictEqual(dir.el.getAttribute('href'), url)
})
})
describe('text', function () {
var dir = mockDirective('text')
it('should work with a string', function () {
dir.update('hallo')
assert.strictEqual(dir.el.textContent, 'hallo')
})
it('should work with a number', function () {
dir.update(12345)
assert.strictEqual(dir.el.textContent, '12345')
})
it('should work with booleans', function () {
dir.update(true)
assert.strictEqual(dir.el.textContent, 'true')
})
it('should work with objects', function () {
dir.update({foo:"bar"})
assert.strictEqual(dir.el.textContent, '{"foo":"bar"}')
})
it('should be empty with other stuff', function () {
dir.update(null)
assert.strictEqual(dir.el.textContent, '')
dir.update(undefined)
assert.strictEqual(dir.el.textContent, '')
dir.update(function () {})
assert.strictEqual(dir.el.textContent, '')
})
})
describe('html', function () {
var dir = mockDirective('html')
it('should work with a string', function () {
dir.update('hi!!!')
assert.strictEqual(dir.el.innerHTML, 'hi!!!')
dir.update('<span>haha</span><a>lol</a>')
assert.strictEqual(dir.el.querySelector('span').textContent, 'haha')
})
it('should work with a number', function () {
dir.update(12345)
assert.strictEqual(dir.el.innerHTML, '12345')
})
it('should work with booleans', function () {
dir.update(true)
assert.strictEqual(dir.el.textContent, 'true')
})
it('should work with objects', function () {
dir.update({foo:"bar"})
assert.strictEqual(dir.el.textContent, '{"foo":"bar"}')
})
it('should be empty with other stuff', function () {
dir.update(null)
assert.strictEqual(dir.el.innerHTML, '')
dir.update(undefined)
assert.strictEqual(dir.el.innerHTML, '')
dir.update(function () {})
assert.strictEqual(dir.el.innerHTML, '')
})
it('should swap html if el is a comment placeholder', function () {
var dir = mockDirective('html'),
comment = document.createComment('hi'),
parent = dir.el
parent.innerHTML = 'what!'
parent.appendChild(comment)
dir.el = comment
dir.bind()
assert.ok(dir.holder)
assert.ok(dir.nodes)
var pre = 'what!',
after = '<!--hi-->',
h1 = '<span>hello</span><span>world</span>',
h2 = '<a>whats</a><a>up</a>'
dir.update(h1)
assert.strictEqual(parent.innerHTML, pre + h1 + after)
dir.update(h2)
assert.strictEqual(parent.innerHTML, pre + h2 + after)
})
})
describe('show', function () {
var dir = mockDirective('show')
it('should be default value when value is truthy', function () {
dir.update(1)
assert.strictEqual(dir.el.style.display, '')
dir.update('hi!')
assert.strictEqual(dir.el.style.display, '')
dir.update(true)
assert.strictEqual(dir.el.style.display, '')
dir.update({})
assert.strictEqual(dir.el.style.display, '')
dir.update(function () {})
assert.strictEqual(dir.el.style.display, '')
})
it('should be none when value is falsy', function () {
dir.update(0)
assert.strictEqual(dir.el.style.display, 'none')
dir.update('')
assert.strictEqual(dir.el.style.display, 'none')
dir.update(false)
assert.strictEqual(dir.el.style.display, 'none')
dir.update(null)
assert.strictEqual(dir.el.style.display, 'none')
dir.update(undefined)
assert.strictEqual(dir.el.style.display, 'none')
})
})
describe('class', function () {
it('should set class to the value if it has no arg', function () {
var dir = mockDirective('class')
dir.update('test')
assert.ok(dir.el.classList.contains('test'))
dir.update('hoho')
assert.ok(!dir.el.classList.contains('test'))
assert.ok(dir.el.classList.contains('hoho'))
})
it('should add/remove class based on truthy/falsy if it has an arg', function () {
var dir = mockDirective('class')
dir.arg = 'test'
dir.update(true)
assert.ok(dir.el.classList.contains('test'))
dir.update(false)
assert.ok(!dir.el.classList.contains('test'))
})
})
describe('model', function () {
describe('input[checkbox]', function () {
var dir = mockDirective('model', 'input', 'checkbox')
dir.bind()
before(function () {
document.body.appendChild(dir.el)
})
it('should set checked on update()', function () {
dir.update(true)
assert.ok(dir.el.checked)
dir.update(false)
assert.ok(!dir.el.checked)
})
it('should trigger vm.$set when clicked', function () {
var triggered = false
dir.key = 'foo'
dir.vm = { $set: function (key, val) {
assert.strictEqual(key, 'foo')
assert.strictEqual(val, true)
triggered = true
}}
dir.el.dispatchEvent(mockMouseEvent('click'))
assert.ok(triggered)
})
it('should remove event listener with unbind()', function () {
var removed = true
dir.vm.$set = function () {
removed = false
}
dir.unbind()
dir.el.dispatchEvent(mockMouseEvent('click'))
assert.ok(removed)
})
after(function () {
document.body.removeChild(dir.el)
})
})
describe('input[radio]', function () {
var dir1 = mockDirective('model', 'input', 'radio'),
dir2 = mockDirective('model', 'input', 'radio')
dir1.el.name = 'input-radio'
dir2.el.name = 'input-radio'
dir1.el.value = '12345'
dir2.el.value = '54321'
dir1.bind()
dir2.bind()
before(function () {
document.body.appendChild(dir1.el)
document.body.appendChild(dir2.el)
})
it('should set el.checked on update()', function () {
assert.notOk(dir1.el.checked)
dir1.update(12345)
assert.ok(dir1.el.checked)
})
it('should trigger vm.$set when clicked', function () {
var triggered = false
dir2.key = 'radio'
dir2.vm = { $set: function (key, val) {
triggered = true
assert.strictEqual(key, 'radio')
assert.strictEqual(val, dir2.el.value)
}}
dir2.el.dispatchEvent(mockMouseEvent('click'))
assert.ok(triggered)
assert.ok(dir2.el.checked)
assert.notOk(dir1.el.checked)
})
it('should remove listeners on unbind()', function () {
var removed = true
dir1.vm = { $set: function () {
removed = false
}}
dir1.unbind()
dir1.el.dispatchEvent(mockMouseEvent('click'))
assert.ok(removed)
})
after(function () {
document.body.removeChild(dir1.el)
document.body.removeChild(dir2.el)
})
})
describe('select', function () {
var dir = mockDirective('model', 'select'),
o1 = document.createElement('option'),
o2 = document.createElement('option')
o1.value = 0
o2.value = 1
dir.el.appendChild(o1)
dir.el.appendChild(o2)
dir.bind()
before(function () {
document.body.appendChild(dir.el)
})
it('should set value on update()', function () {
dir.update(0)
assert.strictEqual(dir.el.value, '0')
})
it('should trigger vm.$set when value is changed', function () {
var triggered = false
dir.key = 'select'
dir.vm = { $set: function (key, val) {
triggered = true
assert.strictEqual(key, 'select')
assert.equal(val, 1)
}}
dir.el.options.selectedIndex = 1
dir.el.dispatchEvent(mockHTMLEvent('change'))
assert.ok(triggered)
})
it('should remove listener on unbind()', function () {
var removed = true
dir.vm = { $set: function () {
removed = false
}}
dir.unbind()
dir.el.dispatchEvent(mockHTMLEvent('change'))
assert.ok(removed)
})
after(function () {
document.body.removeChild(dir.el)
})
})
describe('input[text] and others', function () {
var dir = mockDirective('model', 'input', 'text')
dir.bind()
before(function () {
document.body.appendChild(dir.el)
})
it('should set the value on update()', function () {
dir.update('foobar')
assert.strictEqual(dir.el.value, 'foobar')
})
// `lazy` option is tested in the API suite
it('should trigger vm.$set when value is changed via input', function () {
var triggered = false
dir.key = 'foo'
dir.vm = { $set: function (key, val) {
assert.ok(dir.lock, 'the directive should be locked if it has no filters')
assert.strictEqual(key, 'foo')
assert.strictEqual(val, 'bar')
triggered = true
}}
dir.el.value = 'bar'
dir.el.dispatchEvent(mockHTMLEvent('input'))
assert.ok(triggered)
})
it('should remove event listener with unbind()', function () {
var removed = true
dir.vm.$set = function () {
removed = false
}
dir.unbind()
dir.el.dispatchEvent(mockHTMLEvent('input'))
assert.ok(removed)
})
it('should not lock during vm.$set if it has filters', function (done) {
var triggered = false
var dir = mockDirective('model', 'input', 'text')
dir.filters = []
dir.bind()
dir.vm = {$set:function () {
assert.notOk(dir.lock)
triggered = true
}}
dir.el.value = 'foo'
document.body.appendChild(dir.el)
dir.el.dispatchEvent(mockHTMLEvent('input'))
// timeout becuase the update is async
setTimeout(function () {
assert.ok(triggered)
document.body.removeChild(dir.el)
done()
}, 1)
})
after(function () {
document.body.removeChild(dir.el)
})
})
})
describe('if', function () {
it('should remain detached if it was detached during bind() and never attached', function () {
var dir = mockDirective('if')
dir.bind()
dir.update(true)
assert.notOk(dir.el.parentNode)
dir.update(false)
assert.notOk(dir.el.parentNode)
})
it('should remove el and insert ref when value is falsy', function () {
var dir = mockDirective('if'),
parent = document.createElement('div')
parent.appendChild(dir.el)
dir.bind()
dir.update(false)
assert.notOk(dir.el.parentNode)
assert.notOk(parent.contains(dir.el))
// phantomJS weird bug:
// Node.contains() returns false when argument is a comment node.
assert.strictEqual(dir.ref.parentNode, parent)
})
it('should append el and remove ref when value is truthy', function () {
var dir = mockDirective('if'),
parent = document.createElement('div')
parent.appendChild(dir.el)
dir.bind()
dir.update(false)
dir.update(true)
assert.strictEqual(dir.el.parentNode, parent)
assert.ok(parent.contains(dir.el))
assert.notOk(parent.contains(dir.ref))
})
it('should work even if it was detached during bind()', function () {
var dir = mockDirective('if')
dir.bind()
var parent = document.createElement('div')
parent.appendChild(dir.el)
dir.update(false)
assert.strictEqual(dir.parent, parent)
assert.notOk(dir.el.parentNode)
assert.notOk(parent.contains(dir.el))
assert.strictEqual(dir.ref.parentNode, parent)
dir.update(true)
assert.strictEqual(dir.el.parentNode, parent)
assert.ok(parent.contains(dir.el))
assert.notOk(parent.contains(dir.ref))
})
})
describe('on (non-delegated only)', function () {
var dir = mockDirective('on')
dir.arg = 'click'
before(function () {
document.body.appendChild(dir.el)
})
it('should set the handler to be triggered by arg through update()', function () {
var triggered = false
dir.update(function () {
triggered = true
})
dir.el.dispatchEvent(mockMouseEvent('click'))
assert.ok(triggered)
})
it('should wrap the handler to supply expected args', function () {
var vm = dir.binding.compiler.vm, // owner VM
e = mockMouseEvent('click'), // original event
triggered = false
dir.update(function (ev) {
assert.strictEqual(this, vm, 'handler should be called on owner VM')
assert.strictEqual(ev, e, 'event should be passed in')
assert.strictEqual(ev.vm, dir.vm)
triggered = true
})
dir.el.dispatchEvent(e)
assert.ok(triggered)
})
it('should remove previous handler when update() a new handler', function () {
var triggered1 = false,
triggered2 = false
dir.update(function () {
triggered1 = true
})
dir.update(function () {
triggered2 = true
})
dir.el.dispatchEvent(mockMouseEvent('click'))
assert.notOk(triggered1)
assert.ok(triggered2)
})
it('should remove the handler in unbind()', function () {
var triggered = false
dir.update(function () {
triggered = true
})
dir.unbind()
dir.el.dispatchEvent(mockMouseEvent('click'))
assert.notOk(triggered)
assert.strictEqual(dir.handler, null, 'should remove reference to handler')
assert.strictEqual(dir.el.vue_viewmodel, null, 'should remove reference to VM on the element')
})
it('should not use delegation if the event is blur or focus', function () {
var dir = mockDirective('on', 'input'),
triggerCount = 0,
handler = function () {
triggerCount++
}
document.body.appendChild(dir.el)
dir.arg = 'focus'
dir.update(handler)
dir.el.dispatchEvent(mockHTMLEvent('focus'))
assert.strictEqual(triggerCount, 1)
dir.arg = 'blur'
dir.update(handler)
dir.el.dispatchEvent(mockHTMLEvent('blur'))
assert.strictEqual(triggerCount, 2)
document.body.removeChild(dir.el)
})
after(function () {
document.body.removeChild(dir.el)
})
})
describe('pre', function () {
it('should skip compilation', function () {
var testId = 'pre-test'
mock(testId, '<span v-pre><strong>{{lol}}</strong><a v-text="hi"></a></span>')
var t = new Vue({
el: '#' + testId,
data: {
lol: 'heyhey',
hi: 'hohoho'
}
})
assert.strictEqual(t.$el.querySelector('strong').textContent, '{{lol}}')
assert.strictEqual(t.$el.querySelector('a').textContent, '')
assert.ok(t.$el.querySelector('a').hasAttribute('v-text'))
})
})
describe('component', function () {
it('should create a child viewmodel with given constructor', function () {
var testId = 'component-test'
mock(testId, '<div v-component="' + testId + '"></div>')
var t = new Vue({
el: '#' + testId,
data: {
msg: '123'
},
components: {
'component-test': {
template: '<span>{{msg}}</span>'
}
}
})
assert.strictEqual(t.$el.querySelector('span').textContent, '123')
})
})
describe('with', function () {
it('should create a child viewmodel with given data', function () {
var testId = 'with-test'
mock(testId, '<span v-with="test">{{msg}}</span>')
var t = new Vue({
el: '#' + testId,
data: {
test: {
msg: testId
}
}
})
assert.strictEqual(t.$el.querySelector('span').textContent, testId)
})
})
describe('component-id', function () {
it('should register a VM isntance on its parent\'s $', function () {
var called = false
var Child = Vue.extend({
methods: {
test: function () {
called = true
}
}
})
var t = new Vue({
template: '<div v-component="child" v-component-id="hihi"></div>',
components: {
child: Child
}
})
assert.ok(t.$.hihi instanceof Child)
t.$.hihi.test()
assert.ok(called)
t.$.hihi.$destroy()
assert.notOk('hihi' in t.$)
})
})
// More detailed testing for v-repeat can be found in functional tests.
// this is mainly for code coverage
describe('repeat', function () {
var nextTick = require('vue/src/utils').nextTick,
VM = require('vue/src/viewmodel')
it('should work', function (done) {
var handlerCalled = false
var v = new Vue({
template: '<span v-repeat="items" v-on="click:check">{{title}}</span>',
data: {
items: [
{title: 1},
{title: 2}
]
},
methods: {
check: function (e) {
assert.ok(e.targetVM instanceof VM)
assert.strictEqual(this, v)
handlerCalled = true
}
}
})
nextTick(function () {
assert.equal(v.$el.innerHTML, '<span>1</span><span>2</span><!--v-repeat-items-->')
v.items.push({title:3})
v.items.pop()
v.items.unshift({title:0})
v.items.shift()
v.items.splice(0, 1, {title:-1})
v.items.sort(function (a, b) {
return a.title > b.title
})
v.items.reverse()
nextTick(function () {
assert.equal(v.$el.innerHTML, '<span>2</span><span>-1</span><!--v-repeat-items-->')
testHandler()
})
})
function testHandler () {
document.getElementById('test').appendChild(v.$el)
var span = v.$el.querySelector('span'),
e = mockMouseEvent('click')
span.dispatchEvent(e)
nextTick(function () {
assert.ok(handlerCalled)
done()
})
}
})
})
describe('style', function () {
it('should apply a normal style', function () {
var d = mockDirective('style')
d.arg = 'text-align'
d.bind()
assert.strictEqual(d.prop, 'textAlign')
d.update('center')
assert.strictEqual(d.el.style.textAlign, 'center')
})
it('should apply prefixed style', function () {
var d = mockDirective('style')
d.arg = '-webkit-transform'
d.bind()
assert.strictEqual(d.prop, 'webkitTransform')
d.update('scale(2)')
assert.strictEqual(d.el.style.webkitTransform, 'scale(2)')
})
it('should auto prefix styles', function () {
var d = mockDirective('style')
d.arg = '$transform'
d.bind()
assert.ok(d.prefixed)
assert.strictEqual(d.prop, 'transform')
var val = 'scale(2)'
d.update(val)
assert.strictEqual(d.el.style.transform, val)
assert.strictEqual(d.el.style.webkitTransform, val)
assert.strictEqual(d.el.style.mozTransform, val)
assert.strictEqual(d.el.style.msTransform, val)
})
})
})
function mockDirective (dirName, tag, type) {
var dir = Vue.directive(dirName),
ret = {
binding: { compiler: { vm: {} } },
compiler: { vm: {}, options: {}, execHook: function () {}},
el: document.createElement(tag || 'div')
}
if (typeof dir === 'function') {
ret.update = dir
} else {
for (var key in dir) {
ret[key] = dir[key]
}
}
if (tag === 'input') ret.el.type = type || 'text'
return ret
}