diff --git a/lib/src/request/request_manager.dart b/lib/src/request/request_manager.dart index 363a51e..de48fcf 100644 --- a/lib/src/request/request_manager.dart +++ b/lib/src/request/request_manager.dart @@ -89,6 +89,11 @@ class RequestManager { } extension RequestManagerExtensions on RequestManager { + /// Registers the given [handler]. + /// + /// This will create a function based request handler. + /// + /// See [RequestHandler.function]. void registerFunction>( FutureOr Function(TRequest) handler, ) { diff --git a/test/unit/event/event_subscription_builder/event_subscription_builder_test.dart b/test/unit/event/event_subscription_builder/event_subscription_builder_test.dart index 975541b..f0ea21b 100644 --- a/test/unit/event/event_subscription_builder/event_subscription_builder_test.dart +++ b/test/unit/event/event_subscription_builder/event_subscription_builder_test.dart @@ -26,6 +26,15 @@ void main() { }); group('subscribe', () { + test('it subscribes the handler', () { + final handler = EventHandler.function((event) {}); + + EventSubscriptionBuilder.create(mockEventHandlerStore) + .subscribe(handler); + + verify(() => mockEventHandlerStore.register(handler)); + }); + test('it return a subscription', () { final subscription = EventSubscriptionBuilder.create(mockEventHandlerStore) @@ -52,6 +61,13 @@ void main() { }); group('subscribeFunction', () { + test('it subscribes the handler', () { + EventSubscriptionBuilder.create(mockEventHandlerStore) + .subscribeFunction((event) {}); + + verify(() => mockEventHandlerStore.register(any>())); + }); + test('it return a subscription', () { final subscription = EventSubscriptionBuilder.create(mockEventHandlerStore) @@ -63,6 +79,52 @@ void main() { reason: 'it should return a subscription', ); }); + + test('it can cancel the subscription', () { + final subscription = + EventSubscriptionBuilder.create(mockEventHandlerStore) + .subscribeFunction((event) {}); + + subscription.cancel(); + + verify( + () => mockEventHandlerStore.unregister(any>()), + ); + }); + }); + + group('subscribeFactory', () { + EventHandler handlerFactory() => + EventHandler.function((event) {}); + + test('it subscribes the handler', () { + EventSubscriptionBuilder.create(mockEventHandlerStore) + .subscribeFactory(handlerFactory); + + verify(() => mockEventHandlerStore.registerFactory(handlerFactory)); + }); + + test('it return a subscription', () { + final subscription = + EventSubscriptionBuilder.create(mockEventHandlerStore) + .subscribeFactory(handlerFactory); + + expect( + subscription, + isA(), + reason: 'it should return a subscription', + ); + }); + + test('it can cancel the subscription', () { + final subscription = + EventSubscriptionBuilder.create(mockEventHandlerStore) + .subscribeFactory(handlerFactory); + + subscription.cancel(); + + verify(() => mockEventHandlerStore.unregisterFactory(handlerFactory)); + }); }); }); } diff --git a/test/unit/request/request_handler/request_handler_store_test.dart b/test/unit/request/request_handler/request_handler_store_test.dart index 7791f00..ecf7cc1 100644 --- a/test/unit/request/request_handler/request_handler_store_test.dart +++ b/test/unit/request/request_handler/request_handler_store_test.dart @@ -1,3 +1,4 @@ +import 'package:dart_mediator/src/request/handler/request_handler.dart'; import 'package:dart_mediator/src/request/handler/request_handler_store.dart'; import 'package:test/test.dart'; @@ -7,13 +8,16 @@ void main() { group('RequestHandlerStore', () { late RequestHandlerStore requestHandlerStore; + RequestHandler> handlerFactory() => + MockRequestHandler>(); + + final mockRequestHandler = MockRequestHandler>(); + setUp(() { requestHandlerStore = RequestHandlerStore(); }); group('register', () { - final mockRequestHandler = MockRequestHandler>(); - test('it registers the handler', () { expect( () => requestHandlerStore.register(mockRequestHandler), @@ -28,17 +32,42 @@ void main() { throwsAssertionError, ); }); + + test('it throws when a factory for this type was already registered', () { + requestHandlerStore.registerFactory(handlerFactory); + + expect( + () => requestHandlerStore.register(mockRequestHandler), + throwsAssertionError, + ); + }); }); group('registerFactory', () { test('it registers the handler', () { expect( - () => requestHandlerStore.registerFactory>( - () => MockRequestHandler>(), - ), + () => requestHandlerStore.registerFactory(handlerFactory), returnsNormally, ); }); + + test('it throws when registering the same factory multiple times', () { + requestHandlerStore.registerFactory(handlerFactory); + + expect( + () => requestHandlerStore.registerFactory(handlerFactory), + throwsAssertionError, + ); + }); + + test('it throws when a handler for this type was already registered', () { + requestHandlerStore.register(mockRequestHandler); + + expect( + () => requestHandlerStore.registerFactory(handlerFactory), + throwsAssertionError, + ); + }); }); group('unregister', () { diff --git a/test/unit/request/request_handler/request_handler_test.dart b/test/unit/request/request_handler/request_handler_test.dart new file mode 100644 index 0000000..a9fb126 --- /dev/null +++ b/test/unit/request/request_handler/request_handler_test.dart @@ -0,0 +1,24 @@ +import 'package:dart_mediator/src/request/handler/request_handler.dart'; +import 'package:test/test.dart'; + +import '../../../mocks.dart'; + +void main() { + group('RequestHandler', () { + setUp(() {}); + + group('handle', () { + test('it handles the request', () { + var handled = false; + final mockRequest = MockRequest(); + final handler = RequestHandler>.function((req) { + handled = true; + return 123; + }); + + expect(handler.handle(mockRequest), 123); + expect(handled, isTrue); + }); + }); + }); +}