Skip to content

Commit

Permalink
Add has effective text style (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmolnar authored Jan 18, 2024
1 parent cc890d2 commit 5670d99
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/src/spot/effective/effective_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ extension EffectiveTextMatcher on WidgetMatcher<Text> {
match: match.hideNullability(),
);
}

/// Matches the style of a [Text] against a given [TextStyle].
WidgetMatcher<Text> hasEffectiveTextStyle(TextStyle? value) {
return hasEffectiveTextStyleWhere((it) {
if (value == null) {
it.isNull();
} else {
it.equals(value);
}
});
}
}

extension TextStyleSubject on Subject<TextStyle> {
Expand Down
51 changes: 51 additions & 0 deletions test/widgets/effective_text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Text>().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<Text>().existsOnce().hasEffectiveTextStyle(
style.copyWith(
fontSize: 16,
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal,
letterSpacing: 1,
),
),
throwsSpotErrorContaining([
'has "textStyle" that: equals <TextStyle(inherit: true, size: 16.0, weight: 400, style: normal, letterSpacing: 1.0)>, actual: <TextStyle(inherit: true, size: 20.0, weight: 700, style: italic, letterSpacing: 2.0)>',
]),
);
});
});
}

0 comments on commit 5670d99

Please sign in to comment.