Skip to content

Commit 9b099c9

Browse files
committed
add id validation and tests
1 parent 0bd25e6 commit 9b099c9

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

lib/src/utils/is_limit_rate_hit.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ Future<bool> isLimitRateHit(
1919
return false;
2020
}
2121

22-
validateLimitRateParams(options.throttle!);
23-
2422
final id = options.id ?? 'default';
23+
24+
validateLimitRateParams(options.throttle!, id);
25+
2526
final leftTime = await getLeftTime(id, options.throttle!, storage);
2627

2728
if (leftTime > 0) {
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
void validateLimitRateParams(int throttle) {
1+
void validateLimitRateParams(int throttle, [String? id]) {
22
if (throttle < 0) {
33
throw 'The LimitRate throttle has to be a positive number';
44
}
5+
6+
if (id != null && id.isEmpty) {
7+
throw 'The LimitRate ID has to be a non-empty string';
8+
}
59
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'package:emailjs/src/utils/validate_limit_rate_params.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
group('should fail', () {
6+
test('throttle is a negative number', () {
7+
expect(
8+
() => validateLimitRateParams(-1000),
9+
throwsA(startsWith('The LimitRate throttle has to be a positive number')),
10+
);
11+
});
12+
13+
test('ID is empty', () {
14+
expect(
15+
() => validateLimitRateParams(1000, ''),
16+
throwsA(startsWith('The LimitRate ID has to be a non-empty string')),
17+
);
18+
});
19+
});
20+
21+
group('should successfully pass the validation', () {
22+
test('throttle is a positive number', () {
23+
expect(
24+
() => validateLimitRateParams(1000),
25+
returnsNormally,
26+
);
27+
});
28+
29+
test('ID is valid string', () {
30+
expect(
31+
() => validateLimitRateParams(1000, 'app'),
32+
returnsNormally,
33+
);
34+
});
35+
});
36+
}

0 commit comments

Comments
 (0)