Skip to content

Commit b278001

Browse files
authored
feat: made resetAdapters public (#1014)
* feat: made resetAdapters public * chore: run dart format * docs: improved documentation * docs: PR suggestion by @themisir Co-authored-by: Misir Jafarov <[email protected]> * refactor: included visibleForTesting
1 parent 18b0eea commit b278001

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

hive/lib/src/hive.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ abstract class HiveInterface implements TypeRegistry {
6969

7070
/// Checks if a box exists
7171
Future<bool> boxExists(String name, {String? path});
72+
73+
/// Clears all registered adapters.
74+
///
75+
/// To register an adapter use [registerAdapter].
76+
///
77+
/// NOTE: [resetAdapters] also clears the default adapters registered
78+
/// by Hive.
79+
///
80+
/// WARNING: This method is only intended to be used for integration and unit tests
81+
/// and SHOULD not be used in production code.
82+
@visibleForTesting
83+
void resetAdapters();
7284
}
7385

7486
///

hive/lib/src/registry/type_registry_impl.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ class TypeRegistryImpl implements TypeRegistry {
132132
return findAdapterForTypeId(typeId) != null;
133133
}
134134

135-
/// Not part of public API
136135
void resetAdapters() {
137136
_typeAdapters.clear();
138137
}

hive/test/tests/hive_impl_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ import 'package:test/test.dart';
99

1010
import 'common.dart';
1111

12+
class _TestAdapter extends TypeAdapter<int> {
13+
_TestAdapter([this.typeId = 0]);
14+
15+
@override
16+
final int typeId;
17+
18+
@override
19+
int read(_) => 5;
20+
21+
@override
22+
void write(_, __) {}
23+
}
24+
1225
void main() {
1326
group('HiveImpl', () {
1427
Future<HiveImpl> initHive() async {
@@ -316,5 +329,24 @@ void main() {
316329
await hive.close();
317330
});
318331
});
332+
333+
group('.resetAdapters()', () {
334+
test('returns normally', () async {
335+
final hive = await initHive();
336+
expect(hive.resetAdapters, returnsNormally);
337+
});
338+
339+
test('clears an adapter', () async {
340+
final hive = await initHive();
341+
final adapter = _TestAdapter(1);
342+
343+
expect(hive.isAdapterRegistered(adapter.typeId), isFalse);
344+
hive.registerAdapter(adapter);
345+
expect(hive.isAdapterRegistered(adapter.typeId), isTrue);
346+
347+
hive.resetAdapters();
348+
expect(hive.isAdapterRegistered(adapter.typeId), isFalse);
349+
});
350+
});
319351
});
320352
}

0 commit comments

Comments
 (0)