Skip to content

Commit bd5d719

Browse files
committed
test: added test for all files under utils/taskchampion
1 parent 46d9fed commit bd5d719

File tree

3 files changed

+313
-0
lines changed

3 files changed

+313
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:mockito/annotations.dart';
3+
import 'package:mockito/mockito.dart';
4+
import 'package:shared_preferences/shared_preferences.dart';
5+
import 'package:taskwarrior/app/utils/taskchampion/credentials_storage.dart';
6+
7+
import 'credentials_storage_test.mocks.dart';
8+
9+
@GenerateMocks([SharedPreferences])
10+
void main() {
11+
group('CredentialsStorage', () {
12+
late MockSharedPreferences mockSharedPreferences;
13+
14+
setUp(() {
15+
mockSharedPreferences = MockSharedPreferences();
16+
});
17+
18+
test('should return null when encryption secret is not set', () async {
19+
when(mockSharedPreferences.getString('encryptionSecret'))
20+
.thenReturn(null);
21+
SharedPreferences.setMockInitialValues({});
22+
23+
final encryptionSecret = await CredentialsStorage.getEncryptionSecret();
24+
expect(encryptionSecret, isNull);
25+
});
26+
27+
test('should return encryption secret when it is set', () async {
28+
when(mockSharedPreferences.getString('encryptionSecret'))
29+
.thenReturn('secret123');
30+
SharedPreferences.setMockInitialValues({
31+
'encryptionSecret': 'secret123',
32+
});
33+
34+
final encryptionSecret = await CredentialsStorage.getEncryptionSecret();
35+
expect(encryptionSecret, 'secret123');
36+
});
37+
38+
test('should return null when client ID is not set', () async {
39+
when(mockSharedPreferences.getString('clientId')).thenReturn(null);
40+
SharedPreferences.setMockInitialValues({});
41+
42+
final clientId = await CredentialsStorage.getClientId();
43+
expect(clientId, isNull);
44+
});
45+
46+
test('should return client ID when it is set', () async {
47+
when(mockSharedPreferences.getString('clientId')).thenReturn('client123');
48+
SharedPreferences.setMockInitialValues({
49+
'clientId': 'client123',
50+
});
51+
52+
final clientId = await CredentialsStorage.getClientId();
53+
expect(clientId, 'client123');
54+
});
55+
});
56+
}
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
// Mocks generated by Mockito 5.4.4 from annotations
2+
// in taskwarrior/test/utils/taskchampion/credentials_storage_test.dart.
3+
// Do not manually edit this file.
4+
5+
// ignore_for_file: no_leading_underscores_for_library_prefixes
6+
import 'dart:async' as _i3;
7+
8+
import 'package:mockito/mockito.dart' as _i1;
9+
import 'package:shared_preferences/src/shared_preferences_legacy.dart' as _i2;
10+
11+
// ignore_for_file: type=lint
12+
// ignore_for_file: avoid_redundant_argument_values
13+
// ignore_for_file: avoid_setters_without_getters
14+
// ignore_for_file: comment_references
15+
// ignore_for_file: deprecated_member_use
16+
// ignore_for_file: deprecated_member_use_from_same_package
17+
// ignore_for_file: implementation_imports
18+
// ignore_for_file: invalid_use_of_visible_for_testing_member
19+
// ignore_for_file: prefer_const_constructors
20+
// ignore_for_file: unnecessary_parenthesis
21+
// ignore_for_file: camel_case_types
22+
// ignore_for_file: subtype_of_sealed_class
23+
24+
/// A class which mocks [SharedPreferences].
25+
///
26+
/// See the documentation for Mockito's code generation for more information.
27+
class MockSharedPreferences extends _i1.Mock implements _i2.SharedPreferences {
28+
MockSharedPreferences() {
29+
_i1.throwOnMissingStub(this);
30+
}
31+
32+
@override
33+
Set<String> getKeys() => (super.noSuchMethod(
34+
Invocation.method(
35+
#getKeys,
36+
[],
37+
),
38+
returnValue: <String>{},
39+
) as Set<String>);
40+
41+
@override
42+
Object? get(String? key) => (super.noSuchMethod(Invocation.method(
43+
#get,
44+
[key],
45+
)) as Object?);
46+
47+
@override
48+
bool? getBool(String? key) => (super.noSuchMethod(Invocation.method(
49+
#getBool,
50+
[key],
51+
)) as bool?);
52+
53+
@override
54+
int? getInt(String? key) => (super.noSuchMethod(Invocation.method(
55+
#getInt,
56+
[key],
57+
)) as int?);
58+
59+
@override
60+
double? getDouble(String? key) => (super.noSuchMethod(Invocation.method(
61+
#getDouble,
62+
[key],
63+
)) as double?);
64+
65+
@override
66+
String? getString(String? key) => (super.noSuchMethod(Invocation.method(
67+
#getString,
68+
[key],
69+
)) as String?);
70+
71+
@override
72+
bool containsKey(String? key) => (super.noSuchMethod(
73+
Invocation.method(
74+
#containsKey,
75+
[key],
76+
),
77+
returnValue: false,
78+
) as bool);
79+
80+
@override
81+
List<String>? getStringList(String? key) =>
82+
(super.noSuchMethod(Invocation.method(
83+
#getStringList,
84+
[key],
85+
)) as List<String>?);
86+
87+
@override
88+
_i3.Future<bool> setBool(
89+
String? key,
90+
bool? value,
91+
) =>
92+
(super.noSuchMethod(
93+
Invocation.method(
94+
#setBool,
95+
[
96+
key,
97+
value,
98+
],
99+
),
100+
returnValue: _i3.Future<bool>.value(false),
101+
) as _i3.Future<bool>);
102+
103+
@override
104+
_i3.Future<bool> setInt(
105+
String? key,
106+
int? value,
107+
) =>
108+
(super.noSuchMethod(
109+
Invocation.method(
110+
#setInt,
111+
[
112+
key,
113+
value,
114+
],
115+
),
116+
returnValue: _i3.Future<bool>.value(false),
117+
) as _i3.Future<bool>);
118+
119+
@override
120+
_i3.Future<bool> setDouble(
121+
String? key,
122+
double? value,
123+
) =>
124+
(super.noSuchMethod(
125+
Invocation.method(
126+
#setDouble,
127+
[
128+
key,
129+
value,
130+
],
131+
),
132+
returnValue: _i3.Future<bool>.value(false),
133+
) as _i3.Future<bool>);
134+
135+
@override
136+
_i3.Future<bool> setString(
137+
String? key,
138+
String? value,
139+
) =>
140+
(super.noSuchMethod(
141+
Invocation.method(
142+
#setString,
143+
[
144+
key,
145+
value,
146+
],
147+
),
148+
returnValue: _i3.Future<bool>.value(false),
149+
) as _i3.Future<bool>);
150+
151+
@override
152+
_i3.Future<bool> setStringList(
153+
String? key,
154+
List<String>? value,
155+
) =>
156+
(super.noSuchMethod(
157+
Invocation.method(
158+
#setStringList,
159+
[
160+
key,
161+
value,
162+
],
163+
),
164+
returnValue: _i3.Future<bool>.value(false),
165+
) as _i3.Future<bool>);
166+
167+
@override
168+
_i3.Future<bool> remove(String? key) => (super.noSuchMethod(
169+
Invocation.method(
170+
#remove,
171+
[key],
172+
),
173+
returnValue: _i3.Future<bool>.value(false),
174+
) as _i3.Future<bool>);
175+
176+
@override
177+
_i3.Future<bool> commit() => (super.noSuchMethod(
178+
Invocation.method(
179+
#commit,
180+
[],
181+
),
182+
returnValue: _i3.Future<bool>.value(false),
183+
) as _i3.Future<bool>);
184+
185+
@override
186+
_i3.Future<bool> clear() => (super.noSuchMethod(
187+
Invocation.method(
188+
#clear,
189+
[],
190+
),
191+
returnValue: _i3.Future<bool>.value(false),
192+
) as _i3.Future<bool>);
193+
194+
@override
195+
_i3.Future<void> reload() => (super.noSuchMethod(
196+
Invocation.method(
197+
#reload,
198+
[],
199+
),
200+
returnValue: _i3.Future<void>.value(),
201+
returnValueForMissingStub: _i3.Future<void>.value(),
202+
) as _i3.Future<void>);
203+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:shared_preferences/shared_preferences.dart';
4+
import 'package:taskwarrior/app/utils/taskchampion/taskchampion.dart';
5+
6+
void main() {
7+
TestWidgetsFlutterBinding.ensureInitialized();
8+
9+
group('ManageTaskChampionCreds', () {
10+
setUp(() {
11+
SharedPreferences.setMockInitialValues({});
12+
});
13+
14+
testWidgets('should load credentials and display them in text fields',
15+
(WidgetTester tester) async {
16+
SharedPreferences prefs = await SharedPreferences.getInstance();
17+
await prefs.setString('encryptionSecret', 'testSecret');
18+
await prefs.setString('clientId', 'testClientId');
19+
20+
await tester.pumpWidget(MaterialApp(home: ManageTaskChampionCreds()));
21+
22+
await tester.pump();
23+
24+
expect(find.text('testSecret'), findsOneWidget);
25+
expect(find.text('testClientId'), findsOneWidget);
26+
});
27+
28+
testWidgets('should save credentials when the save button is pressed',
29+
(WidgetTester tester) async {
30+
await tester.pumpWidget(MaterialApp(home: ManageTaskChampionCreds()));
31+
32+
await tester.enterText(find.byType(TextField).at(0), 'newSecret');
33+
await tester.enterText(find.byType(TextField).at(1), 'newClientId');
34+
35+
await tester.tap(find.text('Save Credentials'));
36+
await tester.pump();
37+
38+
SharedPreferences prefs = await SharedPreferences.getInstance();
39+
expect(prefs.getString('encryptionSecret'), 'newSecret');
40+
expect(prefs.getString('clientId'), 'newClientId');
41+
});
42+
43+
testWidgets('should show snackbar when credentials are saved',
44+
(WidgetTester tester) async {
45+
await tester.pumpWidget(MaterialApp(home: ManageTaskChampionCreds()));
46+
47+
await tester.tap(find.text('Save Credentials'));
48+
await tester.pump();
49+
await tester.pump(const Duration(seconds: 1));
50+
51+
expect(find.text('Credentials saved successfully'), findsOneWidget);
52+
});
53+
});
54+
}

0 commit comments

Comments
 (0)