From 24b4dcd69ea9eb82d4315d1191d5f64c7ef23990 Mon Sep 17 00:00:00 2001 From: Victor Eronmosele Date: Wed, 16 Oct 2024 06:12:39 +0100 Subject: [PATCH 1/3] test: add tests for `TransactionNotifier` --- .../transaction_notifier_test.dart | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 test/features/transaction/transaction_notifier_test.dart diff --git a/test/features/transaction/transaction_notifier_test.dart b/test/features/transaction/transaction_notifier_test.dart new file mode 100644 index 00000000..4f46801a --- /dev/null +++ b/test/features/transaction/transaction_notifier_test.dart @@ -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 extends Mock { + void call(T? previous, T next); +} + +void main() async { + await TestData.initializeDids(); + + setUpAll(() { + registerFallbackValue( + const AsyncData(null), + ); + }); + + group('TransactionNotifier', () { + final pfi = TestData.getPfi('did:dht:pfiDid'); + const exchangeId = 'rfq_01ha835rhefwmagsknrrhvaa0k'; + final parameters = TransactionProviderParameters(pfi, exchangeId); + + final did = TestData.aliceDid; + + 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, + ), + ], + ); + + final listener = Listener>(); + + final transactionProviderListenable = transactionProvider(parameters); + + container.listen(transactionProviderListenable, listener.call); + + await container.read(transactionProviderListenable.future); + + verify( + () => listener( + const AsyncLoading(), + any(that: isA>()), + ), + ); + }); + + 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>(); + + final transactionProviderListenable = transactionProvider(parameters); + + container.listen(transactionProviderListenable, listener.call); + + unawaited(await container.read(transactionProviderListenable.future)); + + verify( + () => listener( + any(that: isA()), + const AsyncData(null), + ), + ); + }); + }); +} From 2844b7bedd3979bdfc3172c976cd093bb2bfb458 Mon Sep 17 00:00:00 2001 From: Victor Eronmosele Date: Thu, 17 Oct 2024 00:49:05 +0100 Subject: [PATCH 2/3] chore: move mock Listener to mocks.dart --- test/features/transaction/transaction_notifier_test.dart | 4 ---- test/helpers/mocks.dart | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/features/transaction/transaction_notifier_test.dart b/test/features/transaction/transaction_notifier_test.dart index 4f46801a..b14bf3d2 100644 --- a/test/features/transaction/transaction_notifier_test.dart +++ b/test/features/transaction/transaction_notifier_test.dart @@ -12,10 +12,6 @@ import '../../helpers/mocks.dart'; import '../../helpers/riverpod_helpers.dart'; import '../../helpers/test_data.dart'; -class Listener extends Mock { - void call(T? previous, T next); -} - void main() async { await TestData.initializeDids(); diff --git a/test/helpers/mocks.dart b/test/helpers/mocks.dart index 64687fcb..c6a80003 100644 --- a/test/helpers/mocks.dart +++ b/test/helpers/mocks.dart @@ -79,3 +79,7 @@ class MockAccountBalanceNotifier @override FutureOr build() async => accountBalance; } + +class Listener extends Mock { + void call(T? previous, T next); +} From fb5e88cdabcbea7ee62101ac20b1d3ea41e33478 Mon Sep 17 00:00:00 2001 From: Victor Eronmosele Date: Thu, 17 Oct 2024 00:56:51 +0100 Subject: [PATCH 3/3] chore: move shared variables up to main function in transaction notifier test --- .../transaction/transaction_notifier_test.dart | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/test/features/transaction/transaction_notifier_test.dart b/test/features/transaction/transaction_notifier_test.dart index b14bf3d2..7b73e064 100644 --- a/test/features/transaction/transaction_notifier_test.dart +++ b/test/features/transaction/transaction_notifier_test.dart @@ -15,6 +15,11 @@ import '../../helpers/test_data.dart'; void main() async { await TestData.initializeDids(); + final pfi = TestData.getPfi('did:dht:pfiDid'); + const exchangeId = 'rfq_01ha835rhefwmagsknrrhvaa0k'; + final parameters = TransactionProviderParameters(pfi, exchangeId); + final did = TestData.aliceDid; + setUpAll(() { registerFallbackValue( const AsyncData(null), @@ -22,12 +27,6 @@ void main() async { }); group('TransactionNotifier', () { - final pfi = TestData.getPfi('did:dht:pfiDid'); - const exchangeId = 'rfq_01ha835rhefwmagsknrrhvaa0k'; - final parameters = TransactionProviderParameters(pfi, exchangeId); - - final did = TestData.aliceDid; - test('should set the state to AsyncValue.data(transaction) on read', () async { final mockTbdexService = MockTbdexService();