-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
92 lines (74 loc) · 2.07 KB
/
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
/*!
* emitter-only <https://github.com/doowb/emitter-only>
*
* Copyright (c) 2015 .
* Licensed under the MIT license.
*/
'use strict';
/* deps:mocha */
var assert = require('assert');
var should = require('should');
var util = require('util');
var Emitter = require('component-emitter');
var only = require('./');
describe('emitter-only', function () {
it('should add an event listener one time', function () {
function MyEmitter() {}
MyEmitter.prototype.only = function() {
return only.apply(this, arguments);
};
var count = 0;
var output = [];
var emitter = Emitter(new MyEmitter());
emitter.on('foo', function (msg) {
count++;
output.push('foo-' + msg);
});
emitter.only('one-time', 'foo', function (msg) {
count++;
output.push('foo-1-' + msg);
});
emitter.only('one-time', 'foo', function (msg) {
count++;
output.push('foo-2-' + msg);
});
emitter.only('two-time', 'foo', function (msg) {
count++;
output.push('foo-3-' + msg);
});
emitter.emit('foo', 'bar');
assert.equal(count, 3);
assert.deepEqual(output, ['foo-bar', 'foo-1-bar', 'foo-3-bar']);
});
it('should add an event listener one time on inherited constructor', function () {
function MyEmitter() {
Emitter.call(this);
}
util.inherits(MyEmitter, Emitter);
MyEmitter.prototype.only = function() {
return only.apply(this, arguments);
};
var count = 0;
var output = [];
var emitter = new MyEmitter();
emitter.on('foo', function (msg) {
count++;
output.push('foo-' + msg);
});
emitter.only('one-time', 'foo', function (msg) {
count++;
output.push('foo-1-' + msg);
});
emitter.only('one-time', 'foo', function (msg) {
count++;
output.push('foo-2-' + msg);
});
emitter.only('two-time', 'foo', function (msg) {
count++;
output.push('foo-3-' + msg);
});
emitter.emit('foo', 'bar');
assert.equal(count, 3);
assert.deepEqual(output, ['foo-bar', 'foo-1-bar', 'foo-3-bar']);
});
});