1
1
// test/src/ht_email_repository_test.dart
2
2
import 'package:ht_email_client/ht_email_client.dart' ;
3
3
import 'package:ht_email_repository/ht_email_repository.dart' ;
4
- import 'package:ht_shared/ht_shared.dart' ; // For HtHttpException and subtypes
4
+ import 'package:ht_shared/ht_shared.dart' ;
5
5
import 'package:mocktail/mocktail.dart' ;
6
6
import 'package:test/test.dart' ;
7
7
@@ -15,59 +15,56 @@ void main() {
15
15
16
16
const testEmail
= '[email protected] ' ;
17
17
const testOtpCode = '123456' ;
18
+ const testTemplateId = 'd-otp-template' ;
18
19
19
20
setUp (() {
20
21
mockEmailClient = MockHtEmailClient ();
21
22
emailRepository = HtEmailRepository (emailClient: mockEmailClient);
22
-
23
- // Register fallback values for any() matchers if needed,
24
- // especially for parameters with default values or complex types.
25
- // Example: registerFallbackValue(Uri.parse('http://example.com'));
26
23
});
27
24
28
- // Teardown can be used to reset mocks after each test if necessary
29
- // tearDown(() {
30
- // reset(mockEmailClient);
31
- // });
32
-
33
25
test ('can be instantiated' , () {
34
26
expect (HtEmailRepository (emailClient: MockHtEmailClient ()), isNotNull);
35
27
});
36
28
37
29
group ('sendOtpEmail' , () {
38
- test ('calls sendOtpEmail on client successfully' , () async {
30
+ test (
31
+ 'calls sendTransactionalEmail on client with correct data successfully' ,
32
+ () async {
39
33
// Arrange
40
34
when (
41
- () => mockEmailClient.sendOtpEmail (
35
+ () => mockEmailClient.sendTransactionalEmail (
42
36
recipientEmail: any (named: 'recipientEmail' ),
43
- otpCode: any (named: 'otpCode' ),
37
+ templateId: any (named: 'templateId' ),
38
+ templateData: any (named: 'templateData' ),
44
39
),
45
40
).thenAnswer ((_) async {}); // Simulate successful void return
46
41
47
42
// Act
48
43
await emailRepository.sendOtpEmail (
49
44
recipientEmail: testEmail,
50
45
otpCode: testOtpCode,
46
+ templateId: testTemplateId,
51
47
);
52
48
53
49
// Assert
54
50
verify (
55
- () => mockEmailClient.sendOtpEmail (
51
+ () => mockEmailClient.sendTransactionalEmail (
56
52
recipientEmail: testEmail,
57
- otpCode: testOtpCode,
53
+ templateId: testTemplateId,
54
+ templateData: {'otp_code' : testOtpCode},
58
55
),
59
56
).called (1 );
60
- // Verify no other methods were called on the mock
61
57
verifyNoMoreInteractions (mockEmailClient);
62
58
});
63
59
64
- test ('propagates NetworkException from client' , () async {
60
+ test ('propagates HtHttpException from client' , () async {
65
61
// Arrange
66
62
const exception = NetworkException ();
67
63
when (
68
- () => mockEmailClient.sendOtpEmail (
64
+ () => mockEmailClient.sendTransactionalEmail (
69
65
recipientEmail: any (named: 'recipientEmail' ),
70
- otpCode: any (named: 'otpCode' ),
66
+ templateId: any (named: 'templateId' ),
67
+ templateData: any (named: 'templateData' ),
71
68
),
72
69
).thenThrow (exception); // Simulate client throwing the exception
73
70
@@ -76,106 +73,21 @@ void main() {
76
73
() => emailRepository.sendOtpEmail (
77
74
recipientEmail: testEmail,
78
75
otpCode: testOtpCode,
76
+ templateId: testTemplateId,
79
77
),
80
- throwsA (exception), // Expect the exact exception to be propagated
78
+ throwsA (isA < HtHttpException >()),
81
79
);
82
80
83
81
// Verify the client method was called
84
82
verify (
85
- () => mockEmailClient.sendOtpEmail (
86
- recipientEmail: testEmail,
87
- otpCode: testOtpCode,
88
- ),
89
- ).called (1 );
90
- verifyNoMoreInteractions (mockEmailClient);
91
- });
92
-
93
- test ('propagates InvalidInputException from client' , () async {
94
- // Arrange
95
- const exception = InvalidInputException ('Invalid email format' );
96
- when (
97
- () => mockEmailClient.sendOtpEmail (
98
- recipientEmail: any (named: 'recipientEmail' ),
99
- otpCode: any (named: 'otpCode' ),
100
- ),
101
- ).thenThrow (exception);
102
-
103
- // Act & Assert
104
- expect (
105
- () => emailRepository.sendOtpEmail (
106
- recipientEmail: testEmail,
107
- otpCode: testOtpCode,
108
- ),
109
- throwsA (exception),
110
- );
111
-
112
- // Verify
113
- verify (
114
- () => mockEmailClient.sendOtpEmail (
115
- recipientEmail: testEmail,
116
- otpCode: testOtpCode,
117
- ),
118
- ).called (1 );
119
- verifyNoMoreInteractions (mockEmailClient);
120
- });
121
-
122
- test ('propagates ServerException from client' , () async {
123
- // Arrange
124
- const exception = ServerException ('Email service unavailable' );
125
- when (
126
- () => mockEmailClient.sendOtpEmail (
127
- recipientEmail: any (named: 'recipientEmail' ),
128
- otpCode: any (named: 'otpCode' ),
129
- ),
130
- ).thenThrow (exception);
131
-
132
- // Act & Assert
133
- expect (
134
- () => emailRepository.sendOtpEmail (
135
- recipientEmail: testEmail,
136
- otpCode: testOtpCode,
137
- ),
138
- throwsA (exception),
139
- );
140
-
141
- // Verify
142
- verify (
143
- () => mockEmailClient.sendOtpEmail (
144
- recipientEmail: testEmail,
145
- otpCode: testOtpCode,
146
- ),
147
- ).called (1 );
148
- verifyNoMoreInteractions (mockEmailClient);
149
- });
150
-
151
- test ('propagates OperationFailedException from client' , () async {
152
- // Arrange
153
- const exception = OperationFailedException ('Unknown sending error' );
154
- when (
155
- () => mockEmailClient.sendOtpEmail (
156
- recipientEmail: any (named: 'recipientEmail' ),
157
- otpCode: any (named: 'otpCode' ),
158
- ),
159
- ).thenThrow (exception);
160
-
161
- // Act & Assert
162
- expect (
163
- () => emailRepository.sendOtpEmail (
164
- recipientEmail: testEmail,
165
- otpCode: testOtpCode,
166
- ),
167
- throwsA (exception),
168
- );
169
-
170
- // Verify
171
- verify (
172
- () => mockEmailClient.sendOtpEmail (
83
+ () => mockEmailClient.sendTransactionalEmail (
173
84
recipientEmail: testEmail,
174
- otpCode: testOtpCode,
85
+ templateId: testTemplateId,
86
+ templateData: {'otp_code' : testOtpCode},
175
87
),
176
88
).called (1 );
177
89
verifyNoMoreInteractions (mockEmailClient);
178
90
});
179
- }); // End group 'sendOtpEmail'
180
- }); // End group 'HtEmailRepository'
181
- } // End main
91
+ });
92
+ });
93
+ }
0 commit comments