-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.test.js
177 lines (141 loc) · 6.01 KB
/
app.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
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
171
172
173
174
175
176
177
'use strict';
const assert = require('chai').assert
, Events = require('events').EventEmitter
, emitter = new Events()
, fakeEmitter = new Events()
, app = require('./app');
let offTest = false;
describe('The Main Tests for event-state', () => {
beforeEach(() => {
emitter.required = app;
fakeEmitter.off = function () {
offTest = true;
};
fakeEmitter.once = undefined;
fakeEmitter.required = app;
});
it('should return a function', () => {
assert.isFunction(app);
assert.isFunction(emitter.required);
});
describe('Events should trigger the state machine', () => {
it('callback should receive an array', (done) => {
emitter.required(['test-event', 'test-event-2'], (dataArray) => {
assert.isArray(dataArray);
assert.strictEqual(dataArray.length, 2);
done();
});
emitter.emit('test-event', {'test': true});
emitter.emit('test-event-2', {'test': '1'});
});
it('callback array should be ordered in the same order as events', (done) => {
emitter.required(['test-event', 'test-event-2'], (dataArray) => {
assert.isArray(dataArray);
assert.strictEqual('test-event', dataArray[0]);
assert.strictEqual('test-event-2', dataArray[1]);
done();
});
emitter.emit('test-event', 'test-event');
emitter.emit('test-event-2', 'test-event-2');
});
it('events called without data should return true as their value', (done) => {
emitter.required(['test-event', 'test-event-2'], (dataArray) => {
assert.isArray(dataArray);
assert.strictEqual(true, dataArray[0]);
assert.strictEqual('test-event-2', dataArray[1]);
done();
});
emitter.emit('test-event');
emitter.emit('test-event-2', 'test-event-2');
});
it('multiple groups of events should trigger nicely and independently', (done) => {
emitter.required(['test-event', 'test-event-2'], (dataArray) => {
assert.isArray(dataArray);
assert.strictEqual('test-event', dataArray[0]);
assert.strictEqual('test-event-2', dataArray[1]);
emitter.emit('test-event-3', 'test-event-3');
});
emitter.required(['test-event', 'test-event-3'], (dataArray) => {
assert.isArray(dataArray);
assert.strictEqual('test-event', dataArray[0]);
assert.strictEqual('test-event-3', dataArray[1]);
done();
});
emitter.emit('test-event', 'test-event');
emitter.emit('test-event-2', 'test-event-2');
});
it('should allow for multiple triggers of the same state', (done) => {
var i = false;
emitter.required(['test', 'test-2'], () => {
if(i){
assert.strictEqual(i, true);
done();
} else {
assert.strictEqual(false, i);
i = true;
}
}, {}, true);
emitter.emit('test', 'test-event');
emitter.emit('test-2', 'test-event-2');
emitter.emit('test-2', 'test-event-2');
});
it('should unbind listeners if not multiple flagged and using .on', (done) => {
fakeEmitter.required(['test', 'test-3'], () => {
setTimeout(() => {
assert.strictEqual(true, offTest);
done();
}, 10);
});
fakeEmitter.emit('test');
fakeEmitter.emit('test-3');
});
});
describe('required state should be cancelable', () => {
it('should return an object with a cancel function that cancels the required', () => {
var test = true
, requiredEvent = emitter.required(['test-event', 'test-event-2'], () => {
test = false;
});
assert.isObject(requiredEvent);
assert.isFunction(requiredEvent.cancel);
});
it('should cancel the callback if called', (done) => {
var test = true
, requiredEvent = emitter.required(['test-event', 'test-event-2'], () => {
test = false;
});
//cancel the event
requiredEvent.cancel();
emitter.on('test-event-2', () => {
assert.strictEqual(true, test);
done();
});
emitter.emit('test-event', 'test-event');
emitter.emit('test-event-2', 'test-event-2');
});
it('should return an object with an add function that adds states to watch for', () => {
var test = true
, requiredEvent = emitter.required(['test-event', 'test-event-2'], () => {
test = false;
});
assert.isObject(requiredEvent);
assert.isFunction(requiredEvent.add);
});
it('should add additional functions when add is called with one or more events', (done) => {
var requiredEvent = emitter.required(['test-event-4', 'test-event-5'], (arr) => {
assert.strictEqual(6, arr.length);
done();
});
//cancel the event
emitter.emit('test-event-4', 'test-event');
requiredEvent.add('test-event-6');
emitter.emit('test-event-5', 'test-event-2');
requiredEvent.add('test-event-6', 'test-event-7');
requiredEvent.add(['test-event-8', 'test-event-9']);
emitter.emit('test-event-6', 'test-event-6');
emitter.emit('test-event-7', 'test-event-7');
emitter.emit('test-event-8', 'test-event-8');
emitter.emit('test-event-9', 'test-event-9');
});
});
});