From 5670d99d06fcfa60bb83c635384df479908d020e Mon Sep 17 00:00:00 2001 From: Daniel Molnar <77627178+danielmolnar@users.noreply.github.com> Date: Thu, 18 Jan 2024 16:29:06 +0100 Subject: [PATCH] Add has effective text style (#37) --- lib/src/spot/effective/effective_text.dart | 11 +++++ test/widgets/effective_text_test.dart | 51 ++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/lib/src/spot/effective/effective_text.dart b/lib/src/spot/effective/effective_text.dart index 7d146fe4..93cac119 100644 --- a/lib/src/spot/effective/effective_text.dart +++ b/lib/src/spot/effective/effective_text.dart @@ -39,6 +39,17 @@ extension EffectiveTextMatcher on WidgetMatcher { match: match.hideNullability(), ); } + + /// Matches the style of a [Text] against a given [TextStyle]. + WidgetMatcher hasEffectiveTextStyle(TextStyle? value) { + return hasEffectiveTextStyleWhere((it) { + if (value == null) { + it.isNull(); + } else { + it.equals(value); + } + }); + } } extension TextStyleSubject on Subject { diff --git a/test/widgets/effective_text_test.dart b/test/widgets/effective_text_test.dart index 427ff3bf..a26cb33e 100644 --- a/test/widgets/effective_text_test.dart +++ b/test/widgets/effective_text_test.dart @@ -172,5 +172,56 @@ void main() { ]), ); }); + + testWidgets('Match against complete TextStyle', (widgetTester) async { + final style = TextStyle( + fontSize: 20, + fontStyle: FontStyle.italic, + fontWeight: FontWeight.bold, + letterSpacing: 2, + ); + await widgetTester.pumpWidget( + MaterialApp( + home: DefaultTextStyle( + style: style, + child: Text(''), + ), + ), + ); + spot().existsOnce().hasEffectiveTextStyle(style); + }); + + testWidgets( + 'Failed matching against complete TextStyle shows current values', + (widgetTester) async { + final style = TextStyle( + fontSize: 20, + fontStyle: FontStyle.italic, + fontWeight: FontWeight.bold, + letterSpacing: 2, + ); + await widgetTester.pumpWidget( + MaterialApp( + home: DefaultTextStyle( + style: style, + child: Text(''), + ), + ), + ); + + expect( + () => spot().existsOnce().hasEffectiveTextStyle( + style.copyWith( + fontSize: 16, + fontStyle: FontStyle.normal, + fontWeight: FontWeight.normal, + letterSpacing: 1, + ), + ), + throwsSpotErrorContaining([ + 'has "textStyle" that: equals , actual: ', + ]), + ); + }); }); }