UNPKG

mgraph.events

Version:

Modern eventing library for Node.js and browsers

156 lines (143 loc) 3.85 kB
// test/fire.js import { test } from 'tap'; import eventify from '../index.js'; test('fire fires callback', (t) => { const subject = {}; eventify(subject); t.plan(1); subject.on('something', () => { t.ok(true, 'fired callback'); }); subject.fire('something'); t.end(); }); test('fire fires all callbacks', (t) => { t.plan(2); const subject = eventify({}); const onSomething = () => { t.ok(true, 'fired callback'); }; subject.on('something', onSomething); subject.on('something', onSomething); subject.fire('something'); t.end(); }); test('Chaining can be used on fire and "on"', (t) => { t.plan(2); const subject = eventify({}); const onSomething = () => { t.ok(true, 'fired callback'); }; subject.on('beep', onSomething).on('bop', onSomething); subject.fire('beep').fire('bop'); t.end(); }); test('fire passes all arguments', (t) => { t.plan(2); const subject = eventify({}); const testX = 42, testY = 'hello'; subject.on('something', (x, y) => { t.equal(x, testX, "X argument should be expected"); t.equal(y, testY, "Y argument should be expected"); }); subject.fire('something', testX, testY); t.end(); }); test('"on" and fire preserves the context', (t) => { const subject = eventify({}); const context = {}; t.plan(1); subject.on('something', function () { t.equal(this, context, "Context is preserved"); }, context); subject.fire('something'); t.end(); }); test('"off" removes passed listener', (t) => { t.plan(1); const subject = eventify({}); const onFoo = () => { t.fail("off() did not properly remove the handler"); }; const onBar = () => { t.pass("off() removed bar handler"); }; subject.on('foo', onFoo); subject.on('bar', onBar); subject.off('foo', onFoo); subject.fire('foo'); subject.fire('bar'); t.end(); }); test('"off" removes only one from the same event name', (t) => { t.plan(1); const subject = eventify({}); const onFoo1 = () => { t.fail("off() did not properly remove the handler"); }; const onFoo2 = () => { t.pass("off() removed correct handler"); }; subject.on('foo', onFoo1); subject.on('foo', onFoo2); subject.off('foo', onFoo1); subject.fire('foo'); t.end(); }); test('"off" removes all for given event name', (t) => { t.plan(0); const subject = eventify({}); const onFoo = () => { t.fail("off() did not properly remove the handler"); }; subject.on('foo', onFoo); subject.off('foo'); subject.fire('foo'); t.end(); }); test('"off" removes all events', (t) => { t.plan(0); const subject = eventify({}); const onFoo = () => { t.fail("off() did not properly remove the handler"); }; subject.on('foo', onFoo); subject.on('bar', onFoo); subject.off(); subject.fire('foo'); subject.fire('bar'); t.end(); }); test('"off" does not harm when no such event', (t) => { t.plan(1); const subject = eventify({}); const onFoo = () => { t.pass("Existing event fired correctly"); }; subject.on('foo', onFoo); subject.off('bar', onFoo); subject.fire('foo'); subject.fire('bar'); t.end(); }); test('"off" can remove by function', (t) => { t.plan(1); const subject = eventify({}); const onFooYes = () => { t.pass("onFooYes fired"); }; const onFooNo = () => { t.fail("off() did not remove onFooNo"); }; subject.on('foo', onFooYes); subject.on('foo', onFooNo); subject.off('foo', onFooNo); subject.fire('foo'); t.end(); }); test('eventify returns the same subject', (t) => { const subject = {}; const result = eventify(subject); t.equal(result, subject, "eventified result should be the same as subject"); t.end(); });