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