Skip to content

Commit 5206d5e

Browse files
committed
test: refactor HtEmailRepository tests
- Replaced `sendOtpEmail` with `sendTransactionalEmail` - Updated test cases to reflect changes - Removed redundant code and comments - Improved exception handling in tests - Added templateId and templateData parameters
1 parent 7c60f0a commit 5206d5e

File tree

1 file changed

+24
-112
lines changed

1 file changed

+24
-112
lines changed
Lines changed: 24 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// test/src/ht_email_repository_test.dart
22
import 'package:ht_email_client/ht_email_client.dart';
33
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';
55
import 'package:mocktail/mocktail.dart';
66
import 'package:test/test.dart';
77

@@ -15,59 +15,56 @@ void main() {
1515

1616
const testEmail = '[email protected]';
1717
const testOtpCode = '123456';
18+
const testTemplateId = 'd-otp-template';
1819

1920
setUp(() {
2021
mockEmailClient = MockHtEmailClient();
2122
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'));
2623
});
2724

28-
// Teardown can be used to reset mocks after each test if necessary
29-
// tearDown(() {
30-
// reset(mockEmailClient);
31-
// });
32-
3325
test('can be instantiated', () {
3426
expect(HtEmailRepository(emailClient: MockHtEmailClient()), isNotNull);
3527
});
3628

3729
group('sendOtpEmail', () {
38-
test('calls sendOtpEmail on client successfully', () async {
30+
test(
31+
'calls sendTransactionalEmail on client with correct data successfully',
32+
() async {
3933
// Arrange
4034
when(
41-
() => mockEmailClient.sendOtpEmail(
35+
() => mockEmailClient.sendTransactionalEmail(
4236
recipientEmail: any(named: 'recipientEmail'),
43-
otpCode: any(named: 'otpCode'),
37+
templateId: any(named: 'templateId'),
38+
templateData: any(named: 'templateData'),
4439
),
4540
).thenAnswer((_) async {}); // Simulate successful void return
4641

4742
// Act
4843
await emailRepository.sendOtpEmail(
4944
recipientEmail: testEmail,
5045
otpCode: testOtpCode,
46+
templateId: testTemplateId,
5147
);
5248

5349
// Assert
5450
verify(
55-
() => mockEmailClient.sendOtpEmail(
51+
() => mockEmailClient.sendTransactionalEmail(
5652
recipientEmail: testEmail,
57-
otpCode: testOtpCode,
53+
templateId: testTemplateId,
54+
templateData: {'otp_code': testOtpCode},
5855
),
5956
).called(1);
60-
// Verify no other methods were called on the mock
6157
verifyNoMoreInteractions(mockEmailClient);
6258
});
6359

64-
test('propagates NetworkException from client', () async {
60+
test('propagates HtHttpException from client', () async {
6561
// Arrange
6662
const exception = NetworkException();
6763
when(
68-
() => mockEmailClient.sendOtpEmail(
64+
() => mockEmailClient.sendTransactionalEmail(
6965
recipientEmail: any(named: 'recipientEmail'),
70-
otpCode: any(named: 'otpCode'),
66+
templateId: any(named: 'templateId'),
67+
templateData: any(named: 'templateData'),
7168
),
7269
).thenThrow(exception); // Simulate client throwing the exception
7370

@@ -76,106 +73,21 @@ void main() {
7673
() => emailRepository.sendOtpEmail(
7774
recipientEmail: testEmail,
7875
otpCode: testOtpCode,
76+
templateId: testTemplateId,
7977
),
80-
throwsA(exception), // Expect the exact exception to be propagated
78+
throwsA(isA<HtHttpException>()),
8179
);
8280

8381
// Verify the client method was called
8482
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(
17384
recipientEmail: testEmail,
174-
otpCode: testOtpCode,
85+
templateId: testTemplateId,
86+
templateData: {'otp_code': testOtpCode},
17587
),
17688
).called(1);
17789
verifyNoMoreInteractions(mockEmailClient);
17890
});
179-
}); // End group 'sendOtpEmail'
180-
}); // End group 'HtEmailRepository'
181-
} // End main
91+
});
92+
});
93+
}

0 commit comments

Comments
 (0)