|
| 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