event.ts
5.33 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/// <reference path="../typings/index.d.ts"/>
import Event from '../lib/Event';
import * as assert from 'assert';
suite('Event handling', function() {
let event: Event<string>;
setup(function() {
event = new Event();
});
test('no handlers', function() {
assert(!event.hasHandlers, 'event should report any registered handlers');
assert.doesNotThrow(function() {
event.dispatch('');
}, 'dispatch should not throw');
});
suite('one handler', function() {
test('no context', function() {
let called = false;
event.addHandler(function(payload: string) {
assert.equal(payload, 'foo');
called = true;
});
assert(event.hasHandlers, 'event should report registered handlers');
event.dispatch('foo');
assert(called, 'the handler should have been called');
});
test('with context', function() {
let foo = 0;
function handler(payload: string, context: number) {
assert.equal(payload, 'foo');
foo = context;
};
event.addHandler(handler, 5).dispatch('foo');
assert.equal(foo, 5, 'context was not transferred correctly');
assert(event.removeHandler(handler).hasHandlers, 'handler should only be remvoved if contet matches');
assert(!event.removeHandler(handler, 5).hasHandlers, 'handler should be remvoved if contet matches');
});
test('one handler, no context, multiple attachments', function() {
let i = 0;
function handler(): void {
i++;
}
event.addHandler(handler).addHandler(handler).dispatch('foo');
assert.equal(i, 1, 'handler should have been called once');
assert(event.isHandlerAttached(handler), 'handler should be reported as attached');
event.removeHandler(handler);
assert(!event.isHandlerAttached(handler), 'handler should be reported as not attached after removal');
});
test('one handler, different contexts, multiple attachments', function() {
let i = 0;
function handler(): void {
i++;
}
event.addHandler(handler, 1).addHandler(handler, 2).dispatch('foo');
assert.equal(i, 2, 'handler should have been called twice');
assert(!event.isHandlerAttached(handler), 'handler should be reported as not attached w/o context');
assert(event.isHandlerAttached(handler, 1), 'handler should be reported as attached with proper context');
});
});
suite('two handlers', function() {
let called1: boolean, called2: boolean,
contexts: Array<number>;
function handler1(payload: string) {
assert.equal(payload, 'foo');
called1 = true;
};
function handler2(payload: string) {
assert.equal(payload, 'foo');
called2 = true;
};
function ctxHandler(payload: string, context: any) {
contexts[context] = context;
assert.equal(payload, 'foo');
}
setup(function() {
called1 = called2 = false;
contexts = [0, 0, 0];
event
.addHandler(handler1)
.addHandler(handler2);
});
test('both attached', function() {
event.dispatch('foo');
assert(called1, 'handler 1 should have been called');
assert(called2, 'handler 2 should have been called');
});
test('handler 1 detached', function() {
event.removeHandler(handler1);
event.dispatch('foo');
assert(!called1, 'handler 1 should not have been called');
assert(called2, 'handler 2 should have been called');
});
test('handler 2 detached', function() {
event.removeHandler(handler2);
event.dispatch('foo');
assert(called1, 'handler 1 should have been called');
assert(!called2, 'handler 2 should not have been called');
});
test('context, both attached', function() {
event
.addHandler(ctxHandler, 1)
.addHandler(ctxHandler, 2)
.dispatch('foo');
assert.equal(contexts[1], 1, 'context 1 was not transferred correctly');
assert.equal(contexts[2], 2, 'context 2 was not transferred correctly');
});
test('context, handler 1 detached', function() {
event
.addHandler(ctxHandler, 1)
.addHandler(ctxHandler, 2)
.removeHandler(ctxHandler, 1)
.dispatch('foo');
assert.equal(contexts[1], 0, 'handler was not detached');
assert.equal(contexts[2], 2, 'context 2 was not transferred correctly');
});
test('context, handler 2 detached', function() {
event
.addHandler(ctxHandler, 1)
.addHandler(ctxHandler, 2)
.removeHandler(ctxHandler, 2)
.dispatch('foo');
assert.equal(contexts[1], 1, 'context 1 was not transferred correctly');
assert.equal(contexts[2], 0, 'handler was not detached');
});
});
});