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

Commit 459c1f1

Browse files
test: add tests for TbdexOrderInstructionsNotifier (#325)
1 parent e5ce33b commit 459c1f1

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import 'package:didpay/features/did/did_provider.dart';
2+
import 'package:didpay/features/tbdex/tbdex_order_instructions_notifier.dart';
3+
import 'package:didpay/features/tbdex/tbdex_service.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
import 'package:mocktail/mocktail.dart';
6+
7+
import '../../helpers/mocks.dart';
8+
import '../../helpers/riverpod_helpers.dart';
9+
import '../../helpers/test_data.dart';
10+
11+
Future<void> main() async {
12+
await TestData.initializeDids();
13+
14+
final did = TestData.aliceDid;
15+
const pfiDid = '123';
16+
const exchangeId = 'rfq_01ha835rhefwmagsknrrhvaa0k';
17+
18+
group('TbdexOrderInstructionsNotifier`', () {
19+
group('startPolling', () {
20+
test('should return OrderInstructions if successful', () async {
21+
final mockTbdexService = MockTbdexService();
22+
final orderInstruction = TestData.getOrderInstructions();
23+
24+
when(() => mockTbdexService.getExchange(did, pfiDid, exchangeId))
25+
.thenAnswer((_) async => [orderInstruction]);
26+
27+
final container = createContainer(
28+
overrides: [
29+
tbdexServiceProvider.overrideWith(
30+
(ref) => mockTbdexService,
31+
),
32+
didProvider.overrideWith((ref) => did),
33+
],
34+
);
35+
36+
final tbdexOrderInstructionsNotifier =
37+
container.read(orderInstructionsProvider.notifier);
38+
39+
final result = await tbdexOrderInstructionsNotifier.startPolling(
40+
pfiDid,
41+
exchangeId,
42+
);
43+
44+
expect(result, orderInstruction);
45+
});
46+
47+
test('should throw an Exception if not successful', () async {
48+
final mockTbdexService = MockTbdexService();
49+
50+
when(() => mockTbdexService.getExchange(did, pfiDid, exchangeId))
51+
.thenThrow(Exception('Error'));
52+
53+
final container = createContainer(
54+
overrides: [
55+
tbdexServiceProvider.overrideWith(
56+
(ref) => mockTbdexService,
57+
),
58+
didProvider.overrideWith((ref) => did),
59+
],
60+
);
61+
62+
final tbdexOrderInstructionsNotifier =
63+
container.read(orderInstructionsProvider.notifier);
64+
65+
expect(
66+
() => tbdexOrderInstructionsNotifier.startPolling(
67+
pfiDid,
68+
exchangeId,
69+
),
70+
throwsA(isA<Exception>()),
71+
);
72+
});
73+
});
74+
});
75+
}

test/helpers/test_data.dart

+16
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ class TestData {
128128
TypeId.generate(MessageKind.rfq.name),
129129
);
130130

131+
static OrderInstructions getOrderInstructions() => OrderInstructions.create(
132+
pfiDid.uri,
133+
aliceDid.uri,
134+
TypeId.generate(MessageKind.rfq.name),
135+
OrderInstructionsData(
136+
payin: PaymentInstruction(
137+
link: 'https://payin.link',
138+
instruction: 'Pay in instruction',
139+
),
140+
payout: PaymentInstruction(
141+
link: 'https://payout.link',
142+
instruction: 'Pay out instruction',
143+
),
144+
),
145+
);
146+
131147
static JsonSchema paymentDetailsSchema() => JsonSchema.create(
132148
jsonDecode(r'''
133149
{

0 commit comments

Comments
 (0)