Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
mirmoktadir committed Aug 28, 2023
1 parent 999a990 commit 237f350
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/app/data/local/my_shared_pref.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MySharedPref {
static const String _lightThemeKey = 'is_theme_light';

/// set theme current type as light theme
static void setThemeIsLight(bool lightTheme) =>
static Future<void> setThemeIsLight(bool lightTheme) =>
_sharedPreferences!.setBool(_lightThemeKey, lightTheme);

/// get if the current theme type is light
Expand Down Expand Up @@ -66,5 +66,5 @@ class MySharedPref {
}

/// clear all data from shared pref
/// static Future<void> clear() async => await _sharedPreferences!.clear();
static Future<void> clear() async => await _sharedPreferences!.clear();
}
2 changes: 2 additions & 0 deletions lib/config/translations/ar_AR/ar_ar_translation.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import '../strings_enum.dart';

final Map<String, String> arAR = {
Strings.hello: 'مرحباً!',

///API EXCEPTIONS
Strings.requestCanceled: "تم إلغاء الطلب!",
Strings.connectionTimeout: "انتهى وقت محاولة الاتصال!",
Expand Down
2 changes: 2 additions & 0 deletions lib/config/translations/en_US/en_us_translation.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import '../strings_enum.dart';

const Map<String, String> enUs = {
Strings.hello: 'Hello!',

///API EXCEPTIONS
Strings.requestCanceled: "Request cancelled!",
Strings.connectionTimeout: "Connection timeout!",
Expand Down
10 changes: 10 additions & 0 deletions lib/config/translations/localization_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import 'ar_AR/ar_ar_translation.dart';
import 'en_US/en_us_translation.dart';

class LocalizationService extends Translations {
// prevent creating instance
LocalizationService._();

static LocalizationService? _instance;

static LocalizationService getInstance() {
_instance ??= LocalizationService._();
return _instance!;
}

// default language
static Locale defaultLanguage = supportedLanguages['en']!;

Expand Down
2 changes: 2 additions & 0 deletions lib/config/translations/strings_enum.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Strings {
static const String hello = 'hello';

/// API EXCEPTIONS
static const String requestCanceled = "Request cancelled!";
static const String connectionTimeout = "Connection timeout!";
Expand Down
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ Future<void> main() async {
// app screens
locale: MySharedPref.getCurrentLocal(),
// app language
translations:
LocalizationService(), // localization services in app (controller app language)
translations: LocalizationService
.getInstance(), // localization services in app (controller app language)
);
},
),
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,10 @@ packages:
dependency: "direct main"
description:
name: file_picker
sha256: "21145c9c268d54b1f771d8380c195d2d6f655e0567dc1ca2f9c134c02c819e0a"
sha256: bdfa035a974a0c080576c4c8ed01cdf9d1b406a04c7daa05443ef0383a97bedc
url: "https://pub.dev"
source: hosted
version: "5.3.3"
version: "5.3.4"
file_selector_linux:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies:
iconly: ^1.0.1
iconsax: ^0.0.8
expandable_text: ^2.3.0
file_picker: ^5.3.3
file_picker: ^5.3.4
lottie: ^2.6.0
ionicons: ^0.2.2
flutter_otp_text_field: ^1.1.1
Expand Down
Empty file added test/baseclient_test.dart
Empty file.
79 changes: 79 additions & 0 deletions test/localization_service_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get/get.dart';
import 'package:getx_standard/app/data/local/my_shared_pref.dart';
import 'package:getx_standard/config/translations/localization_service.dart';
import 'package:getx_standard/config/translations/strings_enum.dart';

import 'package:shared_preferences/shared_preferences.dart';

main() async {
TestWidgetsFlutterBinding.ensureInitialized();

setUp(() {
Get.testMode = true;
});

// mock initial data
Map<String, Object> values = <String, Object>{};
SharedPreferences.setMockInitialValues(values);

await MySharedPref.init();

test('check if language is supported', () {
// check if English is supported
bool isEnSupported = LocalizationService.isLanguageSupported('en');
expect(isEnSupported, true);

// check if French supported
bool isFrSupported = LocalizationService.isLanguageSupported('fr');
expect(isFrSupported, false);
});

test('Check getting/updating current local', () async {
await LocalizationService.updateLanguage('en');
Locale currentLocale = LocalizationService.getCurrentLocal();
expect(currentLocale.languageCode, 'en');

await LocalizationService.updateLanguage('ar');
Locale currentLocaleAfterUpdate = LocalizationService.getCurrentLocal();
expect(currentLocaleAfterUpdate.languageCode, 'ar');
});

test('Check if current language is English', () async {
await LocalizationService.updateLanguage('en');
bool isCurrentLangIsEnglish =
LocalizationService.getCurrentLocal().languageCode.contains('en');
expect(isCurrentLangIsEnglish, true);

await LocalizationService.updateLanguage('ar');
bool isCurrentLangEnglishAfterUpdate =
LocalizationService.getCurrentLocal().languageCode.contains('ar');
expect(isCurrentLangEnglishAfterUpdate, true);
});

testWidgets('Check translation', (tester) async {
Get.testMode = false;
await tester.pumpWidget(GetMaterialApp(
locale: MySharedPref.getCurrentLocal(),
translations: LocalizationService.getInstance(),
home: const Scaffold(
body: Center(child: Text('Testing..')),
),
));
await tester.pumpAndSettle();

// make language english and test the word value
await LocalizationService.updateLanguage('en');

await tester.pumpAndSettle();
String helloWord = Strings.hello.tr;
expect(helloWord, 'Hello!');

// make language english and test the word value
await LocalizationService.updateLanguage('ar');
await tester.pumpAndSettle();
String helloWordAfterChangingLanguage = Strings.hello.tr;
expect(helloWordAfterChangingLanguage, 'مرحباً!');
});
}
Empty file added test/my_hive_test.dart
Empty file.
46 changes: 46 additions & 0 deletions test/my_sharedpref_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:getx_standard/app/data/local/my_shared_pref.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// test shared pref (read & write)
Future<void> main() async {
TestWidgetsFlutterBinding.ensureInitialized();

// mock initial data
Map<String, Object> values = <String, Object>{};
SharedPreferences.setMockInitialValues(values);

await MySharedPref.init();

test('clear all the data from storage', () async {
// set new token in shared pref
await MySharedPref.setFcmToken('token');

// check if the token stored
String? token = MySharedPref.getFcmToken();

// token must be set correctly
expect(token, isNotNull);

// clear all data
await MySharedPref.clear();

// token must be null now after clearing data
String? tokenAfterClearing = MySharedPref.getFcmToken();

// token must be null
expect(tokenAfterClearing, isNull);
});

test('test read and write', () async {
// set theme is light to false (write operation)
await MySharedPref.setThemeIsLight(false);

// get the value and test if the saving went fine (read operation)
bool themeIsLight = MySharedPref.getThemeIsLight();

// make sure write and read went fine
expect(themeIsLight, false);
});
}

0 comments on commit 237f350

Please sign in to comment.