def test_on(ees: EventEmitterS, mock: Mock) -> None: ees.on("event", mock.method) assert ees.listener_count("event") == 1 assert mock.method is ees.listeners("event")[0] assert ees.emit("event", 1, data=2) mock.method.assert_called_with(1, data=2) assert ees.listener_count("event") == 1 assert mock.method is ees.listeners("event")[0]
def test_on_decorator(ees: EventEmitterS, mock: Mock) -> None: @ees.on("event") def handler(*args, **kwargs) -> None: mock.method(*args, **kwargs) assert ees.listener_count("event") == 1 assert handler is ees.listeners("event")[0] assert ees.emit("event", 1, data=2) mock.method.assert_called_with(1, data=2) assert ees.listener_count("event") == 1 assert handler is ees.listeners("event")[0]
async def test_coroutine_listener_once(ees_with_event_loop: EventEmitterS, mock: Mock, deferred: Future) -> None: async def handler(*args, **kwargs) -> None: mock.method(*args, **kwargs) deferred.set_result(True) ees_with_event_loop.once("event", handler) assert ees_with_event_loop.listener_count("event") == 1 assert handler is ees_with_event_loop.listeners("event")[0] assert ees_with_event_loop.emit("event", 1, data=2) assert await deferred assert not ees_with_event_loop.emit("event", 1, data=2) mock.method.assert_called_once_with(1, data=2) assert ees_with_event_loop.listener_count("event") == 0 assert handler not in ees_with_event_loop.listeners("event")
def test_remove_all_listeners(ees: EventEmitterS, arg_swallower: Callable[..., None]) -> None: ees.on("event", arg_swallower) assert ees.listener_count("event") == 1 assert arg_swallower is ees.listeners("event")[0] ees.remove_all_listeners() assert ees.listener_count("event") == 0
def test_get_listeners_registered_for_event( ees: EventEmitterS, arg_swallower: Callable[..., None]) -> None: ees.on("event", arg_swallower) ees_listeners = ees.listeners("event") assert len(ees_listeners) == 1 assert ees_listeners[0] is arg_swallower