|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | + |
1 | 4 | import 'package:flutter/services.dart'; |
2 | 5 | import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:get_test/utils/image_test_utils.dart'; |
3 | 7 | import 'package:mockito/annotations.dart'; |
4 | 8 | import 'package:mockito/mockito.dart'; |
5 | 9 | import 'package:http/http.dart' as http; |
6 | 10 | import 'package:sqflite_common_ffi/sqflite_ffi.dart'; |
7 | 11 | import 'package:taskwarrior/api_service.dart'; |
8 | 12 | import 'package:taskwarrior/app/utils/taskchampion/credentials_storage.dart'; |
9 | 13 |
|
10 | | -class MockHttpClient extends Mock implements http.Client {} |
| 14 | +import 'api_service_test.mocks.dart'; |
11 | 15 |
|
12 | 16 | class MockCredentialsStorage extends Mock implements CredentialsStorage {} |
13 | 17 |
|
14 | 18 | class MockMethodChannel extends Mock implements MethodChannel {} |
15 | 19 |
|
16 | | -@GenerateMocks([MockMethodChannel]) |
| 20 | +@GenerateMocks([MockMethodChannel, http.Client]) |
17 | 21 | void main() { |
18 | 22 | TestWidgetsFlutterBinding.ensureInitialized(); |
19 | 23 |
|
20 | 24 | databaseFactory = databaseFactoryFfi; |
| 25 | + MockClient mockClient = MockClient(); |
21 | 26 |
|
22 | 27 | setUpAll(() { |
23 | 28 | sqfliteFfiInit(); |
24 | 29 | }); |
25 | 30 |
|
26 | | - setUp(() {}); |
27 | | - |
28 | 31 | group('Tasks model', () { |
29 | 32 | test('fromJson creates Tasks object', () { |
30 | 33 | final json = { |
@@ -84,11 +87,25 @@ void main() { |
84 | 87 | }); |
85 | 88 |
|
86 | 89 | group('fetchTasks', () { |
87 | | - test('fetchTasks throws exception on failure', () async { |
| 90 | + test('Fetch data successfully', () async { |
| 91 | + final responseJson = jsonEncode({'data': 'Mock data'}); |
| 92 | + when(mockClient.get( |
| 93 | + Uri.parse( |
| 94 | + '$baseUrl/tasks?email=email&origin=$origin&UUID=123&encryptionSecret=secret'), |
| 95 | + headers: { |
| 96 | + "Content-Type": "application/json", |
| 97 | + })).thenAnswer((_) async => http.Response(responseJson, 200)); |
| 98 | + |
| 99 | + final result = await fetchTasks('123', 'secret'); |
| 100 | + |
| 101 | + expect(result, isA<List<Tasks>>()); |
| 102 | + }); |
| 103 | + |
| 104 | + test('fetchTasks returns empty array', () async { |
88 | 105 | const uuid = '123'; |
89 | 106 | const encryptionSecret = 'secret'; |
90 | 107 |
|
91 | | - expect(() => fetchTasks(uuid, encryptionSecret), throwsException); |
| 108 | + expect(await fetchTasks(uuid, encryptionSecret), isEmpty); |
92 | 109 | }); |
93 | 110 | }); |
94 | 111 |
|
|
0 commit comments