Skip to content

Commit 7fc537a

Browse files
committed
fix functional tests for batcher
1 parent c7b2d9c commit 7fc537a

17 files changed

+203
-130
lines changed

examples/todomvc/js/benchmark.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ function runBenchmark () {
4343
function toggle () {
4444
addTime = now() - last
4545
var checkboxes = document.querySelectorAll('.toggle')
46-
for (var i = 0; i < checkboxes.length; i++) {
47-
checkboxes[i].click()
48-
}
46+
//for (var j = 0; j < 5; j++) {
47+
for (var i = 0; i < checkboxes.length; i++) {
48+
checkboxes[i].click()
49+
}
50+
//}
4951
last = now()
5052
setTimeout(remove, 0)
5153
}

src/batcher.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ exports.queue = function (binding, method) {
2424

2525
function flush () {
2626
for (var i = 0; i < queue.length; i++) {
27-
var task = queue[i]
28-
task.binding['_' + task.method]()
29-
has[task.binding.id] = false
27+
var task = queue[i],
28+
b = task.binding
29+
if (b.unbound) continue
30+
b['_' + task.method]()
31+
has[b.id] = false
3032
}
3133
reset()
3234
}

src/binding.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function Binding (compiler, key, isExp, isFn) {
1919
this.instances = []
2020
this.subs = []
2121
this.deps = []
22+
this.unbound = false
2223
}
2324

2425
var BindingProto = Binding.prototype
@@ -70,6 +71,11 @@ BindingProto.pub = function () {
7071
* Unbind the binding, remove itself from all of its dependencies
7172
*/
7273
BindingProto.unbind = function () {
74+
// Indicate this has been unbound.
75+
// It's possible this binding will be in
76+
// the batcher's flush queue when its owner
77+
// compiler has already been destroyed.
78+
this.unbound = true
7379
var i = this.instances.length
7480
while (i--) {
7581
this.instances[i].unbind()

src/directives/model.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ module.exports = {
4040
try {
4141
cursorPos = el.selectionStart
4242
} catch (e) {}
43-
// `input` event has weird updating issue with
44-
// International (e.g. Chinese) input methods,
45-
// have to use a Timeout to hack around it...
46-
setTimeout(function () {
47-
self.vm.$set(self.key, el[attr])
43+
self.vm.$set(self.key, el[attr])
44+
// since updates are async
45+
// we need to reset cursor position async too
46+
utils.nextTick(function () {
4847
if (cursorPos !== undefined) {
4948
el.setSelectionRange(cursorPos, cursorPos)
5049
}
51-
}, 0)
50+
})
5251
}
5352
: function () {
5453
// no filters, don't let it trigger update()
@@ -63,9 +62,9 @@ module.exports = {
6362
if (isIE9) {
6463
self.onCut = function () {
6564
// cut event fires before the value actually changes
66-
setTimeout(function () {
65+
utils.nextTick(function () {
6766
self.set()
68-
}, 0)
67+
})
6968
}
7069
self.onDel = function (e) {
7170
if (e.keyCode === 46 || e.keyCode === 8) {

test/functional/specs/extend.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
casper.test.begin('Encapsulation & Inheritance', 8, function (test) {
22

33
casper
4-
.start('./fixtures/extend.html', function () {
5-
4+
.start('./fixtures/extend.html')
5+
.then(function () {
66
test.assertSelectorHasText('.dir', 'directive works')
77
test.assertSelectorHasText('.filter', 'filter works')
88
test.assertSelectorHasText('.partial', 'partial works')
99
test.assertSelectorHasText('.vm', 'component works')
1010
test.assertSelectorHasText('.vm-w-model', 'component with model works')
11-
1211
test.assertSelectorHasText('#log', 'T created T ready T created C created T ready C ready', 'hook inheritance works')
1312
test.assertSelectorHasText('.cvm', 'component works', 'Child should have access to Parent options')
14-
15-
this.evaluate(function () {
16-
test.vmData = {
17-
selfMsg: 'replacing $data ',
18-
msg: 'also works'
19-
}
20-
})
21-
13+
})
14+
.thenEvaluate(function () {
15+
test.vmData = {
16+
selfMsg: 'replacing $data ',
17+
msg: 'also works'
18+
}
19+
})
20+
.then(function () {
2221
test.assertSelectorHasText('.vm-w-model', 'replacing $data also works')
2322
})
2423
.run(function () {

test/functional/specs/forms.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
casper.test.begin('Forms', 10, function (test) {
22

33
casper
4-
.start('./fixtures/forms.html', function () {
5-
4+
.start('./fixtures/forms.html')
5+
.then(function () {
66
// test initial value binding
77
test.assertField('text', 'some text')
88
test.assertField('checkbox', true)
99
test.assertField('radio', 'b')
1010
test.assertField('select', 'b')
1111
test.assertField('textarea', 'more text')
12-
12+
})
13+
.then(function () {
1314
this.fill('#forms', {
1415
'text': 'changed text',
1516
'checkbox': false,
1617
'radio': 'a',
1718
'select': 'a',
1819
'textarea': 'more changed text'
1920
})
20-
21+
})
22+
.then(function () {
2123
test.assertSelectorHasText('.text', 'changed text')
2224
test.assertSelectorHasText('.checkbox', 'false')
2325
test.assertSelectorHasText('.radio', 'a')

test/functional/specs/nested-props.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,54 @@
11
casper.test.begin('Nested Properties', 20, function (test) {
22

33
casper
4-
.start('./fixtures/nested-props.html', function () {
5-
4+
.start('./fixtures/nested-props.html')
5+
.then(function () {
66
test.assertSelectorHasText('h1 span', '')
77
test.assertSelectorHasText('h2 span', '555')
88
test.assertSelectorHasText('h3 span', 'Yoyoyo555')
9-
10-
this.click('.one')
9+
})
10+
.thenClick('.one', function () {
1111
test.assertSelectorHasText('h1 span', 'one')
1212
test.assertSelectorHasText('h2 span', '1')
1313
test.assertSelectorHasText('h3 span', 'Yoyoyoone1')
14-
15-
this.click('.two')
14+
})
15+
.thenClick('.two', function () {
1616
test.assertSelectorHasText('h1 span', 'two')
1717
test.assertSelectorHasText('h2 span', '2')
1818
test.assertSelectorHasText('h3 span', 'Yoyoyotwo2')
19-
20-
this.click('.three')
19+
})
20+
.thenClick('.three', function () {
2121
test.assertSelectorHasText('h1 span', 'three')
2222
test.assertSelectorHasText('h2 span', '3')
2323
test.assertSelectorHasText('h3 span', 'Yoyoyothree3')
24-
24+
})
25+
.then(function () {
2526
this.fill('#form', {
2627
msg: 'Oh yeah '
2728
})
29+
})
30+
.then(function () {
2831
test.assertSelectorHasText('h3 span', 'Oh yeah three3')
29-
30-
// hidden data variables
31-
// i.e. nested under an object, not explicitly
32-
// bound in the template, but is depended upon
33-
// by an expression or a computed property
32+
})
33+
// hidden data variables
34+
// i.e. nested under an object, not explicitly
35+
// bound in the template, but is depended upon
36+
// by an expression or a computed property
37+
.then(function () {
3438
test.assertSelectorHasText('.hidden', '3')
35-
this.click('.four')
39+
})
40+
.thenClick('.four', function () {
3641
test.assertSelectorHasText('.hidden', '4')
37-
38-
// set a nested object to {}
39-
this.click('.empty1')
42+
})
43+
// set a nested object to {}
44+
.thenClick('.empty1', function () {
4045
test.assertSelectorHasText('h1 span', '')
4146
test.assertSelectorHasText('h3 span', 'Oh yeah 3')
42-
43-
this.click('.empty2')
47+
})
48+
.thenClick('.empty2', function () {
4449
test.assertSelectorHasText('h1 span', '')
4550
test.assertSelectorHasText('h2 span', '')
4651
test.assertSelectorHasText('h3 span', 'Oh yeah ')
47-
4852
})
4953
.run(function () {
5054
test.done()

test/functional/specs/nested-repeat.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
casper.test.begin('Nested Repeat', 12, function (test) {
22

33
casper
4-
.start('./fixtures/nested-repeat.html', function () {
5-
4+
.start('./fixtures/nested-repeat.html')
5+
.then(function () {
66
var i, j
7-
87
for (i = 0; i < 2; i++) {
98
for (j = 0; j < 2; j++) {
109
test.assertSelectorHasText(
@@ -13,10 +12,13 @@ casper.test.begin('Nested Repeat', 12, function (test) {
1312
)
1413
}
1514
}
16-
15+
})
16+
.then(function () {
1717
this.click('#b0')
1818
this.click('#b1')
19-
19+
})
20+
.then(function () {
21+
var i, j
2022
for (i = 0; i < 2; i++) {
2123
for (j = 0; j < 2; j++) {
2224
test.assertSelectorHasText(
@@ -25,17 +27,25 @@ casper.test.begin('Nested Repeat', 12, function (test) {
2527
)
2628
}
2729
}
28-
30+
})
31+
.then(function () {
32+
var i, j
2933
for (i = 0; i < 2; i++) {
3034
for (j = 0; j < 2; j++) {
3135
this.click('#b' + i + '-' + j)
36+
}
37+
}
38+
})
39+
.then(function () {
40+
var i, j
41+
for (i = 0; i < 2; i++) {
42+
for (j = 0; j < 2; j++) {
3243
test.assertSelectorHasText(
3344
'.list-' + i + ' .list-' + j,
3445
i + '.' + j + ' : hi<-hi'
3546
)
3647
}
3748
}
38-
3949
})
4050
.run(function () {
4151
test.done()

test/functional/specs/nested-vms.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
casper.test.begin('Nested Viewmodels', 7, function (test) {
22

33
casper
4-
.start('./fixtures/nested-vms.html', function () {
4+
.start('./fixtures/nested-vms.html')
5+
.then(function () {
56

67
test.assertSelectorHasText('.ancestor', 'Andy Johnson')
78
test.assertSelectorHasText('.jack', 'Jack, son of Andy')

test/functional/specs/repeated-items.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,69 @@
11
casper.test.begin('Repeated Items', 41, function (test) {
22

33
casper
4-
.start('./fixtures/repeated-items.html', function () {
4+
.start('./fixtures/repeated-items.html')
5+
.then(function () {
56

67
// initial values
78
test.assertSelectorHasText('.count', '3')
89
test.assertSelectorHasText('.item:nth-child(1)', '0 A')
910
test.assertSelectorHasText('.item:nth-child(2)', '1 B')
1011
test.assertSelectorHasText('.item:nth-child(3)', '2 C')
1112

12-
this.click('.push')
13+
})
14+
.thenClick('.push', function () {
1315
test.assertSelectorHasText('.count', '4')
1416
test.assertSelectorHasText('.item:nth-child(4)', '3 0')
15-
16-
this.click('.shift')
17+
})
18+
.thenClick('.shift', function () {
1719
test.assertSelectorHasText('.count', '3')
1820
test.assertSelectorHasText('.item:nth-child(1)', '0 B')
1921
test.assertSelectorHasText('.item:nth-child(2)', '1 C')
2022
test.assertSelectorHasText('.item:nth-child(3)', '2 0')
21-
22-
this.click('.pop')
23+
})
24+
.thenClick('.pop', function () {
2325
test.assertSelectorHasText('.count', '2')
2426
test.assertSelectorHasText('.item:nth-child(1)', '0 B')
2527
test.assertSelectorHasText('.item:nth-child(2)', '1 C')
26-
27-
this.click('.unshift')
28+
})
29+
.thenClick('.unshift', function () {
2830
test.assertSelectorHasText('.count', '3')
2931
test.assertSelectorHasText('.item:nth-child(1)', '0 1')
3032
test.assertSelectorHasText('.item:nth-child(2)', '1 B')
3133
test.assertSelectorHasText('.item:nth-child(3)', '2 C')
32-
33-
this.click('.splice')
34+
})
35+
.thenClick('.splice', function () {
3436
test.assertSelectorHasText('.count', '4')
3537
test.assertSelectorHasText('.item:nth-child(1)', '0 1')
3638
test.assertSelectorHasText('.item:nth-child(2)', '1 2')
3739
test.assertSelectorHasText('.item:nth-child(3)', '2 3')
3840
test.assertSelectorHasText('.item:nth-child(4)', '3 C')
39-
40-
this.click('.remove')
41+
})
42+
.thenClick('.remove', function () {
4143
test.assertSelectorHasText('.count', '3')
4244
test.assertSelectorHasText('.item:nth-child(1)', '0 1')
4345
test.assertSelectorHasText('.item:nth-child(2)', '1 2')
4446
test.assertSelectorHasText('.item:nth-child(3)', '2 3')
45-
46-
this.click('.replace')
47+
})
48+
.thenClick('.replace', function () {
4749
test.assertSelectorHasText('.count', '3')
4850
test.assertSelectorHasText('.item:nth-child(1)', '0 1')
4951
test.assertSelectorHasText('.item:nth-child(2)', '1 2')
5052
test.assertSelectorHasText('.item:nth-child(3)', '2 4')
51-
52-
this.click('.reverse')
53+
})
54+
.thenClick('.reverse', function () {
5355
test.assertSelectorHasText('.count', '3')
5456
test.assertSelectorHasText('.item:nth-child(1)', '0 4')
5557
test.assertSelectorHasText('.item:nth-child(2)', '1 2')
5658
test.assertSelectorHasText('.item:nth-child(3)', '2 1')
57-
58-
this.click('.sort')
59+
})
60+
.thenClick('.sort', function () {
5961
test.assertSelectorHasText('.count', '3')
6062
test.assertSelectorHasText('.item:nth-child(1)', '0 1')
6163
test.assertSelectorHasText('.item:nth-child(2)', '1 2')
6264
test.assertSelectorHasText('.item:nth-child(3)', '2 4')
63-
65+
})
66+
.then(function () {
6467
// make sure things work on empty array
6568
this.click('.pop')
6669
this.click('.pop')
@@ -72,10 +75,11 @@ casper.test.begin('Repeated Items', 41, function (test) {
7275
this.click('.sort')
7376
this.click('.reverse')
7477
this.click('.splice')
78+
})
79+
.then(function () {
7580
test.assertSelectorHasText('.count', '2')
7681
test.assertSelectorHasText('.item:nth-child(1)', '0 6')
7782
test.assertSelectorHasText('.item:nth-child(2)', '1 7')
78-
7983
})
8084
.run(function () {
8185
test.done()

0 commit comments

Comments
 (0)