Main coves client
1import 'package:coves_flutter/widgets/sign_in_dialog.dart';
2import 'package:flutter/material.dart';
3import 'package:flutter_test/flutter_test.dart';
4
5void main() {
6 group('SignInDialog', () {
7 testWidgets('should display default title and message', (tester) async {
8 await tester.pumpWidget(
9 MaterialApp(
10 home: Scaffold(
11 body: Builder(
12 builder: (context) {
13 return ElevatedButton(
14 onPressed: () => SignInDialog.show(context),
15 child: const Text('Show Dialog'),
16 );
17 },
18 ),
19 ),
20 ),
21 );
22
23 // Tap button to show dialog
24 await tester.tap(find.text('Show Dialog'));
25 await tester.pumpAndSettle();
26
27 // Verify default title and message
28 expect(find.text('Sign in required'), findsOneWidget);
29 expect(
30 find.text('You need to sign in to interact with posts.'),
31 findsOneWidget,
32 );
33 });
34
35 testWidgets('should display custom title and message', (tester) async {
36 await tester.pumpWidget(
37 MaterialApp(
38 home: Scaffold(
39 body: Builder(
40 builder: (context) {
41 return ElevatedButton(
42 onPressed: () => SignInDialog.show(
43 context,
44 title: 'Custom Title',
45 message: 'Custom message here',
46 ),
47 child: const Text('Show Dialog'),
48 );
49 },
50 ),
51 ),
52 ),
53 );
54
55 // Tap button to show dialog
56 await tester.tap(find.text('Show Dialog'));
57 await tester.pumpAndSettle();
58
59 // Verify custom title and message
60 expect(find.text('Custom Title'), findsOneWidget);
61 expect(find.text('Custom message here'), findsOneWidget);
62 });
63
64 testWidgets('should have Cancel and Sign In buttons', (tester) async {
65 await tester.pumpWidget(
66 MaterialApp(
67 home: Scaffold(
68 body: Builder(
69 builder: (context) {
70 return ElevatedButton(
71 onPressed: () => SignInDialog.show(context),
72 child: const Text('Show Dialog'),
73 );
74 },
75 ),
76 ),
77 ),
78 );
79
80 // Tap button to show dialog
81 await tester.tap(find.text('Show Dialog'));
82 await tester.pumpAndSettle();
83
84 // Verify buttons exist
85 expect(find.text('Cancel'), findsOneWidget);
86 expect(find.text('Sign In'), findsOneWidget);
87 });
88
89 testWidgets('should return false when Cancel is tapped', (tester) async {
90 bool? result;
91
92 await tester.pumpWidget(
93 MaterialApp(
94 home: Scaffold(
95 body: Builder(
96 builder: (context) {
97 return ElevatedButton(
98 onPressed: () async {
99 result = await SignInDialog.show(context);
100 },
101 child: const Text('Show Dialog'),
102 );
103 },
104 ),
105 ),
106 ),
107 );
108
109 // Tap button to show dialog
110 await tester.tap(find.text('Show Dialog'));
111 await tester.pumpAndSettle();
112
113 // Tap Cancel button
114 await tester.tap(find.text('Cancel'));
115 await tester.pumpAndSettle();
116
117 // Verify result is false
118 expect(result, false);
119
120 // Dialog should be dismissed
121 expect(find.text('Sign in required'), findsNothing);
122 });
123
124 testWidgets('should return true when Sign In is tapped', (tester) async {
125 bool? result;
126
127 await tester.pumpWidget(
128 MaterialApp(
129 home: Scaffold(
130 body: Builder(
131 builder: (context) {
132 return ElevatedButton(
133 onPressed: () async {
134 result = await SignInDialog.show(context);
135 },
136 child: const Text('Show Dialog'),
137 );
138 },
139 ),
140 ),
141 ),
142 );
143
144 // Tap button to show dialog
145 await tester.tap(find.text('Show Dialog'));
146 await tester.pumpAndSettle();
147
148 // Tap Sign In button
149 await tester.tap(find.text('Sign In'));
150 await tester.pumpAndSettle();
151
152 // Verify result is true
153 expect(result, true);
154
155 // Dialog should be dismissed
156 expect(find.text('Sign in required'), findsNothing);
157 });
158
159 testWidgets('should dismiss when tapped outside (barrier)', (tester) async {
160 bool? result;
161
162 await tester.pumpWidget(
163 MaterialApp(
164 home: Scaffold(
165 body: Builder(
166 builder: (context) {
167 return ElevatedButton(
168 onPressed: () async {
169 result = await SignInDialog.show(context);
170 },
171 child: const Text('Show Dialog'),
172 );
173 },
174 ),
175 ),
176 ),
177 );
178
179 // Tap button to show dialog
180 await tester.tap(find.text('Show Dialog'));
181 await tester.pumpAndSettle();
182
183 // Tap outside the dialog (on the barrier)
184 await tester.tapAt(const Offset(10, 10));
185 await tester.pumpAndSettle();
186
187 // Verify result is null (dismissed without selecting an option)
188 expect(result, null);
189
190 // Dialog should be dismissed
191 expect(find.text('Sign in required'), findsNothing);
192 });
193
194 testWidgets('should use app colors for styling', (tester) async {
195 await tester.pumpWidget(
196 MaterialApp(
197 home: Scaffold(
198 body: Builder(
199 builder: (context) {
200 return ElevatedButton(
201 onPressed: () => SignInDialog.show(context),
202 child: const Text('Show Dialog'),
203 );
204 },
205 ),
206 ),
207 ),
208 );
209
210 // Tap button to show dialog
211 await tester.tap(find.text('Show Dialog'));
212 await tester.pumpAndSettle();
213
214 // Find the AlertDialog widget
215 final alertDialog = tester.widget<AlertDialog>(
216 find.byType(AlertDialog),
217 );
218
219 // Verify background color is set
220 expect(alertDialog.backgroundColor, isNotNull);
221
222 // Find the Sign In button
223 final signInButton = tester.widget<ElevatedButton>(
224 find.widgetWithText(ElevatedButton, 'Sign In'),
225 );
226
227 // Verify button styling
228 expect(signInButton.style, isNotNull);
229 });
230 });
231}