Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

test: add tests for TransactionNotifier #317

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions test/features/transaction/transaction_notifier_test.dart
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);
}
Copy link
Contributor

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


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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's move this to right below the await TestData.initializeDids()


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice use of the createContainer() helper!


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),
),
);
});
});
}
Loading