Skip to content

Commit 097df66

Browse files
committed
fix: updated api services and its tests accordingly
1 parent 31db9a7 commit 097df66

File tree

3 files changed

+441
-18
lines changed

3 files changed

+441
-18
lines changed

lib/api_service.dart

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,23 @@ String baseUrl = 'http://YOUR_IP:8000';
7171
String origin = 'http://localhost:8080';
7272

7373
Future<List<Tasks>> fetchTasks(String uuid, String encryptionSecret) async {
74-
String url =
75-
'$baseUrl/tasks?email=email&origin=$origin&UUID=$uuid&encryptionSecret=$encryptionSecret';
76-
77-
var response = await http.get(Uri.parse(url), headers: {
78-
"Content-Type": "application/json",
79-
}).timeout(const Duration(seconds: 10000));
80-
if (response.statusCode == 200) {
81-
List<dynamic> allTasks = jsonDecode(response.body);
82-
debugPrint(allTasks.toString());
83-
return allTasks.map((task) => Tasks.fromJson(task)).toList();
84-
} else {
85-
throw Exception('Failed to load tasks');
74+
try {
75+
String url =
76+
'$baseUrl/tasks?email=email&origin=$origin&UUID=$uuid&encryptionSecret=$encryptionSecret';
77+
78+
var response = await http.get(Uri.parse(url), headers: {
79+
"Content-Type": "application/json",
80+
}).timeout(const Duration(seconds: 10000));
81+
if (response.statusCode == 200) {
82+
List<dynamic> allTasks = jsonDecode(response.body);
83+
debugPrint(allTasks.toString());
84+
return allTasks.map((task) => Tasks.fromJson(task)).toList();
85+
} else {
86+
throw Exception('Failed to load tasks');
87+
}
88+
} catch (e) {
89+
debugPrint('Error fetching tasks: $e');
90+
return [];
8691
}
8792
}
8893

test/api_service_test.dart

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
14
import 'package:flutter/services.dart';
25
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:get_test/utils/image_test_utils.dart';
37
import 'package:mockito/annotations.dart';
48
import 'package:mockito/mockito.dart';
59
import 'package:http/http.dart' as http;
610
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
711
import 'package:taskwarrior/api_service.dart';
812
import 'package:taskwarrior/app/utils/taskchampion/credentials_storage.dart';
913

10-
class MockHttpClient extends Mock implements http.Client {}
14+
import 'api_service_test.mocks.dart';
1115

1216
class MockCredentialsStorage extends Mock implements CredentialsStorage {}
1317

1418
class MockMethodChannel extends Mock implements MethodChannel {}
1519

16-
@GenerateMocks([MockMethodChannel])
20+
@GenerateMocks([MockMethodChannel, http.Client])
1721
void main() {
1822
TestWidgetsFlutterBinding.ensureInitialized();
1923

2024
databaseFactory = databaseFactoryFfi;
25+
MockClient mockClient = MockClient();
2126

2227
setUpAll(() {
2328
sqfliteFfiInit();
2429
});
2530

26-
setUp(() {});
27-
2831
group('Tasks model', () {
2932
test('fromJson creates Tasks object', () {
3033
final json = {
@@ -84,11 +87,25 @@ void main() {
8487
});
8588

8689
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 {
88105
const uuid = '123';
89106
const encryptionSecret = 'secret';
90107

91-
expect(() => fetchTasks(uuid, encryptionSecret), throwsException);
108+
expect(await fetchTasks(uuid, encryptionSecret), isEmpty);
92109
});
93110
});
94111

0 commit comments

Comments
 (0)