|
| 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' as emailjs; |
| 6 | + |
| 7 | +class MockClient extends Mock implements http.Client {} |
| 8 | + |
| 9 | +class FakeUri extends Fake implements Uri {} |
| 10 | + |
| 11 | +class MockStorage extends Mock implements emailjs.StorageProvider {} |
| 12 | + |
| 13 | +initMockRequest({int? status = 200}) { |
| 14 | + final mockHttpClient = MockClient(); |
| 15 | + when(() => mockHttpClient.post( |
| 16 | + any(), |
| 17 | + headers: any(named: 'headers'), |
| 18 | + body: any(named: 'body'), |
| 19 | + )).thenAnswer((_) async => http.Response('OK', status!)); |
| 20 | + |
| 21 | + return mockHttpClient; |
| 22 | +} |
| 23 | + |
| 24 | +void main() { |
| 25 | + setUpAll(() { |
| 26 | + registerFallbackValue(FakeUri()); |
| 27 | + }); |
| 28 | + |
| 29 | + setUp(() async { |
| 30 | + final Map<String, Object> values = <String, Object>{}; |
| 31 | + SharedPreferences.setMockInitialValues(values); |
| 32 | + }); |
| 33 | + |
| 34 | + group('send method', () { |
| 35 | + test('should call the send method and fail on http error', () async { |
| 36 | + final mockHttpClient = initMockRequest(status: 400); |
| 37 | + |
| 38 | + emailjs.init( |
| 39 | + const emailjs.Options( |
| 40 | + publicKey: 'LC2JWGTestKeySomething', |
| 41 | + privateKey: 'PrKeyTestKeySomething', |
| 42 | + ), |
| 43 | + mockHttpClient, |
| 44 | + ); |
| 45 | + |
| 46 | + try { |
| 47 | + final response = await emailjs.send( |
| 48 | + 'default_service', |
| 49 | + 'my_test_template', |
| 50 | + ); |
| 51 | + expect(response, isNull); |
| 52 | + } catch (error) { |
| 53 | + expect('$error', '[400] OK'); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + test('should call the send method and fail on http error as future', () async { |
| 58 | + final mockHttpClient = initMockRequest(status: 400); |
| 59 | + |
| 60 | + emailjs.init( |
| 61 | + const emailjs.Options( |
| 62 | + publicKey: 'LC2JWGTestKeySomething', |
| 63 | + privateKey: 'PrKeyTestKeySomething', |
| 64 | + ), |
| 65 | + mockHttpClient, |
| 66 | + ); |
| 67 | + |
| 68 | + emailjs.send( |
| 69 | + 'default_service', |
| 70 | + 'my_test_template', |
| 71 | + ).then((response) { |
| 72 | + expect(response, isNull); |
| 73 | + }).catchError((error) { |
| 74 | + expect('$error', '[400] OK'); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + test('should call the init and the send method successfully', () async { |
| 79 | + final mockHttpClient = initMockRequest(); |
| 80 | + |
| 81 | + emailjs.init( |
| 82 | + const emailjs.Options( |
| 83 | + publicKey: 'LC2JWGTestKeySomething', |
| 84 | + privateKey: 'PrKeyTestKeySomething', |
| 85 | + ), |
| 86 | + mockHttpClient, |
| 87 | + ); |
| 88 | + |
| 89 | + try { |
| 90 | + final response = await emailjs.send( |
| 91 | + 'default_service', |
| 92 | + 'my_test_template', |
| 93 | + ); |
| 94 | + expect('$response', '[200] OK'); |
| 95 | + } catch (error) { |
| 96 | + expect(error, isNull); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + test('should call the init and the send method successfully as future', () { |
| 101 | + final mockHttpClient = initMockRequest(); |
| 102 | + |
| 103 | + emailjs.init( |
| 104 | + const emailjs.Options( |
| 105 | + publicKey: 'LC2JWGTestKeySomething', |
| 106 | + privateKey: 'PrKeyTestKeySomething', |
| 107 | + ), |
| 108 | + mockHttpClient, |
| 109 | + ); |
| 110 | + |
| 111 | + emailjs.send( |
| 112 | + 'default_service', |
| 113 | + 'my_test_template', |
| 114 | + ).then((response) { |
| 115 | + expect('$response', '[200] OK'); |
| 116 | + }).catchError((error) { |
| 117 | + expect(error, isNull); |
| 118 | + }); |
| 119 | + }); |
| 120 | + }); |
| 121 | +} |
0 commit comments