Skip to content

Commit 99fdec6

Browse files
author
Sergey Khomushin
committed
emailjs_test: add testing for library
1 parent 880b483 commit 99fdec6

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

test/emailjs_test.dart

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:mocktail/mocktail.dart';
3+
import 'package:http/http.dart' as http;
4+
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+
void main() {
12+
setUpAll(() {
13+
registerFallbackValue(FakeUri());
14+
});
15+
16+
group('required params validator', () {
17+
test('should send method and fail on the public key', () {
18+
expect(
19+
() => EmailJS.send('default_service', 'my_test_template'),
20+
throwsA(startsWith('The public key is required')),
21+
);
22+
});
23+
24+
test('should send method and fail on the service ID', () {
25+
EmailJS.init('LC2JWGTestKeySomething');
26+
27+
expect(
28+
() => EmailJS.send('', 'my_test_template'),
29+
throwsA(startsWith('The service ID is required')),
30+
);
31+
});
32+
33+
test('should send method and fail on the template ID', () {
34+
EmailJS.init('LC2JWGTestKeySomething');
35+
36+
expect(
37+
() => EmailJS.send('default_service', ''),
38+
throwsA(startsWith('The template ID is required')),
39+
);
40+
});
41+
});
42+
43+
group('EmailJS.send method', () {
44+
test('should init and send method successfully', () async {
45+
final mockHttpClient = MockClient();
46+
47+
when(() => mockHttpClient.post(
48+
any(),
49+
headers: any(named: 'headers'),
50+
body: any(named: 'body'),
51+
)).thenAnswer((_) async => http.Response('OK', 200));
52+
53+
EmailJS.init('LC2JWGTestKeySomething', null, mockHttpClient);
54+
55+
try {
56+
final result = await EmailJS.send(
57+
'default_service',
58+
'my_test_template',
59+
);
60+
expect(result, equals('OK'));
61+
} catch (error) {
62+
expect(error, isNull);
63+
}
64+
});
65+
66+
test('should send method successfully with 4 params', () async {
67+
final mockHttpClient = MockClient();
68+
69+
when(() => mockHttpClient.post(
70+
any(),
71+
headers: any(named: 'headers'),
72+
body: any(named: 'body'),
73+
)).thenAnswer((_) async => http.Response('OK', 200));
74+
75+
// pass the mock http client
76+
EmailJS.init('', null, mockHttpClient);
77+
78+
try {
79+
final result = await EmailJS.send(
80+
'default_service',
81+
'my_test_template',
82+
null,
83+
'LC2JWGTestKeySomething',
84+
);
85+
expect(result, equals('OK'));
86+
} catch (error) {
87+
expect(error, isNull);
88+
}
89+
});
90+
91+
test('should send method and fail', () async {
92+
final mockHttpClient = MockClient();
93+
94+
when(() => mockHttpClient.post(
95+
any(),
96+
headers: any(named: 'headers'),
97+
body: any(named: 'body'),
98+
)).thenAnswer((_) async => http.Response('The Public Key is required', 403));
99+
100+
// pass the mock http client
101+
EmailJS.init('LC2JWGTestKeySomething', null, mockHttpClient);
102+
103+
try {
104+
final result = await EmailJS.send(
105+
'default_service',
106+
'my_test_template',
107+
);
108+
expect(result, isNull);
109+
} catch (error) {
110+
expect(error, equals('The Public Key is required'));
111+
}
112+
});
113+
});
114+
}

0 commit comments

Comments
 (0)