Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add effective text style selectors #38

Merged
merged 5 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/src/spot/effective/effective_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import 'package:spot/spot.dart';
import 'package:spot/src/spot/element_extensions.dart';
import 'package:spot/src/spot/selectors.dart';

/// Matchers for the [Text] widget to make assertions about:
/// - [Text.maxLines]
/// - [Text.textStyle]
extension EffectiveTextMatcher on WidgetMatcher<Text> {
/// Matches the [Text] widget when it has the given [maxLines].
///
Expand All @@ -20,6 +23,7 @@ extension EffectiveTextMatcher on WidgetMatcher<Text> {
);
}

/// Matches the [Text] widget for given maxLines.
WidgetMatcher<Text> hasEffectiveMaxLines(int? value) {
return hasEffectiveMaxLinesWhere((it) {
if (value == null) {
Expand All @@ -30,6 +34,7 @@ extension EffectiveTextMatcher on WidgetMatcher<Text> {
});
}

/// Matches the [Text] widget when it has the given [TextStyle].
WidgetMatcher<Text> hasEffectiveTextStyleWhere(MatchProp<TextStyle> match) {
return hasProp(
elementSelector: (subject) => subject.context.nest<TextStyle?>(
Expand Down Expand Up @@ -82,7 +87,11 @@ extension TextStyleSubject on Subject<TextStyle> {
}
}

/// Selectors for the [Text] widget like
/// - [Text.maxLines]
/// - [Text.textStyle]
extension EffectiveTextSelector on WidgetSelector<Text> {
/// Selects the [Text] widget where given `maxLines` properties match.
WidgetSelector<Text> withEffectiveMaxLinesMatching(MatchProp<int?> match) {
return withProp(
elementSelector: (subject) => subject.context.nest<int?>(
Expand All @@ -93,9 +102,34 @@ extension EffectiveTextSelector on WidgetSelector<Text> {
);
}

/// Selects a [Text] widget with given `maxLines`.
WidgetSelector<Text> withEffectiveMaxLines(int? value) {
return withEffectiveMaxLinesMatching((it) => it.equals(value));
}

/// Selects the [Text] widget where given [TextStyle] properties match.
WidgetSelector<Text> withEffectiveTextStyleMatching(
MatchProp<TextStyle> match,
) {
return withProp(
elementSelector: (subject) => subject.context.nest<TextStyle>(
() => ['with "textStyle"'],
(Element element) => Extracted.value(_extractTextStyle(element)),
),
match: match,
);
}

/// Selects a [Text] widget with a given [TextStyle].
WidgetSelector<Text> withEffectiveTextStyle(TextStyle? value) {
return withEffectiveTextStyleMatching((it) {
if (value == null) {
it.isNull();
} else {
it.equals(value);
}
});
}
}

int? _extractMaxLines(Element element) {
Expand Down
89 changes: 89 additions & 0 deletions test/widgets/effective_text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,94 @@ void main() {
]),
);
});

testWidgets(
'Select with TextStyle',
(widgetTester) async {
final style = TextStyle(
fontSize: 20,
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
letterSpacing: 2,
);

await widgetTester.pumpWidget(
MaterialApp(
home: Column(
children: [
Text(
'Great Text',
style: TextStyle(fontSize: 20),
),
DefaultTextStyle(
style: style,
child: Text('Great Text'),
),
],
),
),
);

// Select with single props
spot<Text>().withText('Great Text').withEffectiveTextStyleMatching(
(style) {
style.fontSize.equals(20);
style.fontStyle.equals(FontStyle.italic);
style.fontWeight.equals(FontWeight.bold);
style.letterSpacing.equals(2);
},
).existsOnce();

// Select with complete TextStyle
spot<Text>()
.withText('Great Text')
.withEffectiveTextStyle(style)
.existsOnce();
},
);

testWidgets('Failed selection with TextStyle shows missing 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('Great Text'),
),
),
);

expect(
() =>
spot<Text>().withText('Great Text').withEffectiveTextStyleMatching(
(style) {
style.fontSize.equals(20);
style.fontStyle.equals(FontStyle.normal);
style.fontWeight.equals(FontWeight.bold);
style.letterSpacing.equals(2);
},
).existsOnce(),
throwsSpotErrorContaining([
'with "textStyle" that: has fontSize that: equals <20.0> has fontStyle that: equals <FontStyle.normal> has fontWeight that: equals <FontWeight.w700> has letterSpacing that: equals <2.0>',
]),
);

expect(
() => spot<Text>()
.withText('Great Text')
.withEffectiveTextStyle(style.copyWith(fontStyle: FontStyle.normal))
.existsOnce(),
throwsSpotErrorContaining([
'with "textStyle" that: equals <TextStyle(inherit: true, size: 20.0, weight: 700, style: normal, letterSpacing: 2.0)>',
]),
);
});
});
}