Skip to content

Commit

Permalink
add spec for hasOwnListenersFor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Ferrari committed Sep 7, 2022
1 parent a408140 commit 1ba7f0e
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/strongbus_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,19 +979,19 @@ describe('Strongbus.Bus', () => {

describe('and the instance has no delegates', () => {
it("lists the instance's listeners", () => {
expect(bus.listeners).toEqual(new Map([[
expect(bus.ownListeners).toEqual(new Map([[
'foo', new Set([onTestEvent])
]]));
bus.on('*', onAnyEvent);
expect(bus.listeners.get('foo')).toEqual(new Set([onTestEvent]));
expect(bus.listeners.get('*').size).toEqual(1); // will be an anonymous wrapper around `onAnyEvent`
expect(bus.ownListeners.get('foo')).toEqual(new Set([onTestEvent]));
expect(bus.ownListeners.get('*').size).toEqual(1); // will be an anonymous wrapper around `onAnyEvent`
});
});

describe('and the instance has delegates with no listeners', () => {
it("lists the instance's listeners", () => {
bus.pipe(bus2);
expect(bus.listeners).toEqual(new Map([[
expect(bus.ownListeners).toEqual(new Map([[
'foo', new Set([onTestEvent])
]]));
});
Expand Down Expand Up @@ -1061,6 +1061,35 @@ describe('Strongbus.Bus', () => {
});
});

describe('#hasOwnListenersFor', () => {
describe('given an instance has no listeners registered for an event', () => {
it('returns false', () => {
bus.destroy();
expect(bus.hasOwnListenersFor('foo')).toBe(false);
});

describe('given an instance has delegates registered for an event', () => {
it('returns false', () => {
bus.destroy();
const bus2 = new DelegateTestBus({emulateListenerCount: true});
bus.pipe(bus2);
bus2.on('foo', () => {return; });
expect(bus.hasOwnListenersFor('foo')).toBe(false);
});
});
});

describe('given an instance has listeners registered for an event', () => {
it('returns true', () => {
bus.destroy();
const handleFoo = (payload: string) => {return; };
bus.on('foo', handleFoo);

expect(bus.hasOwnListenersFor('foo')).toBe(true);
});
});
});

describe('#destroy', () => {
it('removes all event listeners, triggering proper lifecycle events', () => {
bus = new Strongbus.Bus({allowUnhandledEvents: false});
Expand Down

0 comments on commit 1ba7f0e

Please sign in to comment.