diff --git a/example/lib/widgets/date_text.dart b/example/lib/widgets/date_text.dart new file mode 100644 index 0000000..2f3f6ab --- /dev/null +++ b/example/lib/widgets/date_text.dart @@ -0,0 +1,16 @@ +import 'package:clock/clock.dart'; +import 'package:flutter/material.dart'; + +class DateText extends StatelessWidget { + const DateText({ + super.key, + }); + + @override + Widget build(BuildContext context) { + return Text( + clock.now().toIso8601String(), + textAlign: TextAlign.center, + ); + } +} diff --git a/example/lib/widgets/widgets.dart b/example/lib/widgets/widgets.dart index 5e9ab63..b9c3249 100644 --- a/example/lib/widgets/widgets.dart +++ b/example/lib/widgets/widgets.dart @@ -1,2 +1,3 @@ export 'contact_list_tile.dart'; +export 'date_text.dart'; export 'red_button.dart'; diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 805776b..433d911 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -9,6 +9,7 @@ environment: sdk: ">=2.17.0 <3.0.0" dependencies: + clock: ^1.1.2 flutter: sdk: flutter diff --git a/example/test/widgets/custom_wrapper_golden_test.dart b/example/test/widgets/custom_wrapper_golden_test.dart new file mode 100644 index 0000000..6d897c9 --- /dev/null +++ b/example/test/widgets/custom_wrapper_golden_test.dart @@ -0,0 +1,25 @@ +import 'package:alchemist/alchemist.dart'; +import 'package:clock/clock.dart'; +import 'package:example/widgets/widgets.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + group('Custom Wrapper Golden Tests', () { + goldenTest( + 'renders correctly', + fileName: 'custom_wrapper', + testWrapper: (test) async => withClock( + Clock.fixed(DateTime.utc(2025, 9, 10)), + test, + ), + builder: () => GoldenTestGroup( + children: [ + GoldenTestScenario( + name: 'now', + child: const DateText(), + ), + ], + ), + ); + }); +} diff --git a/lib/src/golden_test.dart b/lib/src/golden_test.dart index acdacfc..e6e098b 100644 --- a/lib/src/golden_test.dart +++ b/lib/src/golden_test.dart @@ -130,6 +130,17 @@ Future loadFonts() async { /// built-in interaction receives a finder indicating all of the widgets /// that should be interacted with. /// +/// The [testWrapper] is needed to wrap the test zone to another during the test +/// For example, when you need to use `runWithClock` inside goldens +/// ```dart +/// goldenTest( +/// ... +/// wrapper: (callback) async => withClock( +/// Clock.fixed(DateTime(2025, 12, 10)), +/// callback, +/// ) +/// ``` +/// /// **Note**: If a built-in [whilePerforming] interaction is provided, the /// widget tree is **always** pumped at least once before the assertion phase /// of the test. @@ -149,6 +160,7 @@ Future goldenTest( PumpAction pumpBeforeTest = onlyPumpAndSettle, PumpWidget pumpWidget = onlyPumpWidget, Interaction? whilePerforming, + Future Function(Future Function() callback)? testWrapper, }) async { if (skip) return; @@ -173,10 +185,11 @@ Future goldenTest( await goldenTestAdapter.testWidgets( description, (tester) async { - final variantConfig = variant.currentConfig; - await goldenTestRunner.run( - tester: tester, - goldenPath: await variantConfig.filePathResolver( + Future callback() async { + final variantConfig = variant.currentConfig; + await goldenTestRunner.run( + tester: tester, + goldenPath: await variantConfig.filePathResolver( fileName, variantConfig.environmentName, ), @@ -192,7 +205,15 @@ Future goldenTest( pumpBeforeTest: pumpBeforeTest, pumpWidget: pumpWidget, whilePerforming: whilePerforming, - ); + ); + } + + if (testWrapper != null) { + await testWrapper(callback); + } else { + await callback(); + } + }, tags: tags, variant: variant,