-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
event_emitter_test.go
105 lines (95 loc) · 3.24 KB
/
event_emitter_test.go
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
package playwright
import (
"testing"
"github.com/stretchr/testify/require"
)
const (
testEventName = "foobar"
testEventNameFoo = "foo"
testEventNameBar = "bar"
)
func TestEventEmitterListenerCount(t *testing.T) {
handler := &eventEmitter{}
wasCalled := make(chan interface{}, 1)
myHandler := func(payload ...interface{}) {
wasCalled <- payload[0]
}
require.Nil(t, handler.events[testEventNameFoo])
handler.On(testEventNameFoo, myHandler)
require.Equal(t, 1, handler.ListenerCount(testEventNameFoo))
handler.Once(testEventNameFoo, myHandler)
require.Equal(t, 2, handler.ListenerCount(testEventNameFoo))
require.Nil(t, handler.events[testEventNameBar])
handler.Once(testEventNameBar, myHandler)
require.Equal(t, 1, handler.ListenerCount(testEventNameBar))
require.Equal(t, 3, handler.ListenerCount(""))
}
func TestEventEmitterOn(t *testing.T) {
handler := &eventEmitter{}
wasCalled := make(chan interface{}, 1)
require.Nil(t, handler.events[testEventName])
handler.On(testEventName, func(payload ...interface{}) {
wasCalled <- payload[0]
})
require.Equal(t, 1, handler.ListenerCount(testEventName))
value := 123
handler.Emit(testEventName, value)
result := <-wasCalled
require.Equal(t, 1, handler.ListenerCount(testEventName))
require.Equal(t, result.(int), value)
}
func TestEventEmitterOnce(t *testing.T) {
handler := &eventEmitter{}
wasCalled := make(chan interface{}, 1)
require.Nil(t, handler.events[testEventName])
handler.Once(testEventName, func(payload ...interface{}) {
wasCalled <- payload[0]
})
require.Equal(t, 1, handler.ListenerCount(testEventName))
value := 123
handler.Emit(testEventName, value)
result := <-wasCalled
require.Equal(t, result.(int), value)
require.Equal(t, 0, handler.ListenerCount(testEventName))
}
func TestEventEmitterRemove(t *testing.T) {
handler := &eventEmitter{}
wasCalled := make(chan interface{}, 1)
require.Nil(t, handler.events[testEventName])
myHandler := func(payload ...interface{}) {
wasCalled <- payload[0]
}
handler.On(testEventName, myHandler)
require.Equal(t, 1, handler.ListenerCount(testEventName))
value := 123
handler.Emit(testEventName, value)
result := <-wasCalled
require.Equal(t, 1, handler.ListenerCount(testEventName))
require.Equal(t, result.(int), value)
handler.Once(testEventName, myHandler)
handler.RemoveListener(testEventName, myHandler)
require.Equal(t, 0, handler.ListenerCount(testEventName))
}
func TestEventEmitterRemoveEmpty(t *testing.T) {
handler := &eventEmitter{}
handler.RemoveListener(testEventName, func(...interface{}) {})
require.Equal(t, 0, handler.ListenerCount(testEventName))
}
func TestEventEmitterRemoveKeepExisting(t *testing.T) {
handler := &eventEmitter{}
handler.On(testEventName, func(...interface{}) {})
handler.Once(testEventName, func(...interface{}) {})
handler.RemoveListener("abc123", func(...interface{}) {})
handler.RemoveListener(testEventName, func(...interface{}) {})
require.Equal(t, 2, handler.ListenerCount(testEventName))
}
func TestEventEmitterOnLessArgsAcceptingReceiver(t *testing.T) {
handler := &eventEmitter{}
wasCalled := make(chan bool, 1)
require.Nil(t, handler.events[testEventName])
handler.Once(testEventName, func(ev ...interface{}) {
wasCalled <- true
})
handler.Emit(testEventName)
<-wasCalled
}