Skip to content

Commit 7c7957b

Browse files
authored
Merge pull request #54 from deepikahr/button_testing
test cases added for button
2 parents fe937bc + 2989b44 commit 7c7957b

File tree

5 files changed

+271
-65
lines changed

5 files changed

+271
-65
lines changed

lib/components/loader/gf_loader.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class GFLoader extends StatefulWidget {
1818
this.loaderstrokeWidth = 4.0,
1919
this.size = GFSize.MEDIUM,
2020
this.child})
21-
: super(key: key);
21+
: assert(type != null),
22+
super(key: key);
2223

2324
/// Type of [Widget] used only in custom type and it is prominent over the loaderIconOne, loaderIconTwo, loaderIconThree in custom type
2425
final Widget child;

lib/components/shimmer/gf_shimmer.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class GFShimmer extends StatefulWidget {
1515
this.showGradient = false,
1616
this.mainColor = Colors.grey,
1717
this.secondaryColor = GFColors.LIGHT,
18-
}) : super(key: key);
18+
}) : assert(child != null),
19+
super(key: key);
1920

2021
/// The child of type [Widget] to display shimmer effect.
2122
final Widget child;

test/button_test.dart

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:getwidget/getwidget.dart';
5+
6+
void main() {
7+
const text = 'click me';
8+
const size = GFSize.SMALL;
9+
const childWidget = Text('tap me');
10+
11+
testWidgets('GF Button without padding, shape', (tester) async {
12+
// `GFBUtton.shape` null.
13+
expect(
14+
() => GFButton(
15+
onPressed: null,
16+
shape: null,
17+
padding: null,
18+
),
19+
throwsAssertionError,
20+
);
21+
});
22+
23+
testWidgets('GF Button can be constructed', (tester) async {
24+
const butttonKey = Key('header');
25+
26+
const GFButton button = GFButton(
27+
key: butttonKey,
28+
onPressed: null,
29+
text: text,
30+
size: size,
31+
child: childWidget,
32+
);
33+
34+
const TestApp app = TestApp(button);
35+
36+
await tester.pumpWidget(app);
37+
38+
expect(find.text('click me'), findsOneWidget);
39+
40+
expect(app.button.text, text);
41+
expect(app.button.size, size);
42+
43+
await tester.tap(find.byKey(butttonKey));
44+
await tester.pump();
45+
46+
await tester.pumpWidget(app);
47+
});
48+
49+
testWidgets('GF Button can be constructed', (tester) async {
50+
const butttonKey = Key('header');
51+
52+
const GFButton button = GFButton(
53+
key: butttonKey,
54+
onPressed: null,
55+
text: text,
56+
size: size,
57+
child: childWidget,
58+
);
59+
60+
const TestApp app = TestApp(button);
61+
62+
await tester.pumpWidget(app);
63+
64+
expect(find.text('click me'), findsOneWidget);
65+
66+
expect(app.button.child, childWidget);
67+
expect(app.button.text, text);
68+
expect(app.button.size, size);
69+
70+
await tester.tap(find.byKey(butttonKey));
71+
await tester.press(find.byKey(butttonKey));
72+
await tester.longPress(find.byKey(butttonKey));
73+
await tester.pump();
74+
75+
await tester.pumpWidget(app);
76+
});
77+
78+
// testWidgets('GF Button with custom shape',
79+
// (WidgetTester tester) async {
80+
//
81+
// const GFButton button = GFButton(
82+
// onPressed: null,
83+
// text: text,
84+
// size: size,
85+
// child: childWidget,
86+
// );
87+
//
88+
// const TestApp app = TestApp(button);
89+
//
90+
// await tester.pumpWidget(app);
91+
//
92+
// final roundedRectangleBorder = tester
93+
// .widget<GFButton>(find.byType(Card))
94+
// .borderShape as RoundedRectangleBorder;
95+
// expect(roundedRectangleBorder.borderRadius, BorderRadius.circular(15));
96+
// });
97+
}
98+
99+
class TestApp extends StatefulWidget {
100+
const TestApp(this.button);
101+
102+
final GFButton button;
103+
104+
@override
105+
_TestAppState createState() => _TestAppState();
106+
}
107+
108+
class _TestAppState extends State<TestApp> {
109+
@override
110+
Widget build(BuildContext context) => MaterialApp(
111+
home: Scaffold(
112+
body: Column(
113+
children: [
114+
widget.button,
115+
],
116+
),
117+
),
118+
);
119+
}

