From dd17670793cf79f53df51a424e8076d589e1d9bd Mon Sep 17 00:00:00 2001 From: Michel Jung Date: Sat, 30 Nov 2024 17:05:06 +0100 Subject: [PATCH] feat: Add renderName option --- lib/src/alchemist_config.dart | 9 +++++++++ lib/src/golden_test_scenario.dart | 19 +++++++++++-------- test/src/golden_test_scenario_test.dart | 15 ++++++++++++++- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/lib/src/alchemist_config.dart b/lib/src/alchemist_config.dart index 71a155c..0fb4d5c 100644 --- a/lib/src/alchemist_config.dart +++ b/lib/src/alchemist_config.dart @@ -67,11 +67,13 @@ class AlchemistConfig extends Equatable { /// {@macro alchemist_config} const AlchemistConfig({ bool? forceUpdateGoldenFiles, + bool? renderName, GoldenTestTheme? goldenTestTheme, ThemeData? theme, PlatformGoldensConfig? platformGoldensConfig, CiGoldensConfig? ciGoldensConfig, }) : _forceUpdateGoldenFiles = forceUpdateGoldenFiles, + _renderName = renderName, _theme = theme, _goldenTestTheme = goldenTestTheme, _platformGoldensConfig = platformGoldensConfig, @@ -177,6 +179,13 @@ class AlchemistConfig extends Equatable { bool get forceUpdateGoldenFiles => _forceUpdateGoldenFiles ?? false; final bool? _forceUpdateGoldenFiles; + /// Whether to render the name of the test scenario on top of the image. + /// + /// If set to `false`, the name will not be rendered. This can be useful when + /// Alchemist is used to create screenshots for a store page. + bool get renderName => _renderName ?? true; + final bool? _renderName; + /// The [ThemeData] to use when generating golden tests. /// /// If no [ThemeData] is provided, the default [ThemeData.light] will be used. diff --git a/lib/src/golden_test_scenario.dart b/lib/src/golden_test_scenario.dart index 65a8532..95c1535 100644 --- a/lib/src/golden_test_scenario.dart +++ b/lib/src/golden_test_scenario.dart @@ -70,8 +70,9 @@ class GoldenTestScenario extends StatelessWidget { @override Widget build(BuildContext context) { + final config = AlchemistConfig.current(); final testTheme = Theme.of(context).extension() ?? - AlchemistConfig.current().goldenTestTheme ?? + config.goldenTestTheme ?? GoldenTestTheme.standard(); return Padding( padding: testTheme.padding, @@ -79,14 +80,16 @@ class GoldenTestScenario extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ - Text( - name, - style: testTheme.nameTextStyle, - textHeightBehavior: const TextHeightBehavior( - applyHeightToFirstAscent: false, + if (config.renderName) ...[ + Text( + name, + style: testTheme.nameTextStyle, + textHeightBehavior: const TextHeightBehavior( + applyHeightToFirstAscent: false, + ), ), - ), - const SizedBox(height: 8), + const SizedBox(height: 8) + ], ConstrainedBox( constraints: constraints ?? GoldenTestScenarioConstraints.maybeOf(context) ?? diff --git a/test/src/golden_test_scenario_test.dart b/test/src/golden_test_scenario_test.dart index f9c357a..f506850 100644 --- a/test/src/golden_test_scenario_test.dart +++ b/test/src/golden_test_scenario_test.dart @@ -1,4 +1,4 @@ -import 'package:alchemist/src/golden_test_scenario.dart'; +import 'package:alchemist/alchemist.dart'; import 'package:alchemist/src/golden_test_scenario_constraints.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -31,6 +31,19 @@ void main() { expect(find.text('name'), findsOneWidget); }); + testWidgets('does not render name as label', (tester) async { + await AlchemistConfig.runWithConfig( + config: const AlchemistConfig(renderName: false), + run: () async { + final subject = buildSubject(); + + await tester.pumpWidget(subject); + + expect(find.text('name'), findsNothing); + }, + ); + }); + testWidgets('renders child', (tester) async { final subject = buildSubject();