forked from playwright-community/playwright-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_emitter_test.go
90 lines (81 loc) · 2.7 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
package playwright
import (
"testing"
"github.com/stretchr/testify/require"
)
const testEventName = "foobar"
func TestEventEmitterOn(t *testing.T) {
handler := &eventEmitter{}
handler.initEventEmitter()
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{}
handler.initEventEmitter()
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{}
handler.initEventEmitter()
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.initEventEmitter()
handler.RemoveListener(testEventName, func(...interface{}) {})
require.Equal(t, 0, handler.ListenerCount(testEventName))
}
func TestEventEmitterRemoveKeepExisting(t *testing.T) {
handler := &eventEmitter{}
handler.initEventEmitter()
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{}
handler.initEventEmitter()
wasCalled := make(chan bool, 1)
require.Nil(t, handler.events[testEventName])
handler.Once(testEventName, func(ev ...interface{}) {
wasCalled <- true
})
handler.Emit(testEventName)
<-wasCalled
}