Skip to content

Commit 0bd25e6

Browse files
committed
LimitRate: add tests
1 parent 51208c9 commit 0bd25e6

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import 'package:emailjs/src/models/default_storage.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:shared_preferences/shared_preferences.dart';
4+
5+
import 'package:emailjs/emailjs.dart';
6+
import 'package:emailjs/src/utils/is_limit_rate_hit.dart';
7+
8+
void main() {
9+
setUp(() async {
10+
final Map<String, Object> values = <String, Object>{};
11+
SharedPreferences.setMockInitialValues(values);
12+
});
13+
14+
group('limit rate is disabled', () {
15+
test('empty limit rate options', () async {
16+
DefaultStorage storage = DefaultStorage();
17+
const limitRate = LimitRate();
18+
19+
expect(await isLimitRateHit(limitRate, storage), false);
20+
});
21+
22+
test('throttle is 0', () async {
23+
DefaultStorage storage = DefaultStorage();
24+
const limitRate = LimitRate(throttle: 0);
25+
26+
expect(await isLimitRateHit(limitRate, storage), false);
27+
});
28+
29+
test('no record', () async {
30+
DefaultStorage storage = DefaultStorage();
31+
const limitRate = LimitRate(throttle: 1000, id: 'app');
32+
33+
expect(await isLimitRateHit(limitRate, storage), false);
34+
});
35+
36+
test('no hit limit', () async {
37+
DefaultStorage storage = DefaultStorage();
38+
const limitRate = LimitRate(throttle: 100, id: 'app');
39+
40+
expect(await isLimitRateHit(limitRate, storage), false);
41+
42+
await Future.delayed(const Duration(milliseconds: 150));
43+
44+
expect(await isLimitRateHit(limitRate, storage), false);
45+
});
46+
47+
test('not same page or ID', () async {
48+
DefaultStorage storage = DefaultStorage();
49+
LimitRate limitRate = const LimitRate(throttle: 100, id: 'app');
50+
51+
expect(await isLimitRateHit(limitRate, storage), false);
52+
53+
limitRate = const LimitRate(throttle: 100, id: 'new-app');
54+
55+
expect(await isLimitRateHit(limitRate, storage), false);
56+
});
57+
});
58+
59+
group('limit rate is enabled', () {
60+
test('hit limit', () async {
61+
DefaultStorage storage = DefaultStorage();
62+
const limitRate = LimitRate(throttle: 100, id: 'app');
63+
64+
expect(await isLimitRateHit(limitRate, storage), false);
65+
expect(await isLimitRateHit(limitRate, storage), true);
66+
});
67+
});
68+
}

0 commit comments

Comments
 (0)