Skip to content

Commit

Permalink
Improve coverage (#8)
Browse files Browse the repository at this point in the history
* Add better tests for subscribe function

* add better tests for subscribe factory

* Add better comment

* Add an individual test for request handler

* Add tests for asserts
  • Loading branch information
Matthiee authored May 5, 2024
1 parent d1a9a8e commit 8bab4ee
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lib/src/request/request_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<TResponse, TRequest extends Request<TResponse>>(
FutureOr<TResponse> Function(TRequest) handler,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ void main() {
});

group('subscribe', () {
test('it subscribes the handler', () {
final handler = EventHandler<int>.function((event) {});

EventSubscriptionBuilder<int>.create(mockEventHandlerStore)
.subscribe(handler);

verify(() => mockEventHandlerStore.register(handler));
});

test('it return a subscription', () {
final subscription =
EventSubscriptionBuilder<int>.create(mockEventHandlerStore)
Expand All @@ -52,6 +61,13 @@ void main() {
});

group('subscribeFunction', () {
test('it subscribes the handler', () {
EventSubscriptionBuilder<int>.create(mockEventHandlerStore)
.subscribeFunction((event) {});

verify(() => mockEventHandlerStore.register(any<EventHandler<int>>()));
});

test('it return a subscription', () {
final subscription =
EventSubscriptionBuilder<int>.create(mockEventHandlerStore)
Expand All @@ -63,6 +79,52 @@ void main() {
reason: 'it should return a subscription',
);
});

test('it can cancel the subscription', () {
final subscription =
EventSubscriptionBuilder<int>.create(mockEventHandlerStore)
.subscribeFunction((event) {});

subscription.cancel();

verify(
() => mockEventHandlerStore.unregister(any<EventHandler<int>>()),
);
});
});

group('subscribeFactory', () {
EventHandler<int> handlerFactory() =>
EventHandler<int>.function((event) {});

test('it subscribes the handler', () {
EventSubscriptionBuilder<int>.create(mockEventHandlerStore)
.subscribeFactory(handlerFactory);

verify(() => mockEventHandlerStore.registerFactory(handlerFactory));
});

test('it return a subscription', () {
final subscription =
EventSubscriptionBuilder<int>.create(mockEventHandlerStore)
.subscribeFactory(handlerFactory);

expect(
subscription,
isA<EventSubscription>(),
reason: 'it should return a subscription',
);
});

test('it can cancel the subscription', () {
final subscription =
EventSubscriptionBuilder<int>.create(mockEventHandlerStore)
.subscribeFactory(handlerFactory);

subscription.cancel();

verify(() => mockEventHandlerStore.unregisterFactory(handlerFactory));
});
});
});
}
39 changes: 34 additions & 5 deletions test/unit/request/request_handler/request_handler_store_test.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -7,13 +8,16 @@ void main() {
group('RequestHandlerStore', () {
late RequestHandlerStore requestHandlerStore;

RequestHandler<int, MockRequest<int>> handlerFactory() =>
MockRequestHandler<int, MockRequest<int>>();

final mockRequestHandler = MockRequestHandler<int, MockRequest<int>>();

setUp(() {
requestHandlerStore = RequestHandlerStore();
});

group('register', () {
final mockRequestHandler = MockRequestHandler<int, MockRequest<int>>();

test('it registers the handler', () {
expect(
() => requestHandlerStore.register(mockRequestHandler),
Expand All @@ -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<int, MockRequest<int>>(
() => MockRequestHandler<int, MockRequest<int>>(),
),
() => 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', () {
Expand Down
24 changes: 24 additions & 0 deletions test/unit/request/request_handler/request_handler_test.dart
Original file line number Diff line number Diff line change
@@ -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<int>();
final handler = RequestHandler<int, MockRequest<int>>.function((req) {
handled = true;
return 123;
});

expect(handler.handle(mockRequest), 123);
expect(handled, isTrue);
});
});
});
}

0 comments on commit 8bab4ee

Please sign in to comment.