test/loader_test.dart

Lines changed: 28 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,6 @@ void main() {
2323

2424
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
2525

26-
// testWidgets('Asserts.', (tester) async {
27-
// expect(
28-
// () => GFLoader(
29-
//
30-
// ),
31-
// throwsAssertionError,
32-
// );
33-
//
34-
// expect(
35-
// () => GFLoader(
36-
//
37-
// ),
38-
// throwsAssertionError,
39-
// );
40-
// });
41-
4226
testWidgets('GF Loader can be constructed', (tester) async {
4327
final GFLoader loader = GFLoader(
4428
loaderColorOne: firstColor,
@@ -102,19 +86,6 @@ void main() {
10286
expect(app.loader.loaderstrokeWidth, stroke);
10387
});
10488

105-
// testWidgets('Asserts.', (tester) async {
106-
// // when type is null
107-
//
108-
// expect(() => GFLoader(
109-
// type: null,
110-
// loaderIconOne : iconOne,
111-
// loaderIconTwo: iconTwo,
112-
// loaderIconThree: iconThree,
113-
// ),
114-
// throwsAssertionError,
115-
// );
116-
// });
117-
11889
testWidgets('GF Loader with square type can be constructed', (tester) async {
11990
const customType = GFLoaderType.square;
12091

@@ -181,40 +152,6 @@ void main() {
181152
expect(app.loader.androidLoaderColor, color);
182153
});
183154

184-
// testWidgets('Asserts.', (tester) async {
185-
// // when type is null
186-
//
187-
// // expect(() => GFLoader(
188-
// // type: null,
189-
// // loaderIconOne : iconOne,
190-
// // loaderIconTwo: iconTwo,
191-
// // loaderIconThree: iconThree,
192-
// // ),
193-
// // throwsAssertionError,
194-
// //
195-
// // );
196-
//
197-
// const GFLoader loader = GFLoader(
198-
// type: null,
199-
// loaderIconOne : iconOne,
200-
// loaderIconTwo: iconTwo,
201-
// loaderIconThree: iconThree,
202-
// );
203-
//
204-
// const TestApp app = TestApp(loader);
205-
//
206-
// await tester.pumpWidget(app);
207-
//
208-
// expect(
209-
// tester.takeException(),
210-
// isA<FlutterError>().having(
211-
// (error) => error.message,
212-
// 'message',
213-
// 'Type should be custom for icons loader to display',
214-
// ),
215-
// );
216-
// });
217-
218155
testWidgets('GF Loader with custom loader can be constructed using child',
219156
(tester) async {
220157
const customType = GFLoaderType.custom;
@@ -232,6 +169,34 @@ void main() {
232169
expect(app.loader.child, childWidget);
233170
expect(app.loader.type, customType);
234171
});
172+
173+
testWidgets('Custom GF Loader can be constructed with wrong type',
174+
(tester) async {
175+
const GFLoader loader = GFLoader(
176+
type: GFLoaderType.custom,
177+
loaderIconOne: iconOne,
178+
);
179+
180+
const TestApp app = TestApp(loader);
181+
182+
await tester.pumpWidget(app);
183+
// expect(app.loader.type, GFLoaderType.android);
184+
expect(app.loader.type, GFLoaderType.custom, reason: 'custom icon');
185+
expect(app.loader.loaderIconOne, iconOne);
186+
});
187+
188+
testWidgets('GF Loader can be constructed without type', (tester) async {
189+
// `GFLoader.type` null.
190+
expect(
191+
() => GFLoader(
192+
type: null,
193+
loaderIconOne: iconOne,
194+
loaderIconTwo: iconTwo,
195+
loaderIconThree: iconThree,
196+
),
197+
throwsAssertionError,
198+
);
199+
});
235200
}
236201

237202
class TestApp extends StatefulWidget {

test/shimmer_test.dart

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:getwidget/getwidget.dart';
4+
5+
void main() {
6+
final childWidget = Container(
7+
width: 111,
8+
height: 222,
9+
);
10+
final firstColor = Colors.teal;
11+
final secondColor = Colors.tealAccent;
12+
13+
const count = 5;
14+
const duration = Duration(milliseconds: 3000);
15+
16+
final gradient = LinearGradient(
17+
begin: Alignment.bottomRight,
18+
end: Alignment.centerLeft,
19+
stops: const <double>[0, 0.3, 0.6, 0.9, 1],
20+
colors: [
21+
Colors.teal[100],
22+
Colors.teal[200],
23+
Colors.teal[300],
24+
Colors.teal[400],
25+
Colors.teal[500],
26+
],
27+
);
28+
29+
testWidgets('GF Shimmer without child', (tester) async {
30+
// `GFShimmer.type` null.
31+
expect(
32+
() => GFShimmer(child: null),
33+
throwsAssertionError,
34+
);
35+
});
36+
37+
testWidgets('GF Shimmer can be constructed', (tester) async {
38+
// final childWidget = Icon(Icons.directions_bike_sharp, size: 45,);
39+
40+
final GFShimmer shimmer = GFShimmer(
41+
child: childWidget,
42+
mainColor: firstColor,
43+
secondaryColor: secondColor,
44+
shimmerEffectCount: count,
45+
duration: duration,
46+
);
47+
48+
final TestApp app = TestApp(shimmer);
49+
50+
await tester.pumpWidget(app);
51+
await tester.pump(duration);
52+
53+
await tester.pumpWidget(Container(child: childWidget));
54+
expect(find.byWidget(childWidget), findsOneWidget);
55+
});
56+
57+
testWidgets('GF Shimmer can be constructed withh icon widget',
58+
(tester) async {
59+
final GFShimmer shimmer = GFShimmer(
60+
child: const Icon(
61+
Icons.directions_bike_sharp,
62+
size: 45,
63+
),
64+
mainColor: firstColor,
65+
secondaryColor: secondColor,
66+
shimmerEffectCount: count,
67+
);
68+
69+
final TestApp app = TestApp(shimmer);
70+
71+
await tester.pumpWidget(app);
72+
73+
expect(find.byIcon(Icons.directions_bike_sharp), findsOneWidget);
74+
});
75+
76+
test('should emit the right values for shimmerEffectCount', () async {
77+
final GFShimmer shimmer = GFShimmer(
78+
child: childWidget,
79+
mainColor: firstColor,
80+
secondaryColor: secondColor,
81+
shimmerEffectCount: count,
82+
);
83+
expect(shimmer.shimmerEffectCount, count);
84+
});
85+
86+
test('GF Shimmer with gradient', () async {
87+
final GFShimmer shimmer = GFShimmer(
88+
child: childWidget,
89+
mainColor: firstColor,
90+
secondaryColor: secondColor,
91+
shimmerEffectCount: count,
92+
showGradient: true,
93+
gradient: gradient,
94+
);
95+
expect(shimmer.showGradient, isTrue);
96+
expect(shimmer.gradient, gradient);
97+
});
98+
}
99+
100+
class TestApp extends StatefulWidget {
101+
const TestApp(this.shimmer);
102+
103+
final GFShimmer shimmer;
104+
105+
@override
106+
_TestAppState createState() => _TestAppState();
107+
}
108+
109+
class _TestAppState extends State<TestApp> {
110+
@override
111+
Widget build(BuildContext context) => MaterialApp(
112+
home: Scaffold(
113+
body: Column(
114+
children: [
115+
widget.shimmer,
116+
],
117+
),
118+
),
119+
);
120+
}

0 commit comments

Comments
 (0)