diff --git a/CHANGELOG.md b/CHANGELOG.md index 37f4683..f587986 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.1.2 + +- Updated README info about -> [`system_date_time_format_hook`](https://pub.dev/packages/system_date_time_format_hook) package +- Improved tests + ## 1.1.1 Refactor deprecated methods in tests diff --git a/README.md b/README.md index 3d5d544..50d63cc 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,10 @@ final datePattern = await SystemDateTimeFormat().getDatePattern(); print(datePattern); // e.g. "M/d/yy" ``` +### Do you use [flutter_hooks](https://pub.dev/packages/flutter_hooks) in the project? + +Consider using: [`system_date_time_format_hook`](https://pub.dev/packages/system_date_time_format_hook) instead. + ### Examples | iOS (Region: United States 🇺🇸) | Result | | -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- | @@ -132,6 +136,39 @@ class App extends StatelessWidget { > `SDTFScope` will automatically sync date & time format patterns even if user changes them > in the device system settings while your app is running. +### [Flutter hooks](https://pub.dev/packages/flutter_hooks) +You can use `SDTFScope` as shown in the example above. However, if you already know and use [flutter_hooks](https://pub.dev/packages/flutter_hooks) +in your project you can use [`system_date_time_format_hook`](https://pub.dev/packages/system_date_time_format_hook) instead, to achieve a similar effect: + +Example: +```dart +import 'package:flutter_hooks/flutter_hooks.dart'; +import 'package:system_date_time_format_hook/system_date_time_format_hook.dart'; + +class App extends HookWidget { + const App({super.key}); + + @override + Widget build(BuildContext context) { + final patterns = useSystemDateTimeFormat(); + + final datePattern = patterns.datePattern; + final timePattern = patterns.timePattern; + + print(datePattern); // e.g. "M/d/yy" + print(timePattern); // e.g. "HH:mm" + + return const MaterialApp( + home: Scaffold(), + ); + } +} +``` +> **Note** +> +> [`system_date_time_format_hook`](https://pub.dev/packages/system_date_time_format_hook) will automatically sync date & time format patterns even if user changes them +> in the device system settings while your app is running. + ### Web In order to use this plugin on web app you need to add `system_date_time_format.js` script to your `index.html`: diff --git a/example/pubspec.lock b/example/pubspec.lock index 63d5e34..9473e8d 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -171,7 +171,7 @@ packages: path: ".." relative: true source: path - version: "1.1.1" + version: "1.1.2" term_glyph: dependency: transitive description: diff --git a/example_with_tests/pubspec.lock b/example_with_tests/pubspec.lock index 9ce72a6..f6a185f 100644 --- a/example_with_tests/pubspec.lock +++ b/example_with_tests/pubspec.lock @@ -179,7 +179,7 @@ packages: path: ".." relative: true source: path - version: "1.1.1" + version: "1.1.2" term_glyph: dependency: transitive description: diff --git a/lib/src/widgets/sdtf_scope.dart b/lib/src/widgets/sdtf_scope.dart index 3f526b1..23c1b3c 100644 --- a/lib/src/widgets/sdtf_scope.dart +++ b/lib/src/widgets/sdtf_scope.dart @@ -3,7 +3,11 @@ import 'package:system_date_time_format/src/widgets/sdtf_scope_inherited.dart'; import 'package:system_date_time_format/system_date_time_format.dart'; class SDTFScope extends StatefulWidget { - const SDTFScope({required this.child, this.format, super.key}); + const SDTFScope({ + required this.child, + @visibleForTesting this.format, + super.key, + }); final Widget child; final SystemDateTimeFormat? format; diff --git a/pubspec.yaml b/pubspec.yaml index 41cdf1b..2c2921c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: system_date_time_format description: A plugin for getting date and time format patterns from device system settings. -version: 1.1.1 +version: 1.1.2 repository: https://github.com/Nikoro/system_date_time_format issue_tracker: https://github.com/Nikoro/system_date_time_format/issues diff --git a/test/_tools/extensions/extensions.dart b/test/_tools/extensions/extensions.dart index 0830c0f..8da1a76 100644 --- a/test/_tools/extensions/extensions.dart +++ b/test/_tools/extensions/extensions.dart @@ -1,6 +1,8 @@ -import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; part 'function_extensions.dart'; part 'iterable_extensions.dart'; +part 'mocktail_extensions.dart'; part 'widget_tester_extensions.dart'; diff --git a/test/_tools/extensions/function_extensions.dart b/test/_tools/extensions/function_extensions.dart index dda4ea8..fa6eb36 100644 --- a/test/_tools/extensions/function_extensions.dart +++ b/test/_tools/extensions/function_extensions.dart @@ -2,7 +2,7 @@ part of 'extensions.dart'; extension FunctionExtensions on Function { String get name { - final regExp = RegExp(r"(?<=\')(.*?)(?=\')"); + final regExp = RegExp(r"(?<=')(.*?)(?=')"); return regExp.firstMatch(toString())?.group(0) ?? ''; } diff --git a/test/_tools/extensions/mocktail_extensions.dart b/test/_tools/extensions/mocktail_extensions.dart new file mode 100644 index 0000000..8b2c1a3 --- /dev/null +++ b/test/_tools/extensions/mocktail_extensions.dart @@ -0,0 +1,10 @@ +part of 'extensions.dart'; + +extension WhenExtension on When { + thenAnswerMany(List> cbs) { + return thenAnswer((invocation) { + if (cbs.isEmpty) throw "No more answers available"; + return cbs.removeAt(0)(invocation); + }); + } +} diff --git a/test/_tools/extensions/widget_tester_extensions.dart b/test/_tools/extensions/widget_tester_extensions.dart index b0648d5..52cfb73 100644 --- a/test/_tools/extensions/widget_tester_extensions.dart +++ b/test/_tools/extensions/widget_tester_extensions.dart @@ -1,8 +1,7 @@ part of 'extensions.dart'; extension WidgetTesterExtensions on WidgetTester { - Future createContext() async { - await pumpWidget(Container()); - return element(find.byType(Container)); + Future setupWidget(Widget widget) { + return pumpWidget(MaterialApp(home: widget)); } } diff --git a/test/_tools/stubs/stubs.dart b/test/_tools/stubs/stubs.dart index fb75211..e0e1950 100644 --- a/test/_tools/stubs/stubs.dart +++ b/test/_tools/stubs/stubs.dart @@ -15,4 +15,17 @@ abstract class Stubs { fullDatePattern: fullDatePattern, timePattern: timePattern, ); + + static const differentDatePattern = 'differentDatePattern'; + static const differentMediumDatePattern = 'differentMediumDatePattern'; + static const differentLongDatePattern = 'differentLongDatePattern'; + static const differentFullDatePattern = 'differentFullDatePattern'; + static const differentTimePattern = 'differentTimePattern'; + static const allDifferentPatterns = Patterns( + datePattern: differentDatePattern, + mediumDatePattern: differentMediumDatePattern, + longDatePattern: differentLongDatePattern, + fullDatePattern: differentFullDatePattern, + timePattern: differentTimePattern, + ); } diff --git a/test/_tools/test_widget.dart b/test/_tools/test_widget.dart new file mode 100644 index 0000000..03fdd12 --- /dev/null +++ b/test/_tools/test_widget.dart @@ -0,0 +1,21 @@ +import 'package:flutter/material.dart'; +import 'package:system_date_time_format/system_date_time_format.dart'; + +class TestWidget extends StatelessWidget { + const TestWidget({super.key}); + + @override + Widget build(BuildContext context) { + final patterns = SystemDateTimeFormat.of(context); + + return Column( + children: [ + Text(patterns.datePattern ?? ''), + Text(patterns.mediumDatePattern ?? ''), + Text(patterns.longDatePattern ?? ''), + Text(patterns.fullDatePattern ?? ''), + Text(patterns.timePattern ?? ''), + ], + ); + } +} diff --git a/test/_tools/tools.dart b/test/_tools/tools.dart index 1564c17..d3333a6 100644 --- a/test/_tools/tools.dart +++ b/test/_tools/tools.dart @@ -1,3 +1,4 @@ export '../_tools/extensions/extensions.dart'; export '../_tools/mocks/mocks.dart'; export '../_tools/stubs/stubs.dart'; +export '../_tools/test_widget.dart'; diff --git a/test/src/widgets/sdtf_scope_test.dart b/test/src/widgets/sdtf_scope_test.dart index e961fce..f608c75 100644 --- a/test/src/widgets/sdtf_scope_test.dart +++ b/test/src/widgets/sdtf_scope_test.dart @@ -10,18 +10,60 @@ void main() { setUp(() { mockSDTFormat = MockSystemDateTimeFormat(); - when(() => mockSDTFormat.getAllPatterns()) - .thenAnswer((_) async => Stubs.allPatterns); + when(() => mockSDTFormat.getAllPatterns()).thenAnswerMany([ + (_) async => Stubs.allPatterns, + (_) async => Stubs.allDifferentPatterns, + ]); }); - testWidgets('getAllPatterns() is called on resume', (tester) async { - await tester.pumpWidget( - SDTFScope(format: mockSDTFormat, child: const Placeholder()), - ); - verify(() => mockSDTFormat.getAllPatterns()).called(1); + group('SDTFScope', () { + testWidgets('returns all patterns from system_date_time_format plugin', + (tester) async { + await tester.setupWidget( + SDTFScope(format: mockSDTFormat, child: const TestWidget()), + ); + await tester.pump(); - tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + expect(find.text(Stubs.datePattern), findsOneWidget); + expect(find.text(Stubs.mediumDatePattern), findsOneWidget); + expect(find.text(Stubs.longDatePattern), findsOneWidget); + expect(find.text(Stubs.fullDatePattern), findsOneWidget); + expect(find.text(Stubs.timePattern), findsOneWidget); + }); - verify(() => mockSDTFormat.getAllPatterns()).called(1); + testWidgets('getAllPatterns() is called on resume', (tester) async { + await tester.pumpWidget( + SDTFScope(format: mockSDTFormat, child: const Placeholder()), + ); + verify(() => mockSDTFormat.getAllPatterns()).called(1); + + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + + verify(() => mockSDTFormat.getAllPatterns()).called(1); + }); + + testWidgets( + 'returns all new different patterns from system_date_time_format plugin when patterns changed on resume', + (tester) async { + await tester.setupWidget( + SDTFScope(format: mockSDTFormat, child: const TestWidget()), + ); + await tester.pump(); + + expect(find.text(Stubs.datePattern), findsOneWidget); + expect(find.text(Stubs.mediumDatePattern), findsOneWidget); + expect(find.text(Stubs.longDatePattern), findsOneWidget); + expect(find.text(Stubs.fullDatePattern), findsOneWidget); + expect(find.text(Stubs.timePattern), findsOneWidget); + + tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + await tester.pumpAndSettle(); + + expect(find.text(Stubs.differentDatePattern), findsOneWidget); + expect(find.text(Stubs.differentMediumDatePattern), findsOneWidget); + expect(find.text(Stubs.differentLongDatePattern), findsOneWidget); + expect(find.text(Stubs.differentFullDatePattern), findsOneWidget); + expect(find.text(Stubs.differentTimePattern), findsOneWidget); + }); }); }