Created
August 4, 2021 05:12
-
-
Save mrverdant13/89d4117165cc070c848d5a2754852dfa to your computer and use it in GitHub Desktop.
`setState` coverage test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class SetStateScreen extends StatelessWidget { | |
const SetStateScreen({ | |
Key? key, | |
}) : super(key: key); | |
@override | |
Widget build(BuildContext context) => Scaffold( | |
appBar: AppBar( | |
title: const Text('`setState` test'), | |
), | |
body: const Body(), | |
); | |
} | |
class Body extends StatefulWidget { | |
const Body({ | |
Key? key, | |
}) : super(key: key); | |
@override | |
_BodyState createState() => _BodyState(); | |
} | |
class _BodyState extends State<Body> { | |
String? _myString; | |
@override | |
Widget build(BuildContext context) => Column( | |
children: [ | |
Expanded( | |
child: Center( | |
child: SetterWidget( | |
onCreate: (value) => setState( | |
() => _myString = value, | |
), | |
), | |
), | |
), | |
Expanded( | |
child: Center( | |
child: Text('Actual value: <$_myString>'), | |
), | |
), | |
Expanded( | |
child: Center( | |
child: _myString == null | |
? const NullIndicatorWidget() | |
: const NonNullIndicatorWidget(), | |
), | |
), | |
], | |
); | |
} | |
class SetterWidget extends StatefulWidget { | |
const SetterWidget({ | |
Key? key, | |
required this.onCreate, | |
}) : super(key: key); | |
final void Function(String? value) onCreate; | |
@override | |
_SetterWidgetState createState() => _SetterWidgetState(); | |
} | |
class _SetterWidgetState extends State<SetterWidget> { | |
@override | |
void initState() { | |
super.initState(); | |
Future.delayed( | |
const Duration(seconds: 2), | |
).then( | |
(_) => widget.onCreate('New value'), | |
); | |
} | |
@override | |
Widget build(BuildContext context) => Text('Setter widget'); | |
} | |
class NullIndicatorWidget extends StatelessWidget { | |
const NullIndicatorWidget({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) => const Text('The value is null'); | |
} | |
class NonNullIndicatorWidget extends StatelessWidget { | |
const NonNullIndicatorWidget({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) => const Text('The value is not null'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_playground/set_state_test/screen.dart'; | |
import 'package:flutter_test/flutter_test.dart'; | |
void main() { | |
testWidgets( | |
'Async `setState` test', | |
(tester) async { | |
await tester.pumpWidget( | |
MaterialApp( | |
home: const Body(), | |
), | |
); | |
final nullValueIndicatorFinder = find.byType(NullIndicatorWidget); | |
expect(nullValueIndicatorFinder, findsOneWidget); | |
await tester.pump(const Duration(seconds: 2)); | |
final nonNullValueIndicatorFinder = find.byType(NonNullIndicatorWidget); | |
expect(nonNullValueIndicatorFinder, findsOneWidget); | |
await tester.pump(const Duration(seconds: 1)); | |
}, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment