|
| 1 | +import 'dart:async' show unawaited; |
| 2 | + |
1 | 3 | import 'package:flowr/flowr_mvvm.dart'; |
2 | 4 | import 'package:flowr/flowr_mvvm_support.dart'; |
3 | 5 | import 'package:flutter/material.dart'; |
@@ -78,6 +80,16 @@ class LifecycleCounterVM extends CounterVM { |
78 | 80 | } |
79 | 81 | } |
80 | 82 |
|
| 83 | +class SelfClosingCounterVM extends LifecycleCounterVM { |
| 84 | + SelfClosingCounterVM(super.events); |
| 85 | + |
| 86 | + @override |
| 87 | + void dispose() { |
| 88 | + unawaited(close()); |
| 89 | + super.dispose(); |
| 90 | + } |
| 91 | +} |
| 92 | + |
81 | 93 | class MyChangNtfApp extends StatelessWidget { |
82 | 94 | const MyChangNtfApp({super.key}); |
83 | 95 |
|
@@ -261,5 +273,52 @@ main() { |
261 | 273 | expect(vm.isClosed, isTrue); |
262 | 274 | expect(() => vm.addListener(() {}), throwsFlutterError); |
263 | 275 | }); |
| 276 | + |
| 277 | + testWidgets('still disposes when the dispose hook throws', (tester) async { |
| 278 | + late TrackedCounter counter; |
| 279 | + |
| 280 | + await tester.pumpWidget( |
| 281 | + FrProvider.listenable<TrackedCounter>( |
| 282 | + (_) => counter = TrackedCounter(), |
| 283 | + dispose: (_, _) => throw StateError('hook failed'), |
| 284 | + child: Consumer<TrackedCounter>( |
| 285 | + builder: |
| 286 | + (_, value, _) => |
| 287 | + Text('${value.count}', textDirection: TextDirection.ltr), |
| 288 | + ), |
| 289 | + ), |
| 290 | + ); |
| 291 | + |
| 292 | + await tester.pumpWidget(const SizedBox()); |
| 293 | + |
| 294 | + expect(tester.takeException(), isA<StateError>()); |
| 295 | + expect(counter.disposeCalls, 1); |
| 296 | + expect(() => counter.addListener(() {}), throwsFlutterError); |
| 297 | + }); |
| 298 | + |
| 299 | + testWidgets('does not close an already self-closing FlowR twice', ( |
| 300 | + tester, |
| 301 | + ) async { |
| 302 | + late SelfClosingCounterVM vm; |
| 303 | + final events = <String>[]; |
| 304 | + |
| 305 | + await tester.pumpWidget( |
| 306 | + FrProvider.listenable<SelfClosingCounterVM>( |
| 307 | + (_) => vm = SelfClosingCounterVM(events), |
| 308 | + child: Consumer<SelfClosingCounterVM>( |
| 309 | + builder: |
| 310 | + (_, value, _) => |
| 311 | + Text('${value.count}', textDirection: TextDirection.ltr), |
| 312 | + ), |
| 313 | + ), |
| 314 | + ); |
| 315 | + |
| 316 | + await tester.pumpWidget(const SizedBox()); |
| 317 | + await tester.pump(); |
| 318 | + |
| 319 | + expect(vm.disposeCalls, 1); |
| 320 | + expect(vm.closeCalls, 1); |
| 321 | + expect(vm.isClosed, isTrue); |
| 322 | + }); |
264 | 323 | }); |
265 | 324 | } |
0 commit comments