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

feat: made resetAdapters public #1014

Merged
merged 5 commits into from
Jun 30, 2022
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
12 changes: 12 additions & 0 deletions hive/lib/src/hive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ abstract class HiveInterface implements TypeRegistry {

/// Checks if a box exists
Future<bool> boxExists(String name, {String? path});

/// Clears all registered adapters.
///
/// To register an adapter use [registerAdapter].
///
/// NOTE: [resetAdapters] also clears the default adapters registered
/// by Hive.
alestiago marked this conversation as resolved.
Show resolved Hide resolved
///
/// WARNING: This method is only intended to be used for integration and unit tests
/// and SHOULD not be used in production code.
@visibleForTesting
void resetAdapters();
}

///
Expand Down
1 change: 0 additions & 1 deletion hive/lib/src/registry/type_registry_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ class TypeRegistryImpl implements TypeRegistry {
return findAdapterForTypeId(typeId) != null;
}

/// Not part of public API
void resetAdapters() {
_typeAdapters.clear();
}
Expand Down
32 changes: 32 additions & 0 deletions hive/test/tests/hive_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ import 'package:test/test.dart';

import 'common.dart';

class _TestAdapter extends TypeAdapter<int> {
_TestAdapter([this.typeId = 0]);

@override
final int typeId;

@override
int read(_) => 5;

@override
void write(_, __) {}
}

void main() {
group('HiveImpl', () {
Future<HiveImpl> initHive() async {
Expand Down Expand Up @@ -316,5 +329,24 @@ void main() {
await hive.close();
});
});

group('.resetAdapters()', () {
test('returns normally', () async {
final hive = await initHive();
expect(hive.resetAdapters, returnsNormally);
});

test('clears an adapter', () async {
final hive = await initHive();
final adapter = _TestAdapter(1);

expect(hive.isAdapterRegistered(adapter.typeId), isFalse);
hive.registerAdapter(adapter);
expect(hive.isAdapterRegistered(adapter.typeId), isTrue);

hive.resetAdapters();
expect(hive.isAdapterRegistered(adapter.typeId), isFalse);
});
});
});
}