Skip to content

Commit 659033f

Browse files
committed
test: Add widget tests for OnboardingScreen.
1 parent e1c46c8 commit 659033f

4 files changed

Lines changed: 108 additions & 6 deletions

File tree

test/presentation/login/login_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'package:codephile/presentation/login/bloc/login_bloc.dart';
55
import 'package:flutter_test/flutter_test.dart';
66

77
void blocTests() {
8-
group('LoginBloc - ', () {
8+
group('LoginBloc -', () {
99
setUpAll(ApiService.init);
1010

1111
test('emits correct initial state', () {
@@ -25,7 +25,7 @@ void blocTests() {
2525
);
2626
});
2727

28-
group('toggles - ', () {
28+
group('toggles -', () {
2929
blocTest<LoginBloc, LoginState>(
3030
'obscure password',
3131
build: () => LoginBloc(),
@@ -51,7 +51,7 @@ void blocTests() {
5151
);
5252
});
5353

54-
group('text fields - ', () {
54+
group('text fields -', () {
5555
blocTest<LoginBloc, LoginState>(
5656
'tracks focus correctly',
5757
build: () => LoginBloc(),
@@ -111,7 +111,7 @@ void blocTests() {
111111
expect(bloc.state.isLoginButtonActive(), false);
112112
});
113113

114-
group('forgot password dialog - ', () {
114+
group('forgot password dialog -', () {
115115
blocTest<LoginBloc, LoginState>(
116116
'opens dialog',
117117
build: () => LoginBloc(),
@@ -144,7 +144,7 @@ void blocTests() {
144144
);
145145
});
146146

147-
group('submit - ', () {
147+
group('submit -', () {
148148
blocTest<LoginBloc, LoginState>(
149149
'does nothing if fields are empty',
150150
build: () => LoginBloc(),
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import 'package:card_swiper/card_swiper.dart';
2+
import 'package:codephile/presentation/onboarding/onboarding_screen.dart';
3+
import 'package:codephile/presentation/onboarding/widgets/onboarding_widgets.dart';
4+
import 'package:flutter/material.dart';
5+
import 'package:flutter_test/flutter_test.dart';
6+
7+
import '../../utils/pump_screen.dart';
8+
9+
void widgetTests() {
10+
group('OnboardingScreen -', () {
11+
testWidgets('contains sub-widgets', (tester) async {
12+
await pumpScreen(tester, () => OnboardingScreen());
13+
14+
expect(find.byType(BackgroundDecoration), findsOneWidget);
15+
expect(find.byType(NextButton), findsOneWidget);
16+
expect(find.byType(Swiper), findsOneWidget);
17+
expect(find.byType(OnboardingPage), findsOneWidget);
18+
19+
/// The [PaginationDots] widget does not show up as anything special in
20+
/// the widget tree because of the idiosyncrasies of the package author.
21+
/// Hence this roundabout way of testing whether the widget is present.
22+
expect(
23+
find.byWidgetPredicate(
24+
(widget) => widget is Align && widget.child is Row,
25+
),
26+
findsOneWidget,
27+
);
28+
});
29+
30+
testWidgets("contains first feature's data", (tester) async {
31+
await pumpScreen(tester, () => OnboardingScreen());
32+
33+
expect(find.text(pages[0].title), findsOneWidget);
34+
expect(find.text(pages[0].description), findsOneWidget);
35+
expect(
36+
find.byWidgetPredicate(
37+
(widget) =>
38+
widget is Image &&
39+
widget.image is AssetImage &&
40+
(widget.image as AssetImage).assetName == pages[0].asset,
41+
),
42+
findsOneWidget,
43+
);
44+
});
45+
46+
testWidgets('pressing NextButton takes user to next page', (tester) async {
47+
await pumpScreen(tester, () => OnboardingScreen());
48+
49+
for (var i = 0; i < pages.length; i++) {
50+
// Finds i^th feature's data
51+
expect(find.text(pages[i].title), findsOneWidget);
52+
expect(find.text(pages[i].description), findsOneWidget);
53+
expect(
54+
find.byWidgetPredicate(
55+
(widget) =>
56+
widget is Image &&
57+
widget.image is AssetImage &&
58+
(widget.image as AssetImage).assetName == pages[i].asset,
59+
),
60+
findsOneWidget,
61+
);
62+
63+
await tester.tap(find.byType(NextButton));
64+
await tester.pumpAndSettle();
65+
}
66+
67+
// TODO(BURG3R5): Uncomment the next line when a HomeScreen widget has been created.
68+
// expect(find.byType(HomeScreen), findsOneWidget);
69+
});
70+
});
71+
}

test/presentation/widget_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
void main() {}
1+
import 'onboarding/onboarding_test.dart' as onboarding;
2+
3+
void main() {
4+
onboarding.widgetTests();
5+
}

test/utils/pump_screen.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'package:codephile/presentation/core/router.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_screenutil/flutter_screenutil.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
import 'package:get/get.dart';
6+
7+
/// Pumps a screen, wrapping it with the necessary configuration widgets.
8+
Future<void> pumpScreen(WidgetTester tester, Widget Function() screen) async {
9+
await tester.pumpWidget(
10+
ScreenUtilInit(
11+
designSize: const Size(360, 640),
12+
builder: () {
13+
return GetMaterialApp(
14+
builder: (context, widget) {
15+
ScreenUtil.setContext(context);
16+
return MediaQuery(
17+
data: MediaQuery.of(context).copyWith(textScaleFactor: 1),
18+
child: widget!,
19+
);
20+
},
21+
onGenerateRoute: AppRouter.generateRoute,
22+
home: screen(),
23+
);
24+
},
25+
),
26+
);
27+
}

0 commit comments

Comments
 (0)