Skip to content

Commit 89f552b

Browse files
committed
test methods
1 parent 1106789 commit 89f552b

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed

test/methods/init_test.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'package:emailjs/emailjs.dart';
2+
import 'package:emailjs/src/store/store.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
void main() {
6+
7+
test('should call the init method with empty options and get default values', () {
8+
init(const Options());
9+
10+
final newStore = Store();
11+
12+
expect(store.host, newStore.host);
13+
expect(store.publicKey, newStore.publicKey);
14+
expect(store.privateKey, newStore.privateKey);
15+
expect(store.blockList, newStore.blockList);
16+
expect(store.limitRate, newStore.limitRate);
17+
expect(store.storeProvider, newStore.storeProvider);
18+
expect(store.client, newStore.client);
19+
});
20+
21+
test('should call the init method with custom options', () {
22+
init(const Options(
23+
publicKey: 'C2JWGTestKeySomething',
24+
blockList: BlockList(
25+
list: ['block@email.com'],
26+
),
27+
limitRate: LimitRate(
28+
throttle: 10000,
29+
),
30+
));
31+
32+
33+
final newStore = Store();
34+
newStore.publicKey = 'C2JWGTestKeySomething';
35+
36+
newStore.blockList = const BlockList(
37+
list: ['block@email.com'],
38+
);
39+
40+
newStore.limitRate = const LimitRate(
41+
throttle: 10000,
42+
);
43+
44+
expect(store.host, newStore.host);
45+
expect(store.publicKey, newStore.publicKey);
46+
expect(store.privateKey, newStore.privateKey);
47+
expect(store.blockList, newStore.blockList);
48+
expect(store.limitRate, newStore.limitRate);
49+
expect(store.storeProvider, newStore.storeProvider);
50+
expect(store.client, newStore.client);
51+
});
52+
}

test/methods/send_test.dart

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import 'package:mocktail/mocktail.dart';
2+
import 'package:http/http.dart' as http;
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:shared_preferences/shared_preferences.dart';
5+
import 'package:emailjs/emailjs.dart';
6+
7+
class MockClient extends Mock implements http.Client {}
8+
9+
class FakeUri extends Fake implements Uri {}
10+
11+
class MockStorage extends Mock implements StorageProvider {}
12+
13+
void main() {
14+
setUpAll(() {
15+
registerFallbackValue(FakeUri());
16+
17+
final mockHttpClient = MockClient();
18+
when(() => mockHttpClient.post(
19+
any(),
20+
headers: any(named: 'headers'),
21+
body: any(named: 'body'),
22+
)).thenAnswer((_) async => http.Response('OK', 200));
23+
init(const Options(), mockHttpClient);
24+
});
25+
26+
setUp(() async {
27+
final Map<String, Object> values = <String, Object>{};
28+
SharedPreferences.setMockInitialValues(values);
29+
});
30+
31+
test('should call the send method and fail on the public key', () {
32+
expect(
33+
() => send('default_service', 'my_test_template'),
34+
throwsA(startsWith('The public key is required')),
35+
);
36+
});
37+
38+
test('should call the send method and fail on the service ID', () {
39+
expect(
40+
() => send('', 'my_test_template', null, const Options(
41+
publicKey: 'C2JWGTestKeySomething',
42+
)),
43+
throwsA(startsWith('The service ID is required')),
44+
);
45+
});
46+
47+
test('should call the send method and fail on the template ID', () {
48+
expect(
49+
() => send('default_service', '', null, const Options(
50+
publicKey: 'C2JWGTestKeySomething',
51+
)),
52+
throwsA(startsWith('The template ID is required')),
53+
);
54+
});
55+
56+
test('should call the send method and fail on blocklist', () async {
57+
try {
58+
final response = await send('default_service', 'my_test_template', {
59+
'email': 'bar@emailjs.com',
60+
}, const Options(
61+
publicKey: 'C2JWGTestKeySomething',
62+
blockList: BlockList(
63+
list: ['foo@emailjs.com', 'bar@emailjs.com'],
64+
watchVariable: 'email',
65+
),
66+
));
67+
68+
expect(response, isNull);
69+
} catch (error) {
70+
expect('$error', '[403] Forbidden');
71+
}
72+
});
73+
74+
test('should call the send method and fail on blocklist as future', () {
75+
send('default_service', 'my_test_template', {
76+
'email': 'bar@emailjs.com',
77+
}, const Options(
78+
publicKey: 'C2JWGTestKeySomething',
79+
blockList: BlockList(
80+
list: ['foo@emailjs.com', 'bar@emailjs.com'],
81+
watchVariable: 'email',
82+
),
83+
)).then((result) {
84+
expect(result, isNull);
85+
}).catchError((error) {
86+
expect('$error', '[403] Forbidden');
87+
});
88+
});
89+
90+
test('should call the send method and fail on limit rate', () async {
91+
sendEmail() {
92+
return send('default_service', 'my_test_template', null, const Options(
93+
publicKey: 'C2JWGTestKeySomething',
94+
limitRate: LimitRate(
95+
id: 'async-send',
96+
throttle: 100,
97+
),
98+
));
99+
}
100+
101+
try {
102+
final response = await sendEmail();
103+
expect('$response', '[200] OK');
104+
} catch (error) {
105+
expect(error, isNull);
106+
}
107+
108+
try {
109+
final response = await sendEmail();
110+
expect(response, isNull);
111+
} catch (error) {
112+
expect('$error', '[429] Too Many Requests');
113+
}
114+
});
115+
116+
test('should call the send method and fail on limit rate as future', () {
117+
sendEmail() {
118+
return send('default_service', 'my_test_template', null, const Options(
119+
publicKey: 'C2JWGTestKeySomething',
120+
limitRate: LimitRate(
121+
id: 'future-send',
122+
throttle: 1000,
123+
),
124+
));
125+
}
126+
127+
sendEmail().then((response) {
128+
expect('$response', '[200] OK');
129+
130+
sendEmail().then((response) {
131+
expect(response, isNull);
132+
}).catchError((error) {
133+
expect('$error', '[429] Too Many Requests');
134+
});
135+
}).catchError((error) {
136+
expect(error, isNull);
137+
});
138+
});
139+
140+
test('should call the send method successfully with 4 params', () async {
141+
try {
142+
final response = await send('default_service', 'my_test_template', {}, const Options(
143+
publicKey: 'C2JWGTestKeySomething',
144+
));
145+
146+
expect('$response', '[200] OK');
147+
} catch (error) {
148+
expect(error, isNull);
149+
}
150+
});
151+
152+
test('should call the send method successfully with 4 params as future', () {
153+
send('default_service', 'my_test_template', {}, const Options(
154+
publicKey: 'C2JWGTestKeySomething',
155+
)).then((response) {
156+
expect('$response', '[200] OK');
157+
}).catchError((error) {
158+
expect(error, isNull);
159+
});
160+
});
161+
}

0 commit comments

Comments
 (0)