|
| 1 | +#!/usr/bin/env node |
| 2 | +const tape = require('tape') |
| 3 | +const events = require('events') |
| 4 | +const EventProxy = require('./EventProxy.js') |
| 5 | + |
| 6 | +tape('basic listening', function (t) { |
| 7 | + var target = new events.EventEmitter() |
| 8 | + var proxy = new EventProxy(target) |
| 9 | + t.equals(proxy.listenerCount('hello'), 0, 'By default no listener is added') |
| 10 | + target.addListener('hello', function () {}) |
| 11 | + t.equals(proxy.listenerCount('hello'), 0, 'Just because a listener is added to the target doesnt mean there is a listener on the source') |
| 12 | + var addResult = proxy.addListener('holla', function (data) { |
| 13 | + t.equals(data, 'mundo', 'target event is passed to the proxy') |
| 14 | + t.end() |
| 15 | + }) |
| 16 | + t.equals(addResult, proxy, 'Return type of ') |
| 17 | + t.equals(target.listenerCount('holla'), 1, 'There should be a listener on the target, after adding one to the proxy') |
| 18 | + target.emit('holla', 'mundo') |
| 19 | +}) |
| 20 | + |
| 21 | +tape('once', function (t) { |
| 22 | + var target = new events.EventEmitter() |
| 23 | + var proxy = new EventProxy(target) |
| 24 | + var count = 0 |
| 25 | + var addResult = proxy.once('hello', function (data) { |
| 26 | + t.equals(count, 0, 'event called once.') |
| 27 | + t.equals(data, 'world', 'data passed through') |
| 28 | + count += 1 |
| 29 | + }) |
| 30 | + t.equals(addResult, proxy, 'Result of once should be the proxy instance') |
| 31 | + t.equals(target.listenerCount('hello'), 1, 'Should have listener after once added') |
| 32 | + target.emit('hello', 'world') |
| 33 | + t.equals(target.listenerCount('hello'), 0, 'Should not have listener on target after once executed') |
| 34 | + t.equals(proxy.listenerCount('hello'), 0, 'Should not have listener on proxy after once executed') |
| 35 | + target.emit('hello', 'mundo') |
| 36 | + t.end() |
| 37 | +}) |
| 38 | + |
| 39 | +tape('prepend once', function (t) { |
| 40 | + var target = new events.EventEmitter() |
| 41 | + var proxy = new EventProxy(target) |
| 42 | + var count = 0 |
| 43 | + target.addListener('hello', function () { |
| 44 | + count += 1 |
| 45 | + }) |
| 46 | + proxy.addListener('hello', function () { |
| 47 | + count += 2 |
| 48 | + }) |
| 49 | + var addResult = proxy.prependOnceListener('hello', function (data) { |
| 50 | + t.equals(count, 1, 'event called once, before the listeners on proxy, after the listeners on target') |
| 51 | + t.equals(data, 'world', 'data passed through') |
| 52 | + count += 1 |
| 53 | + }) |
| 54 | + t.equals(addResult, proxy, 'Result of prepend once is proxy instance') |
| 55 | + target.emit('hello', 'world') |
| 56 | + target.emit('hello', 'mundo') |
| 57 | + t.equals(count, 7) |
| 58 | + t.end() |
| 59 | +}) |
| 60 | + |
| 61 | +tape('remove all listeners', function (t) { |
| 62 | + var target = new events.EventEmitter() |
| 63 | + var proxy = new EventProxy(target) |
| 64 | + var count = 0 |
| 65 | + target.addListener('hello', function () { |
| 66 | + count += 1 |
| 67 | + }) |
| 68 | + proxy.addListener('hello', function () { |
| 69 | + t.fail('Shouldnt be called because removed by removeAllListeners') |
| 70 | + }) |
| 71 | + var removeResult = proxy.removeAllListeners('hello') |
| 72 | + t.equals(removeResult, proxy, 'Result of removeAllListeners is proxy instance') |
| 73 | + target.emit('hello', 'world') |
| 74 | + t.equals(count, 1, 'target listener is still called') |
| 75 | + t.end() |
| 76 | +}) |
| 77 | + |
| 78 | +tape('own events', function (t) { |
| 79 | + var target = new events.EventEmitter() |
| 80 | + var proxy = new EventProxy(target) |
| 81 | + proxy._ownEvents['hello'] = true |
| 82 | + target.on('hello', function (data) { |
| 83 | + t.equals(data, 'world', 'target called with world') |
| 84 | + }) |
| 85 | + var onResult = proxy.on('hello', function (data) { |
| 86 | + t.equals(data, 'mundo', 'proxy called with mundo, target not passed to proxy') |
| 87 | + }) |
| 88 | + t.equals(onResult, proxy, 'result of on is proxy instance') |
| 89 | + target.emit('hello', 'world') |
| 90 | + proxy.emit('hello', 'mundo') |
| 91 | + t.end() |
| 92 | +}) |
0 commit comments