Skip to content

Commit

Permalink
[#228] [RF] Improve Cove Coverage. Cover locale
Browse files Browse the repository at this point in the history
  • Loading branch information
lyskouski committed Sep 8, 2023
1 parent eb7fc8d commit b625a0e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
26 changes: 11 additions & 15 deletions lib/_classes/herald/app_locale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ class AppLocale extends ValueNotifier<Locale?> with SharedPreferencesMixin {
AppLocale([Locale? value]) : super(value) {
final newValue = getPreference(prefLocale);
if (newValue != null) {
final loc = fromCode(newValue);
if (loc != null && AppLocalizations.supportedLocales.contains(loc)) {
_set(loc);
}
set(newValue);
}
}

Expand All @@ -27,22 +24,21 @@ class AppLocale extends ValueNotifier<Locale?> with SharedPreferencesMixin {
}
}

void _set(Locale newValue) {
if (newValue != value) {
value = newValue;
void set(String newValue, [Function? callback]) {
final loc = fromCode(newValue);
if (loc != null && loc != value && AppLocalizations.supportedLocales.contains(loc)) {
value = loc;
code = value.toString();
if (callback != null) {
callback();
}
notifyListeners();
}
}

updateState(BuildContext context) {
void updateState(BuildContext context) {
final value = Localizations.localeOf(context).toString();
final loc = fromCode(value);
if (loc != null) {
labels = AppLocalizations.of(context)!;
setPreference(prefLocale, value).then((_) {
_set(loc);
});
}
labels = AppLocalizations.of(context)!;
set(value, () => setPreference(prefLocale, value));
}
}
39 changes: 39 additions & 0 deletions test/unit/_classes/herald/app_locale_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2023 The terCAD team. All rights reserved.
// Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.

import 'package:app_finance/_classes/herald/app_locale.dart';
import 'package:app_finance/_mixins/shared_preferences_mixin.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:shared_preferences/shared_preferences.dart';

@GenerateNiceMocks([MockSpec<SharedPreferences>(), MockSpec<BuildContext>()])
import 'app_locale_test.mocks.dart';

void main() {
setUp(() {
SharedPreferencesMixin.pref = MockSharedPreferences();
});

group('AppLocale', () {
test('fromCode', () {
expect(AppLocale.fromCode('en'), const Locale('en'));
});

test('set [en]', () {
final locale = AppLocale();
expect(locale.value, null);
locale.set('en');
expect(locale.value, const Locale('en'));
});

test('[TBD] updateState', () {
final locale = AppLocale();
final context = MockBuildContext();
when(context.dependOnInheritedWidgetOfExactType()).thenReturn(null);
expect(() => locale.updateState(context), throwsA(isA<FlutterError>()));
});
});
}

0 comments on commit b625a0e

Please sign in to comment.