Skip to content

Commit

Permalink
Add unregister method for requests (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthiee authored May 12, 2024
1 parent 5399863 commit 8cc66ba
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/src/request/request_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class RequestManager {
_requestHandlerStore.register(handler);
}

/// Registers the request [handler] for the given [TRequest].
void unregister<TResponse, TRequest extends Request<TResponse>>(
RequestHandler<TResponse, TRequest> handler,
) {
_requestHandlerStore.unregister(handler);
}

/// Sends a [request] to a single [RequestHandler].
///
/// Make sure the [RequestHandler] is [register]ed before calling this method.
Expand Down
13 changes: 13 additions & 0 deletions test/integration/request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:dart_mediator/mediator.dart';
import 'package:test/test.dart';

import '../mocks.dart';
import '../test_data.dart';

class GetDataQueryHandler implements QueryHandler<String, GetDataQuery> {
Expand All @@ -22,6 +23,18 @@ void main() {
});

group('requests', () {
test('it unregisters the request handler', () async {
final handler = GetDataQueryHandler();

mediator.requests.register(handler);
mediator.requests.unregister(handler);

await expectLater(
mediator.requests.send(const GetDataQuery(123)),
throwsAssertionError,
);
});

test('it handles the normal request', () async {
mediator.requests.register(GetDataQueryHandler());

Expand Down
11 changes: 11 additions & 0 deletions test/unit/request/requests_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ void main() {
});
});

group('unregister', () {
test('it unregisters the handler', () {
final mockRequestHandler =
MockRequestHandler<String, MockRequest<String>>();

requestsManager.unregister(mockRequestHandler);

verify(() => mockRequestHandlerStore.unregister(mockRequestHandler));
});
});

group('send{TResponse, TRequest}', () {
const output = '123';
late MockRequest<String> mockRequest;
Expand Down

0 comments on commit 8cc66ba

Please sign in to comment.