Skip to content

Commit

Permalink
Add diagnostic prop tests
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy committed Jan 27, 2024
1 parent 03e6112 commit 0a0c8d2
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions test/selectors/diagnostic_prop_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:spot/spot.dart';

import '../util/assert_error.dart';

void main() {
group('diagnostic prop', () {
testWidgets('getDiagnosticProp', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: Text('a', maxLines: 4),
),
);

final maxLines =
spot<Text>().existsOnce().getDiagnosticProp<int>('maxLines');
expect(maxLines, 4);
});

testWidgets('generated getDiagnosticProp', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: Text('a', maxLines: 4),
),
);

final maxLines2 = spot<Text>().existsOnce().getMaxLines();
expect(maxLines2, 4);
});

testWidgets('hasDiagnosticProp', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: Text('a', maxLines: 4),
),
);

spot<Text>()
.existsOnce()
.hasDiagnosticProp<int>('maxLines', (it) => it.equals(4));

expect(
() => spot<Text>()
.existsOnce()
.hasDiagnosticProp<int>('maxLines', (it) => it.equals(2)),
throwsSpotErrorContaining([
'Text with property maxLines',
'equals <2>, actual: <4>',
]),
);
});

testWidgets('generated hasDiagnosticProp', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: Text('a', maxLines: 4),
),
);

spot<Text>()
.existsOnce()
.hasMaxLines(4)
.hasMaxLinesWhere((it) => it.equals(4));

expect(
() => spot<Text>().existsOnce().hasMaxLines(2),
throwsSpotErrorContaining([
'Text with property maxLines',
'equals <2>, actual: <4>',
]),
);
});

testWidgets('withDiagnosticProp', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: Text('a', maxLines: 4),
),
);

spot<Text>()
.withDiagnosticProp<int>('maxLines', (it) => it.equals(4))
.existsOnce();

expect(
() => spot<Text>()
.withDiagnosticProp<int>('maxLines', (it) => it.equals(2))
.existsOnce(),
throwsSpotErrorContaining([
'Could not find Text with prop "maxLines" equals <2> in widget tree',
'A less specific search (Text) discovered 1 matches!',
'Text("a", maxLines: 4,',
]),
);
});

testWidgets('generated withDiagnosticProp', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: Text('a', maxLines: 4),
),
);

spot<Text>().withMaxLines(4).existsOnce();
spot<Text>().whereMaxLines((it) => it.equals(4)).existsOnce();

spot<Text>().withMaxLines(3).doesNotExist();
spot<Text>().whereMaxLines((it) => it.equals(3)).doesNotExist();

expect(
() => spot<Text>().withMaxLines(2).existsOnce(),
throwsSpotErrorContaining([
'Could not find Text with prop "maxLines" equals <2> in widget tree',
'A less specific search (Text) discovered 1 matches!',
'Text("a", maxLines: 4,',
]),
);
});
});
}

0 comments on commit 0a0c8d2

Please sign in to comment.