-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
events_test.js
71 lines (59 loc) · 1.66 KB
/
events_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
module.exports = {
plugin: require('./events'),
setup: setup,
}
function setup(store) {
test('events', function() {
store.set('foo', 'bar')
var expectationNone = _createExpectation('expectNone', undefined)
store.watch('foo', function(){})
var expectation1 = _createExpectation('foo', 'bar')
var expectationOnce = _createExpectation('foo', 'bar', true)
store.watch('foo', function(){})
expectation1.add('bar2')
expectationOnce.add('bar2')
store.set('foo', 'bar2')
expectation1.add(undefined)
store.remove('foo')
expectation1.add('bar3')
store.set('foo', 'bar3')
var expectation2 = _createExpectation('foo', 'bar3')
expectation1.add(undefined)
expectation2.add(undefined)
store.clearAll() // Should fire for foo
store.clearAll() // Should not fire anything
expectation1.unwatch()
expectation2.add('bar4')
store.set('foo', 'bar4') // Should only fire for expectation2
expectation1.check()
expectationOnce.check()
expectation2.check()
expectationNone.check()
expectation2.unwatch()
})
function _createExpectation(key, firstOldVal, useOnce) {
var expectation = {
values: [firstOldVal],
count: 0,
add: function(value) {
this.values.push(value)
},
check: function() {
assert(expectation.count + 1 == expectation.values.length)
},
unwatch: function() {
store.unwatch(watchId)
}
}
var watchId = (useOnce
? store.once(key, callback)
: store.watch(key, callback)
)
function callback(val, oldVal) {
expectation.count += 1
assert(expectation.values[expectation.count] == val)
assert(expectation.values[expectation.count - 1] == oldVal)
}
return expectation
}
}