Skip to content

Commit

Permalink
feat!: Added nameTextStyle (#132)
Browse files Browse the repository at this point in the history
* feat!: Added nameTextStyle

* fix: test for the new nameTextStyle

* fix: revert golden test
  • Loading branch information
vanlooverenkoen authored Oct 9, 2024
1 parent 6a18cf2 commit 4bc0fc5
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/src/golden_test_scenario.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ class GoldenTestScenario extends StatelessWidget {

@override
Widget build(BuildContext context) {
final testTheme = Theme.of(context).extension<GoldenTestTheme>() ??
AlchemistConfig.current().goldenTestTheme ??
GoldenTestTheme.standard();
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
name,
style: const TextStyle(fontSize: 18),
style: testTheme.nameTextStyle,
textHeightBehavior: const TextHeightBehavior(
applyHeightToFirstAscent: false,
),
Expand Down
12 changes: 12 additions & 0 deletions lib/src/golden_test_theme.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:alchemist/src/golden_test_group.dart';
import 'package:alchemist/src/golden_test_scenario.dart';
import 'package:flutter/material.dart';

/// {@template golden_test_theme}
Expand All @@ -12,6 +13,7 @@ class GoldenTestTheme extends ThemeExtension<GoldenTestTheme> {
GoldenTestTheme({
required this.backgroundColor,
required this.borderColor,
required this.nameTextStyle,
});

/// The standard theme for golden tests, used when no other theme is provided.
Expand All @@ -21,6 +23,7 @@ class GoldenTestTheme extends ThemeExtension<GoldenTestTheme> {
// change out from under us, which would cause golden tests to fail.
backgroundColor: const Color(0xFF2b54a1),
borderColor: const Color(0xFF3d394a),
nameTextStyle: const TextStyle(fontSize: 18),
);
}

Expand All @@ -30,14 +33,19 @@ class GoldenTestTheme extends ThemeExtension<GoldenTestTheme> {
/// The border color used to separate scenarios in a [GoldenTestGroup].
final Color borderColor;

/// The text style that is used to show the name in a [GoldenTestScenario]
final TextStyle nameTextStyle;

@override
ThemeExtension<GoldenTestTheme> copyWith({
Color? backgroundColor,
Color? borderColor,
TextStyle? nameTextStyle,
}) {
return GoldenTestTheme(
backgroundColor: backgroundColor ?? this.backgroundColor,
borderColor: borderColor ?? this.borderColor,
nameTextStyle: nameTextStyle ?? this.nameTextStyle,
);
}

Expand All @@ -53,6 +61,10 @@ class GoldenTestTheme extends ThemeExtension<GoldenTestTheme> {
backgroundColor: Color.lerp(backgroundColor, other.backgroundColor, t) ??
backgroundColor,
borderColor: Color.lerp(borderColor, other.borderColor, t) ?? borderColor,
nameTextStyle: nameTextStyle.copyWith(
color: Color.lerp(nameTextStyle.color, other.nameTextStyle.color, t) ??
nameTextStyle.color,
),
);
}
}
5 changes: 5 additions & 0 deletions test/src/golden_test_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ void main() {
final goldenTestTheme = GoldenTestTheme(
backgroundColor: Colors.blueGrey,
borderColor: Colors.orange,
nameTextStyle: const TextStyle(
color: Colors.black,
fontSize: 14,
fontWeight: ui.FontWeight.bold,
),
);
const ciRenderShadows = true;
final config = AlchemistConfig(
Expand Down
72 changes: 72 additions & 0 deletions test/src/golden_test_theme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ void main() {
goldenTestTheme: GoldenTestTheme(
backgroundColor: Colors.green,
borderColor: const Color(0xFF000000),
nameTextStyle: const TextStyle(
color: Colors.black,
fontSize: 18,
),
),
);

Expand Down Expand Up @@ -117,6 +121,10 @@ void main() {
goldenTestTheme: GoldenTestTheme(
backgroundColor: const Color(0xFF000000),
borderColor: Colors.green,
nameTextStyle: const TextStyle(
color: Color(0xFFFFFFFF),
fontSize: 18,
),
),
pumpBeforeTest: onlyPumpAndSettle,
pumpWidget: onlyPumpWidget,
Expand All @@ -135,6 +143,70 @@ void main() {
});
});
});
group('nameTextStyle', () {
group('when no override is provided', () {
testWidgets('it renders as default', (tester) async {
const adapter = FlutterGoldenTestAdapter();

await adapter.pumpGoldenTest(
tester: tester,
textScaleFactor: 1,
constraints: const BoxConstraints(),
obscureFont: true,
globalConfigTheme: null,
variantConfigTheme: null,
goldenTestTheme: null,
pumpBeforeTest: onlyPumpAndSettle,
pumpWidget: onlyPumpWidget,
widget: GoldenTestScenario(
name: 'Scenario name',
child: const Text('some text'),
),
);

final box = find.text('Scenario name');
expect(
tester.widget<Text>(box).style,
GoldenTestTheme.standard().nameTextStyle,
);
});
});

group('when an override is provided', () {
testWidgets('it renders as the provided color', (tester) async {
const adapter = FlutterGoldenTestAdapter();
const nameTextStyle = TextStyle(
color: Color.fromARGB(255, 248, 0, 0),
fontSize: 18,
fontWeight: FontWeight.bold,
);
await adapter.pumpGoldenTest(
tester: tester,
textScaleFactor: 1,
constraints: const BoxConstraints(),
obscureFont: true,
globalConfigTheme: null,
variantConfigTheme: null,
goldenTestTheme: GoldenTestTheme(
backgroundColor: const Color(0xFF000000),
borderColor: Colors.green,
nameTextStyle: nameTextStyle,
),
pumpBeforeTest: onlyPumpAndSettle,
pumpWidget: onlyPumpWidget,
widget: GoldenTestScenario(
name: 'Scenario name',
child: const Text('some text'),
),
);
final box = find.text('Scenario name');
expect(
tester.widget<Text>(box).style,
nameTextStyle,
);
});
});
});
});
});
}

0 comments on commit 4bc0fc5

Please sign in to comment.