This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 6
test: add tests for TransactionNotifier
#317
Merged
ethanwlee
merged 3 commits into
TBD54566975:main
from
victoreronmosele:tests-for-transaction-notifier
Oct 17, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
test/features/transaction/transaction_notifier_test.dart
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,114 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:didpay/features/did/did_provider.dart'; | ||
import 'package:didpay/features/tbdex/tbdex_service.dart'; | ||
import 'package:didpay/features/transaction/transaction.dart'; | ||
import 'package:didpay/features/transaction/transaction_notifier.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
import '../../helpers/mocks.dart'; | ||
import '../../helpers/riverpod_helpers.dart'; | ||
import '../../helpers/test_data.dart'; | ||
|
||
class Listener<T> extends Mock { | ||
void call(T? previous, T next); | ||
} | ||
|
||
void main() async { | ||
await TestData.initializeDids(); | ||
|
||
setUpAll(() { | ||
registerFallbackValue( | ||
const AsyncData<Transaction?>(null), | ||
); | ||
}); | ||
|
||
group('TransactionNotifier', () { | ||
final pfi = TestData.getPfi('did:dht:pfiDid'); | ||
const exchangeId = 'rfq_01ha835rhefwmagsknrrhvaa0k'; | ||
final parameters = TransactionProviderParameters(pfi, exchangeId); | ||
|
||
final did = TestData.aliceDid; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: let's move this to right below the |
||
|
||
test('should set the state to AsyncValue.data(transaction) on read', | ||
() async { | ||
final mockTbdexService = MockTbdexService(); | ||
final exchange = TestData.getExchange(); | ||
|
||
when( | ||
() => mockTbdexService.getExchange( | ||
did, | ||
pfi.did, | ||
exchangeId, | ||
), | ||
).thenAnswer((_) async => exchange); | ||
|
||
final container = createContainer( | ||
overrides: [ | ||
didProvider.overrideWith( | ||
(ref) => did, | ||
), | ||
tbdexServiceProvider.overrideWith( | ||
(ref) => mockTbdexService, | ||
), | ||
], | ||
); | ||
Comment on lines
+43
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice use of the |
||
|
||
final listener = Listener<AsyncValue<void>>(); | ||
|
||
final transactionProviderListenable = transactionProvider(parameters); | ||
|
||
container.listen(transactionProviderListenable, listener.call); | ||
|
||
await container.read(transactionProviderListenable.future); | ||
|
||
verify( | ||
() => listener( | ||
const AsyncLoading<Transaction?>(), | ||
any(that: isA<AsyncData<Transaction?>>()), | ||
), | ||
); | ||
}); | ||
|
||
test('should set the state to AsyncValue.error when error occurs', | ||
() async { | ||
final mockTbdexService = MockTbdexService(); | ||
|
||
when( | ||
() => mockTbdexService.getExchange( | ||
did, | ||
pfi.did, | ||
exchangeId, | ||
), | ||
).thenThrow(Exception('Error fetching exchange')); | ||
|
||
final container = createContainer( | ||
overrides: [ | ||
didProvider.overrideWith( | ||
(ref) => did, | ||
), | ||
tbdexServiceProvider.overrideWith( | ||
(ref) => mockTbdexService, | ||
), | ||
], | ||
); | ||
|
||
final listener = Listener<AsyncValue<void>>(); | ||
|
||
final transactionProviderListenable = transactionProvider(parameters); | ||
|
||
container.listen(transactionProviderListenable, listener.call); | ||
|
||
unawaited(await container.read(transactionProviderListenable.future)); | ||
|
||
verify( | ||
() => listener( | ||
any(that: isA<AsyncError>()), | ||
const AsyncData<Transaction?>(null), | ||
), | ||
); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think we can just move this to
mocks.dart