Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unregister method for requests #11

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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