chore(event-listeners-manager): add tests

This commit is contained in:
Julien Fontanet 2022-06-15 14:20:02 +02:00
parent 4f50f90213
commit f096024248
4 changed files with 74 additions and 2 deletions

View File

@ -0,0 +1,67 @@
'use strict'
const t = require('tap')
const { EventEmitter } = require('events')
const { EventListenersManager } = require('./')
const noop = Function.prototype
// function spy (impl = Function.prototype) {
// function spy() {
// spy.calls.push([Array.from(arguments), this])
// }
// spy.calls = []
// return spy
// }
function assertListeners(t, event, listeners) {
t.strictSame(t.context.ee.listeners(event), listeners)
}
t.beforeEach(function (t) {
t.context.ee = new EventEmitter()
t.context.em = new EventListenersManager(t.context.ee)
})
t.test('.add adds a listener', function (t) {
t.context.em.add('foo', noop)
assertListeners(t, 'foo', [noop])
t.end()
})
t.test('.add does not add a duplicate listener', function (t) {
t.context.em.add('foo', noop).add('foo', noop)
assertListeners(t, 'foo', [noop])
t.end()
})
t.test('.remove removes a listener', function (t) {
t.context.em.add('foo', noop).remove('foo', noop)
assertListeners(t, 'foo', [])
t.end()
})
t.test('.removeAll removes all listeners of a given type', function (t) {
t.context.em.add('foo', noop).add('bar', noop).removeAll('foo')
assertListeners(t, 'foo', [])
assertListeners(t, 'bar', [noop])
t.end()
})
t.test('.removeAll removes all listeners', function (t) {
t.context.em.add('foo', noop).add('bar', noop).removeAll()
assertListeners(t, 'foo', [])
assertListeners(t, 'bar', [])
t.end()
})

View File

@ -37,6 +37,10 @@
"license": "ISC",
"version": "1.0.0",
"scripts": {
"postversion": "npm publish --access public"
"postversion": "npm publish --access public",
"test": "tap --branches=72"
},
"devDependencies": {
"tap": "^16.2.0"
}
}

View File

@ -60,6 +60,7 @@
"testEnvironment": "node",
"testPathIgnorePatterns": [
"/@vates/decorate-with/",
"/@vates/event-listeners-manager/",
"/@vates/predicates/",
"/@xen-orchestra/audit-core/",
"/dist/",

View File

@ -17576,7 +17576,7 @@ tap-yaml@^1.0.0:
dependencies:
yaml "^1.5.0"
tap@^16.0.1, tap@^16.1.0:
tap@^16.0.1, tap@^16.1.0, tap@^16.2.0:
version "16.2.0"
resolved "https://registry.yarnpkg.com/tap/-/tap-16.2.0.tgz#0e47edcf1089e386630dc8d779dc8b20cfc7ee1d"
integrity sha512-ikfNLy701p2+sH3R0pAXQ/Aen6ZByaguUY7UsoTLL4AXa2c9gYQL+pI21p13lq54R7/CEoLaViC1sexcWG32ig==