Skip to content

Commit

Permalink
1.1.2
Browse files Browse the repository at this point in the history
* improve tests

* update README

* update CHANGELOG

* bump version to 1.1.2
  • Loading branch information
Nikoro authored Feb 14, 2024
1 parent 4f6ccb2 commit 4b3299e
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
Expand Down Expand Up @@ -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`:
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.1.1"
version: "1.1.2"
term_glyph:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion example_with_tests/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.1.1"
version: "1.1.2"
term_glyph:
dependency: transitive
description:
Expand Down
6 changes: 5 additions & 1 deletion lib/src/widgets/sdtf_scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 3 additions & 1 deletion test/_tools/extensions/extensions.dart
Original file line number Diff line number Diff line change
@@ -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';
2 changes: 1 addition & 1 deletion test/_tools/extensions/function_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?? '';
}
Expand Down
10 changes: 10 additions & 0 deletions test/_tools/extensions/mocktail_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
part of 'extensions.dart';

extension WhenExtension<T> on When<T> {
thenAnswerMany(List<Answer<T>> cbs) {
return thenAnswer((invocation) {
if (cbs.isEmpty) throw "No more answers available";
return cbs.removeAt(0)(invocation);
});
}
}
5 changes: 2 additions & 3 deletions test/_tools/extensions/widget_tester_extensions.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
part of 'extensions.dart';

extension WidgetTesterExtensions on WidgetTester {
Future<BuildContext> createContext() async {
await pumpWidget(Container());
return element(find.byType(Container));
Future<void> setupWidget(Widget widget) {
return pumpWidget(MaterialApp(home: widget));
}
}
13 changes: 13 additions & 0 deletions test/_tools/stubs/stubs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
21 changes: 21 additions & 0 deletions test/_tools/test_widget.dart
Original file line number Diff line number Diff line change
@@ -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 ?? ''),
],
);
}
}
1 change: 1 addition & 0 deletions test/_tools/tools.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export '../_tools/extensions/extensions.dart';
export '../_tools/mocks/mocks.dart';
export '../_tools/stubs/stubs.dart';
export '../_tools/test_widget.dart';
60 changes: 51 additions & 9 deletions test/src/widgets/sdtf_scope_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}

0 comments on commit 4b3299e

Please sign in to comment.