From 459c1f17fb17fdc9fbb77ecff3e6a8e42a1a23d7 Mon Sep 17 00:00:00 2001 From: Victor Eronmosele Date: Thu, 24 Oct 2024 22:42:31 +0100 Subject: [PATCH] test: add tests for `TbdexOrderInstructionsNotifier` (#325) --- ...bdex_order_instructions_notifier_test.dart | 75 +++++++++++++++++++ test/helpers/test_data.dart | 16 ++++ 2 files changed, 91 insertions(+) create mode 100644 test/features/tbdex/tbdex_order_instructions_notifier_test.dart diff --git a/test/features/tbdex/tbdex_order_instructions_notifier_test.dart b/test/features/tbdex/tbdex_order_instructions_notifier_test.dart new file mode 100644 index 00000000..e85a2bf5 --- /dev/null +++ b/test/features/tbdex/tbdex_order_instructions_notifier_test.dart @@ -0,0 +1,75 @@ +import 'package:didpay/features/did/did_provider.dart'; +import 'package:didpay/features/tbdex/tbdex_order_instructions_notifier.dart'; +import 'package:didpay/features/tbdex/tbdex_service.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; + +import '../../helpers/mocks.dart'; +import '../../helpers/riverpod_helpers.dart'; +import '../../helpers/test_data.dart'; + +Future main() async { + await TestData.initializeDids(); + + final did = TestData.aliceDid; + const pfiDid = '123'; + const exchangeId = 'rfq_01ha835rhefwmagsknrrhvaa0k'; + + group('TbdexOrderInstructionsNotifier`', () { + group('startPolling', () { + test('should return OrderInstructions if successful', () async { + final mockTbdexService = MockTbdexService(); + final orderInstruction = TestData.getOrderInstructions(); + + when(() => mockTbdexService.getExchange(did, pfiDid, exchangeId)) + .thenAnswer((_) async => [orderInstruction]); + + final container = createContainer( + overrides: [ + tbdexServiceProvider.overrideWith( + (ref) => mockTbdexService, + ), + didProvider.overrideWith((ref) => did), + ], + ); + + final tbdexOrderInstructionsNotifier = + container.read(orderInstructionsProvider.notifier); + + final result = await tbdexOrderInstructionsNotifier.startPolling( + pfiDid, + exchangeId, + ); + + expect(result, orderInstruction); + }); + + test('should throw an Exception if not successful', () async { + final mockTbdexService = MockTbdexService(); + + when(() => mockTbdexService.getExchange(did, pfiDid, exchangeId)) + .thenThrow(Exception('Error')); + + final container = createContainer( + overrides: [ + tbdexServiceProvider.overrideWith( + (ref) => mockTbdexService, + ), + didProvider.overrideWith((ref) => did), + ], + ); + + final tbdexOrderInstructionsNotifier = + container.read(orderInstructionsProvider.notifier); + + expect( + () => tbdexOrderInstructionsNotifier.startPolling( + pfiDid, + exchangeId, + ), + throwsA(isA()), + ); + }); + }); + }); +} diff --git a/test/helpers/test_data.dart b/test/helpers/test_data.dart index aa32816f..0b4700a4 100644 --- a/test/helpers/test_data.dart +++ b/test/helpers/test_data.dart @@ -128,6 +128,22 @@ class TestData { TypeId.generate(MessageKind.rfq.name), ); + static OrderInstructions getOrderInstructions() => OrderInstructions.create( + pfiDid.uri, + aliceDid.uri, + TypeId.generate(MessageKind.rfq.name), + OrderInstructionsData( + payin: PaymentInstruction( + link: 'https://payin.link', + instruction: 'Pay in instruction', + ), + payout: PaymentInstruction( + link: 'https://payout.link', + instruction: 'Pay out instruction', + ), + ), + ); + static JsonSchema paymentDetailsSchema() => JsonSchema.create( jsonDecode(r''' {