|
| 1 | +import 'dart:async'; |
| 2 | + |
1 | 3 | import 'package:flutter/material.dart'; |
2 | 4 | import 'package:flutter/semantics.dart'; |
3 | 5 | import 'package:flutter_test/flutter_test.dart'; |
@@ -170,6 +172,110 @@ void main() { |
170 | 172 | expect(calls, ['Quick save']); |
171 | 173 | }); |
172 | 174 |
|
| 175 | + testWidgets( |
| 176 | + 'clearing an existing note is persisted even when the widget is ' |
| 177 | + 'disposed before the debounce fires', |
| 178 | + (tester) async { |
| 179 | + final calls = <String?>[]; |
| 180 | + await tester.pumpWidget( |
| 181 | + buildWidget( |
| 182 | + drink: createSampleDrink(notes: 'Existing'), |
| 183 | + onNotesChanged: (notes) async => calls.add(notes), |
| 184 | + ), |
| 185 | + ); |
| 186 | + |
| 187 | + await tester.tap(find.byKey(const ValueKey('user-notes-editor'))); |
| 188 | + await tester.pump(); |
| 189 | + await tester.enterText( |
| 190 | + find.byKey(const ValueKey('user-notes-field')), |
| 191 | + '', |
| 192 | + ); |
| 193 | + |
| 194 | + // Dispose the card before the debounce window elapses — the cleared |
| 195 | + // note must still be flushed exactly once, not silently dropped. |
| 196 | + await tester.pumpWidget(const MaterialApp(home: SizedBox.shrink())); |
| 197 | + |
| 198 | + expect(calls, [null]); |
| 199 | + }, |
| 200 | + ); |
| 201 | + |
| 202 | + testWidgets('a failed save is not reported as Saved and is retried', ( |
| 203 | + tester, |
| 204 | + ) async { |
| 205 | + var shouldFail = true; |
| 206 | + var attempts = 0; |
| 207 | + final saved = <String?>[]; |
| 208 | + await tester.pumpWidget( |
| 209 | + buildWidget( |
| 210 | + onNotesChanged: (notes) async { |
| 211 | + attempts++; |
| 212 | + if (shouldFail) throw Exception('write failed'); |
| 213 | + saved.add(notes); |
| 214 | + }, |
| 215 | + ), |
| 216 | + ); |
| 217 | + |
| 218 | + await tester.tap(find.byKey(const ValueKey('user-notes-editor'))); |
| 219 | + await tester.pump(); |
| 220 | + await tester.enterText( |
| 221 | + find.byKey(const ValueKey('user-notes-field')), |
| 222 | + 'Nice IPA', |
| 223 | + ); |
| 224 | + await tester.pump( |
| 225 | + YourTakeCard.notesDebounceDuration + const Duration(milliseconds: 50), |
| 226 | + ); |
| 227 | + await tester.pump(); |
| 228 | + |
| 229 | + expect(attempts, 1); |
| 230 | + expect(find.text('Saved'), findsNothing); |
| 231 | + |
| 232 | + // The edit stays pending: the next keystroke's flush retries it. |
| 233 | + shouldFail = false; |
| 234 | + await tester.enterText( |
| 235 | + find.byKey(const ValueKey('user-notes-field')), |
| 236 | + 'Nice IPA indeed', |
| 237 | + ); |
| 238 | + await tester.pump( |
| 239 | + YourTakeCard.notesDebounceDuration + const Duration(milliseconds: 50), |
| 240 | + ); |
| 241 | + await tester.pump(); |
| 242 | + |
| 243 | + expect(saved, ['Nice IPA indeed']); |
| 244 | + expect(find.text('Saved'), findsOneWidget); |
| 245 | + |
| 246 | + await tester.pump(YourTakeCard.savedIndicatorDuration); |
| 247 | + }); |
| 248 | + |
| 249 | + testWidgets( |
| 250 | + 'the note just typed stays visible after blur while the save is ' |
| 251 | + 'still in flight', |
| 252 | + (tester) async { |
| 253 | + final completer = Completer<void>(); |
| 254 | + await tester.pumpWidget( |
| 255 | + buildWidget(onNotesChanged: (_) => completer.future), |
| 256 | + ); |
| 257 | + |
| 258 | + await tester.tap(find.byKey(const ValueKey('user-notes-editor'))); |
| 259 | + await tester.pump(); |
| 260 | + await tester.enterText( |
| 261 | + find.byKey(const ValueKey('user-notes-field')), |
| 262 | + 'Great beer', |
| 263 | + ); |
| 264 | + FocusManager.instance.primaryFocus?.unfocus(); |
| 265 | + await tester.pump(); |
| 266 | + |
| 267 | + // Back in display mode with the save unresolved — the new text must |
| 268 | + // not flash back to the placeholder while the write is in flight. |
| 269 | + expect(find.byKey(const ValueKey('user-notes-field')), findsNothing); |
| 270 | + expect(find.text('Great beer'), findsOneWidget); |
| 271 | + expect(find.text('Tap to add your notes'), findsNothing); |
| 272 | + |
| 273 | + completer.complete(); |
| 274 | + await tester.pump(); |
| 275 | + await tester.pump(YourTakeCard.savedIndicatorDuration); |
| 276 | + }, |
| 277 | + ); |
| 278 | + |
173 | 279 | testWidgets('the Saved indicator appears after a save and clears itself', ( |
174 | 280 | tester, |
175 | 281 | ) async { |
@@ -230,6 +336,22 @@ void main() { |
230 | 336 | ); |
231 | 337 | }); |
232 | 338 |
|
| 339 | + testWidgets('the inline note field exposes a text-field semantics node', ( |
| 340 | + tester, |
| 341 | + ) async { |
| 342 | + final handle = tester.ensureSemantics(); |
| 343 | + try { |
| 344 | + await tester.pumpWidget(buildWidget()); |
| 345 | + |
| 346 | + await tester.tap(find.byKey(const ValueKey('user-notes-editor'))); |
| 347 | + await tester.pump(); |
| 348 | + |
| 349 | + expect(find.semantics.byFlag(SemanticsFlag.isTextField), findsOne); |
| 350 | + } finally { |
| 351 | + handle.dispose(); |
| 352 | + } |
| 353 | + }); |
| 354 | + |
233 | 355 | testWidgets('the Saved indicator is announced as a live region', ( |
234 | 356 | tester, |
235 | 357 | ) async { |
|
0 commit comments