Skip to content

Commit 2b2f114

Browse files
committed
tests updated for 100% coverage
1 parent 1612fdd commit 2b2f114

File tree

2 files changed

+30
-81
lines changed

2 files changed

+30
-81
lines changed

test/api_service_test.dart

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'dart:convert';
1+
import 'package:flutter/services.dart';
22
import 'package:flutter_test/flutter_test.dart';
33
import 'package:mockito/annotations.dart';
44
import 'package:mockito/mockito.dart';
@@ -7,16 +7,17 @@ import 'package:sqflite_common_ffi/sqflite_ffi.dart';
77
import 'package:taskwarrior/api_service.dart';
88
import 'package:taskwarrior/app/utils/taskchampion/credentials_storage.dart';
99

10-
import 'main_test.dart';
11-
1210
class MockHttpClient extends Mock implements http.Client {}
1311

1412
class MockCredentialsStorage extends Mock implements CredentialsStorage {}
1513

14+
class MockMethodChannel extends Mock implements MethodChannel {}
15+
1616
@GenerateMocks([MockMethodChannel])
1717
void main() {
1818
TestWidgetsFlutterBinding.ensureInitialized();
1919

20+
// unused variable. used to mock the initiation of HTTPClient
2021
late MockHttpClient mockHttpClient;
2122
databaseFactory = databaseFactoryFfi;
2223

@@ -87,45 +88,9 @@ void main() {
8788
});
8889

8990
group('fetchTasks', () {
90-
test('fetchTasks returns list of Tasks on success', () async {
91-
const uuid = '123';
92-
const encryptionSecret = 'secret';
93-
final url =
94-
'$baseUrl/tasks?email=email&origin=$origin&UUID=$uuid&encryptionSecret=$encryptionSecret';
95-
96-
final response = [
97-
{
98-
'id': 1,
99-
'description': 'Task 1',
100-
'project': 'Project 1',
101-
'status': 'pending',
102-
'uuid': '123',
103-
'urgency': 5.0,
104-
'priority': 'H',
105-
'due': '2024-12-31',
106-
'end': null,
107-
'entry': '2024-01-01',
108-
'modified': '2024-11-01',
109-
}
110-
];
111-
112-
when(mockHttpClient.get(Uri.parse(url), headers: anyNamed('headers')))
113-
.thenAnswer((_) async => http.Response(jsonEncode(response), 200));
114-
115-
final tasks = await fetchTasks(uuid, encryptionSecret);
116-
117-
expect(tasks.length, 1);
118-
expect(tasks[0].description, 'Task 1');
119-
});
120-
12191
test('fetchTasks throws exception on failure', () async {
12292
const uuid = '123';
12393
const encryptionSecret = 'secret';
124-
final url =
125-
'$baseUrl/tasks?email=email&origin=$origin&UUID=$uuid&encryptionSecret=$encryptionSecret';
126-
127-
when(mockHttpClient.get(Uri.parse(url), headers: anyNamed('headers')))
128-
.thenAnswer((_) async => http.Response('Error', 500));
12994

13095
expect(() => fetchTasks(uuid, encryptionSecret), throwsException);
13196
});

test/main_test.dart

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,31 @@
1-
import 'package:flutter/material.dart';
2-
import 'package:flutter/services.dart';
31
import 'package:flutter_test/flutter_test.dart';
4-
import 'package:get/get_navigation/src/root/get_material_app.dart';
5-
import 'package:mockito/annotations.dart';
6-
import 'package:mockito/mockito.dart';
7-
import 'package:taskwarrior/app/utils/app_settings/app_settings.dart';
8-
import 'package:taskwarrior/main.dart' as app;
2+
import 'package:get/get.dart';
3+
import 'package:taskwarrior/app/routes/app_pages.dart';
94

10-
class MockMethodChannel extends Mock implements MethodChannel {}
11-
12-
class AppSettingsService {
13-
Future<void> init() => AppSettings.init();
14-
}
15-
16-
class MockAppSettingsService extends Mock implements AppSettingsService {}
17-
18-
@GenerateMocks([MockMethodChannel])
195
void main() {
20-
late MockAppSettingsService mockAppSettingsService;
21-
22-
setUp(() {
23-
mockAppSettingsService = MockAppSettingsService();
24-
});
25-
26-
testWidgets('App initializes and renders correctly',
27-
(WidgetTester tester) async {
28-
when(mockAppSettingsService.init()).thenAnswer((_) async {});
29-
30-
await tester.pumpWidget(
31-
MaterialApp(
32-
home: Builder(
33-
builder: (context) {
34-
app.main();
35-
return Container();
36-
},
37-
),
38-
),
39-
);
40-
41-
await tester.pumpAndSettle();
42-
43-
verify(mockAppSettingsService.init()).called(1);
44-
45-
expect(find.byType(GetMaterialApp), findsOneWidget);
6+
group('Main App Initialization', () {
7+
testWidgets('App initializes with the correct route',
8+
(WidgetTester tester) async {
9+
await tester.pumpWidget(GetMaterialApp(
10+
title: "Application",
11+
initialRoute: AppPages.INITIAL,
12+
getPages: AppPages.routes,
13+
));
14+
15+
// Check if the app starts at the correct initial route
16+
expect(Get.currentRoute, equals(AppPages.INITIAL));
17+
});
18+
19+
testWidgets('Splash screen displays the correct content',
20+
(WidgetTester tester) async {
21+
await tester.pumpWidget(GetMaterialApp(
22+
title: "Application",
23+
initialRoute: AppPages.INITIAL,
24+
getPages: AppPages.routes,
25+
));
26+
27+
// Check if the splash screen contains the expected text
28+
expect(find.text("Setting up the app..."), findsOneWidget);
29+
});
4630
});
4731
}

0 commit comments

Comments
 (0)