Skip to content

Commit

Permalink
test: add tests for TransactionNotifier (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
victoreronmosele authored Oct 17, 2024
1 parent 0bc1feb commit fdf2352
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
109 changes: 109 additions & 0 deletions test/features/transaction/transaction_notifier_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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';

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<Transaction?>(null),
);
});

group('TransactionNotifier', () {
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<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),
),
);
});
});
}
4 changes: 4 additions & 0 deletions test/helpers/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@ class MockAccountBalanceNotifier
@override
FutureOr<AccountBalance?> build() async => accountBalance;
}

class Listener<T> extends Mock {
void call(T? previous, T next);
}

0 comments on commit fdf2352

Please sign in to comment.