Skip to content

Commit

Permalink
feat: Add renderName option
Browse files Browse the repository at this point in the history
  • Loading branch information
micheljung committed Nov 30, 2024
1 parent dc75c78 commit dd17670
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
9 changes: 9 additions & 0 deletions lib/src/alchemist_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 11 additions & 8 deletions lib/src/golden_test_scenario.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,26 @@ class GoldenTestScenario extends StatelessWidget {

@override
Widget build(BuildContext context) {
final config = AlchemistConfig.current();
final testTheme = Theme.of(context).extension<GoldenTestTheme>() ??
AlchemistConfig.current().goldenTestTheme ??
config.goldenTestTheme ??
GoldenTestTheme.standard();
return Padding(
padding: testTheme.padding,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
name,
style: testTheme.nameTextStyle,
textHeightBehavior: const TextHeightBehavior(
applyHeightToFirstAscent: false,
if (config.renderName) ...<Widget>[
Text(
name,
style: testTheme.nameTextStyle,
textHeightBehavior: const TextHeightBehavior(
applyHeightToFirstAscent: false,
),
),
),
const SizedBox(height: 8),
const SizedBox(height: 8)
],
ConstrainedBox(
constraints: constraints ??
GoldenTestScenarioConstraints.maybeOf(context) ??
Expand Down
15 changes: 14 additions & 1 deletion test/src/golden_test_scenario_test.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit dd17670

Please sign in to comment.