Skip to content

Commit

Permalink
Inject the shared preferences class for easier testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandgeider committed Jan 24, 2025
1 parent 5521d92 commit dad109d
Show file tree
Hide file tree
Showing 26 changed files with 2,614 additions and 5,975 deletions.
10 changes: 7 additions & 3 deletions lib/providers/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import 'package:wger/providers/base_provider.dart';
class UserProvider with ChangeNotifier {
ThemeMode themeMode = ThemeMode.system;
final WgerBaseProvider baseProvider;
late SharedPreferencesAsync prefs;

UserProvider(this.baseProvider) {
UserProvider(this.baseProvider, {SharedPreferencesAsync? prefs}) {
this.prefs = prefs ?? SharedPreferencesAsync();
_loadThemeMode();
}

Expand All @@ -44,15 +46,18 @@ class UserProvider with ChangeNotifier {

// Load theme mode from SharedPreferences
Future<void> _loadThemeMode() async {
final prefs = SharedPreferencesAsync();
final prefsDarkMode = await prefs.getBool(PREFS_USER_DARK_THEME);

print(prefsDarkMode);

if (prefsDarkMode == null) {
themeMode = ThemeMode.system;
} else {
themeMode = prefsDarkMode ? ThemeMode.dark : ThemeMode.light;
}

print(themeMode);

notifyListeners();
}

Expand All @@ -61,7 +66,6 @@ class UserProvider with ChangeNotifier {
themeMode = mode;

// Save to SharedPreferences
final prefs = SharedPreferencesAsync();
if (themeMode == ThemeMode.system) {
await prefs.remove(PREFS_USER_DARK_THEME);
} else {
Expand Down
223 changes: 74 additions & 149 deletions test/auth/auth_screen_test.mocks.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Mocks generated by Mockito 5.4.4 from annotations
// Mocks generated by Mockito 5.4.5 from annotations
// in wger/test/auth/auth_screen_test.dart.
// Do not manually edit this file.

Expand All @@ -19,29 +19,19 @@ import 'package:mockito/src/dummies.dart' as _i5;
// ignore_for_file: deprecated_member_use_from_same_package
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: must_be_immutable
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class

class _FakeResponse_0 extends _i1.SmartFake implements _i2.Response {
_FakeResponse_0(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
_FakeResponse_0(Object parent, Invocation parentInvocation) : super(parent, parentInvocation);
}

class _FakeStreamedResponse_1 extends _i1.SmartFake implements _i2.StreamedResponse {
_FakeStreamedResponse_1(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
_FakeStreamedResponse_1(Object parent, Invocation parentInvocation)
: super(parent, parentInvocation);
}

/// A class which mocks [Client].
Expand All @@ -53,45 +43,25 @@ class MockClient extends _i1.Mock implements _i2.Client {
}

@override
_i3.Future<_i2.Response> head(
Uri? url, {
Map<String, String>? headers,
}) =>
(super.noSuchMethod(
Invocation.method(
#head,
[url],
{#headers: headers},
),
returnValue: _i3.Future<_i2.Response>.value(_FakeResponse_0(
this,
Invocation.method(
#head,
[url],
{#headers: headers},
_i3.Future<_i2.Response> head(Uri? url, {Map<String, String>? headers}) => (super.noSuchMethod(
Invocation.method(#head, [url], {#headers: headers}),
returnValue: _i3.Future<_i2.Response>.value(
_FakeResponse_0(
this,
Invocation.method(#head, [url], {#headers: headers}),
),
)),
),
) as _i3.Future<_i2.Response>);

@override
_i3.Future<_i2.Response> get(
Uri? url, {
Map<String, String>? headers,
}) =>
(super.noSuchMethod(
Invocation.method(
#get,
[url],
{#headers: headers},
),
returnValue: _i3.Future<_i2.Response>.value(_FakeResponse_0(
this,
Invocation.method(
#get,
[url],
{#headers: headers},
_i3.Future<_i2.Response> get(Uri? url, {Map<String, String>? headers}) => (super.noSuchMethod(
Invocation.method(#get, [url], {#headers: headers}),
returnValue: _i3.Future<_i2.Response>.value(
_FakeResponse_0(
this,
Invocation.method(#get, [url], {#headers: headers}),
),
)),
),
) as _i3.Future<_i2.Response>);

@override
Expand All @@ -105,24 +75,18 @@ class MockClient extends _i1.Mock implements _i2.Client {
Invocation.method(
#post,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
{#headers: headers, #body: body, #encoding: encoding},
),
returnValue: _i3.Future<_i2.Response>.value(_FakeResponse_0(
this,
Invocation.method(
#post,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
returnValue: _i3.Future<_i2.Response>.value(
_FakeResponse_0(
this,
Invocation.method(
#post,
[url],
{#headers: headers, #body: body, #encoding: encoding},
),
),
)),
),
) as _i3.Future<_i2.Response>);

@override
Expand All @@ -136,24 +100,18 @@ class MockClient extends _i1.Mock implements _i2.Client {
Invocation.method(
#put,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
{#headers: headers, #body: body, #encoding: encoding},
),
returnValue: _i3.Future<_i2.Response>.value(_FakeResponse_0(
this,
Invocation.method(
#put,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
returnValue: _i3.Future<_i2.Response>.value(
_FakeResponse_0(
this,
Invocation.method(
#put,
[url],
{#headers: headers, #body: body, #encoding: encoding},
),
),
)),
),
) as _i3.Future<_i2.Response>);

@override
Expand All @@ -167,24 +125,18 @@ class MockClient extends _i1.Mock implements _i2.Client {
Invocation.method(
#patch,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
{#headers: headers, #body: body, #encoding: encoding},
),
returnValue: _i3.Future<_i2.Response>.value(_FakeResponse_0(
this,
Invocation.method(
#patch,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
returnValue: _i3.Future<_i2.Response>.value(
_FakeResponse_0(
this,
Invocation.method(
#patch,
[url],
{#headers: headers, #body: body, #encoding: encoding},
),
),
)),
),
) as _i3.Future<_i2.Response>);

@override
Expand All @@ -198,45 +150,29 @@ class MockClient extends _i1.Mock implements _i2.Client {
Invocation.method(
#delete,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
{#headers: headers, #body: body, #encoding: encoding},
),
returnValue: _i3.Future<_i2.Response>.value(_FakeResponse_0(
this,
Invocation.method(
#delete,
[url],
{
#headers: headers,
#body: body,
#encoding: encoding,
},
returnValue: _i3.Future<_i2.Response>.value(
_FakeResponse_0(
this,
Invocation.method(
#delete,
[url],
{#headers: headers, #body: body, #encoding: encoding},
),
),
)),
),
) as _i3.Future<_i2.Response>);

@override
_i3.Future<String> read(
Uri? url, {
Map<String, String>? headers,
}) =>
(super.noSuchMethod(
Invocation.method(
#read,
[url],
{#headers: headers},
),
returnValue: _i3.Future<String>.value(_i5.dummyValue<String>(
this,
Invocation.method(
#read,
[url],
{#headers: headers},
_i3.Future<String> read(Uri? url, {Map<String, String>? headers}) => (super.noSuchMethod(
Invocation.method(#read, [url], {#headers: headers}),
returnValue: _i3.Future<String>.value(
_i5.dummyValue<String>(
this,
Invocation.method(#read, [url], {#headers: headers}),
),
)),
),
) as _i3.Future<String>);

@override
Expand All @@ -245,35 +181,24 @@ class MockClient extends _i1.Mock implements _i2.Client {
Map<String, String>? headers,
}) =>
(super.noSuchMethod(
Invocation.method(
#readBytes,
[url],
{#headers: headers},
),
Invocation.method(#readBytes, [url], {#headers: headers}),
returnValue: _i3.Future<_i6.Uint8List>.value(_i6.Uint8List(0)),
) as _i3.Future<_i6.Uint8List>);

@override
_i3.Future<_i2.StreamedResponse> send(_i2.BaseRequest? request) => (super.noSuchMethod(
Invocation.method(
#send,
[request],
),
returnValue: _i3.Future<_i2.StreamedResponse>.value(_FakeStreamedResponse_1(
this,
Invocation.method(
#send,
[request],
Invocation.method(#send, [request]),
returnValue: _i3.Future<_i2.StreamedResponse>.value(
_FakeStreamedResponse_1(
this,
Invocation.method(#send, [request]),
),
)),
),
) as _i3.Future<_i2.StreamedResponse>);

@override
void close() => super.noSuchMethod(
Invocation.method(
#close,
[],
),
Invocation.method(#close, []),
returnValueForMissingStub: null,
);
}
Loading

0 comments on commit dad109d

Please sign in to comment.