-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unit tests for wallets data providers, local storage (#138)
### Added unit tests for wallet-related providers and LocalStorage This PR adds unit tests for the following components: **currentWalletIdProvider:** - Verifies correct wallet ID selection based on existing data - Handles edge cases like non-existent IDs and empty wallet lists **currentWalletDataProvider:** - Ensures correct wallet data retrieval - Validates error handling for non-existent wallet IDs **walletByIdProvider:** - Confirms accurate wallet data retrieval by ID - Tests error cases for invalid IDs **WalletsDataNotifier:** - Validates initial state setup - Tests CRUD operations (add, update, delete) for wallets - Verifies exception handling for duplicate and non-existent wallets **SelectedWalletIdNotifier:** - Tests initialization from local storage - Verifies data persistence to local storage - Ensures proper reaction to local storage changes **LocalStorage:** - Comprehensive tests for various data types (bool, double, string, enum) - Validates default value handling and error cases
- Loading branch information
1 parent
77b8ceb
commit e51e49b
Showing
12 changed files
with
542 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:ice/app/features/wallet/model/wallet_data.dart'; | ||
import 'package:ice/app/features/wallets/providers/selected_wallet_id_provider.dart'; | ||
import 'package:ice/app/features/wallets/providers/wallets_data_provider.dart'; | ||
import 'package:ice/app/services/storage/local_storage.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
class Listener<T> extends Mock { | ||
void call(T? previous, T value); | ||
} | ||
|
||
class MockLocalStorage extends Mock implements LocalStorage {} | ||
|
||
class MockSelectedWalletIdNotifier extends Notifier<String?> | ||
with Mock | ||
implements SelectedWalletIdNotifier {} | ||
|
||
class MockWalletsDataNotifier extends Notifier<List<WalletData>> | ||
with Mock | ||
implements WalletsDataNotifier {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:ice/app/services/storage/local_storage.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
enum TestEnum { one, two, three } | ||
|
||
const testKey = 'testKey'; | ||
const testStringValue = 'testValue'; | ||
const testBoolValue = true; | ||
const testDoubleValue = 1.5; | ||
final testEnumValue = TestEnum.two; | ||
|
||
void main() { | ||
late LocalStorage localStorage; | ||
|
||
setUp(() async { | ||
SharedPreferences.setMockInitialValues({}); | ||
final prefs = await SharedPreferences.getInstance(); | ||
localStorage = LocalStorage(prefs); | ||
}); | ||
|
||
group('LocalStorage', () { | ||
test('setBool() and getBool()', () { | ||
localStorage.setBool(key: testKey, value: testBoolValue); | ||
|
||
expect(localStorage.getBool(testKey), testBoolValue); | ||
}); | ||
|
||
test('getBool() returns default value when key not found', () { | ||
expect( | ||
localStorage.getBool(testKey, defaultValue: !testBoolValue), | ||
!testBoolValue, | ||
); | ||
}); | ||
|
||
test('setDouble() and getDouble()', () { | ||
localStorage.setDouble(testKey, testDoubleValue); | ||
|
||
expect(localStorage.getDouble(testKey), testDoubleValue); | ||
}); | ||
|
||
test('getDouble() returns default value when key not found', () { | ||
expect( | ||
localStorage.getDouble(testKey, defaultValue: 0.0), | ||
0.0, | ||
); | ||
}); | ||
|
||
test('setString() and getString()', () { | ||
localStorage.setString(testKey, testStringValue); | ||
|
||
expect( | ||
localStorage.getString(testKey), | ||
testStringValue, | ||
); | ||
}); | ||
|
||
test('setEnum() and getEnum()', () { | ||
localStorage.setEnum(testKey, testEnumValue); | ||
|
||
expect( | ||
localStorage.getEnum( | ||
testKey, | ||
TestEnum.values, | ||
defaultValue: TestEnum.one, | ||
), | ||
testEnumValue, | ||
); | ||
}); | ||
|
||
test('getEnum() returns default value when key not found', () { | ||
expect( | ||
localStorage.getEnum( | ||
testKey, | ||
TestEnum.values, | ||
defaultValue: TestEnum.one, | ||
), | ||
TestEnum.one, | ||
); | ||
}); | ||
|
||
test('getEnum() returns default value when value is invalid', () { | ||
localStorage.setString(testKey, 'invalid'); | ||
|
||
expect( | ||
localStorage.getEnum( | ||
testKey, | ||
TestEnum.values, | ||
defaultValue: TestEnum.one, | ||
), | ||
TestEnum.one, | ||
); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:riverpod/riverpod.dart'; | ||
|
||
/// A testing utility which creates a [ProviderContainer] and automatically | ||
/// disposes it at the end of the test. | ||
ProviderContainer createContainer({ | ||
ProviderContainer? parent, | ||
List<Override> overrides = const [], | ||
List<ProviderObserver>? observers, | ||
}) { | ||
// Create a ProviderContainer, and optionally allow specifying parameters. | ||
final container = ProviderContainer( | ||
parent: parent, | ||
overrides: overrides, | ||
observers: observers, | ||
); | ||
|
||
// When the test ends, dispose the container. | ||
addTearDown(container.dispose); | ||
|
||
return container; | ||
} |
Oops, something went wrong.