-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest.js
More file actions
107 lines (88 loc) · 2.07 KB
/
test.js
File metadata and controls
107 lines (88 loc) · 2.07 KB
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
'use strict'
var test = require('tape')
var Hash = require('observ-varhash')
var Observ = require('observ')
var ObservArray = require('observ-array')
var WeakmapEvent = require('./')
test('returns gevental eventent mapped to an object', function (t) {
t.plan(1)
var event = WeakmapEvent()
var obj = {}
var unlisten = event.listen(obj, function (result) {
t.equal(result, 'hello')
unlisten()
})
event.broadcast(obj, 'hello')
// This second broadcast is effectively a noop because of the unlisten
event.broadcast(obj, 'hello')
})
test('dispatch eventents for an object', function (t) {
t.plan(2)
var event = WeakmapEvent()
var obj = {}
var obj2 = {}
event.listen(obj, function (data) {
t.equal(data, 'hello')
})
event.listen(obj2, function (data) {
t.equal(data, 'goodbye')
})
event.broadcast(obj, 'hello')
event.broadcast(obj2, 'goodbye')
})
test('toHash', function (t) {
t.plan(3)
var hash = Hash({
a: Observ(1),
b: Observ(2)
})
var event = WeakmapEvent()
event.listen.toHash(hash, function (data) {
t.equal(data.value, 'foo')
})
event.broadcast(hash.a, {
value: 'foo'
})
event.broadcast(hash.b, {
value: 'foo'
})
// now we add a key which should be listened on
hash.put('c', Observ(3))
event.broadcast(hash.c, {
value: 'foo'
})
})
test('toArray', function (t) {
t.plan(4)
var arr = ObservArray([Observ(1)])
var event = WeakmapEvent()
event.listen.toArray(arr, function (data) {
t.equal(data.value, 'foo')
})
event.broadcast(arr.get(0), {
value: 'foo'
})
arr.push(Observ(2))
event.broadcast(arr.get(1), {
value: 'foo'
})
arr.push(Observ(3), Observ(4), Observ(5))
event.broadcast(arr.get(3), {
value: 'foo'
})
arr.get(0).set('first')
event.broadcast(arr.get(0), {
value: 'foo'
})
arr.splice(3, 1)
})
test('argument validation', function (t) {
var event = WeakmapEvent()
t.throws(function () {
event.listen(function listener () {})
}, 'listen')
t.throws(function () {
event.broadcast('value')
}, 'broadcast')
t.end()
})