From b35f9114e3d9bc18582d0d7469faed6d1a6439bd Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Mon, 22 Jul 2024 23:42:23 +0300 Subject: [PATCH] Fix existing state tests --- lib/src/wolt_modal_sheet.dart | 3 +- melos.yaml | 1 + test/wolt_modal_sheet_state_test.dart | 103 ++++++++++++++++++++++++++ test/wolt_modal_sheet_test.dart | 88 +--------------------- 4 files changed, 107 insertions(+), 88 deletions(-) create mode 100644 test/wolt_modal_sheet_state_test.dart diff --git a/lib/src/wolt_modal_sheet.dart b/lib/src/wolt_modal_sheet.dart index f31dee0a..f6eeb364 100644 --- a/lib/src/wolt_modal_sheet.dart +++ b/lib/src/wolt_modal_sheet.dart @@ -402,7 +402,8 @@ class WoltModalSheetState extends State { LayoutId( id: barrierLayoutId, child: WoltAnimatedModalBarrier( - animationController: widget.route.animationController!, + animationController: widget.route.animationController ?? + widget.route.createAnimationController(), barrierDismissible: widget.route.barrierDismissible, onModalDismissedWithBarrierTap: widget.onModalDismissedWithBarrierTap, diff --git a/melos.yaml b/melos.yaml index 83fe9bbe..6204129d 100644 --- a/melos.yaml +++ b/melos.yaml @@ -2,6 +2,7 @@ name: wolt_modal_sheet repository: https://github.com/woltapp/wolt_modal_sheet packages: + - . ## This is the root package - coffee_maker - coffee_maker_navigator_2 - demo_ui_components diff --git a/test/wolt_modal_sheet_state_test.dart b/test/wolt_modal_sheet_state_test.dart new file mode 100644 index 00000000..8fb848ae --- /dev/null +++ b/test/wolt_modal_sheet_state_test.dart @@ -0,0 +1,103 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:wolt_modal_sheet/wolt_modal_sheet.dart'; + +void main() { + testWidgets('Should update pages when notifier changes', + (WidgetTester tester) async { + final pageListBuilderNotifier = ValueNotifier((BuildContext _) => [ + WoltModalSheetPage( + child: const Text('Initial Page'), + ), + ]); + + await tester.pumpWidget( + MaterialApp( + home: Builder(builder: (context) { + final NavigatorState navigator = Navigator.of(context); + return WoltModalSheet( + pageListBuilderNotifier: pageListBuilderNotifier, + pageIndexNotifier: ValueNotifier(0), + onModalDismissedWithBarrierTap: () {}, + onModalDismissedWithDrag: () {}, + pageContentDecorator: null, + modalDecorator: null, + modalTypeBuilder: (_) => WoltModalType.bottomSheet(), + transitionAnimationController: null, + route: WoltModalSheetRoute( + pageListBuilderNotifier: pageListBuilderNotifier, + transitionAnimationController: + AnimationController(vsync: navigator), + barrierDismissible: true, + ), + enableDrag: null, + showDragHandle: null, + useSafeArea: false, + ); + }), + ), + ); + + // Initial state check. + expect(find.text('Initial Page'), findsOneWidget); + + // Update the notifier. + pageListBuilderNotifier.value = (_) => [ + WoltModalSheetPage(child: const Text('Updated Page')), + ]; + + // Trigger the listener. + pageListBuilderNotifier.notifyListeners(); + await tester.pumpAndSettle(); + + // Check if the UI is updated. + expect(find.text('Updated Page'), findsOneWidget); + }); + + testWidgets('Listener should be removed on dispose', + (WidgetTester tester) async { + final pageListBuilderNotifier = ValueNotifier((_) => [ + WoltModalSheetPage(child: const Text('Initial Page')), + ]); + + await tester.pumpWidget( + MaterialApp( + home: Builder(builder: (context) { + final NavigatorState navigator = Navigator.of(context); + return WoltModalSheet( + pageListBuilderNotifier: pageListBuilderNotifier, + pageIndexNotifier: ValueNotifier(0), + onModalDismissedWithBarrierTap: () {}, + onModalDismissedWithDrag: () {}, + pageContentDecorator: null, + modalDecorator: null, + modalTypeBuilder: (_) => WoltModalType.bottomSheet(), + transitionAnimationController: null, + route: WoltModalSheetRoute( + pageListBuilderNotifier: pageListBuilderNotifier, + transitionAnimationController: + AnimationController(vsync: navigator), + barrierDismissible: true, + ), + enableDrag: null, + showDragHandle: null, + useSafeArea: false, + ); + }), + ), + ); + + // Update the notifier after the widget is disposed. + await tester.pumpWidget(Container()); // Dispose the widget. + pageListBuilderNotifier.value = (_) => [ + WoltModalSheetPage(child: const Text('Should Not Update')), + ]; + + // Trigger the listener. + pageListBuilderNotifier.notifyListeners(); + await tester.pumpAndSettle(); + + // Since the widget is disposed, this text should not be found. + expect(find.text('Should Not Update'), findsNothing); + }); +} diff --git a/test/wolt_modal_sheet_test.dart b/test/wolt_modal_sheet_test.dart index dad1873c..ac4c5542 100644 --- a/test/wolt_modal_sheet_test.dart +++ b/test/wolt_modal_sheet_test.dart @@ -65,92 +65,6 @@ void main() { }); }); - group('WoltModalSheet State Management', () { - testWidgets('should update pages when notifier changes', - (WidgetTester tester) async { - final pageListBuilderNotifier = ValueNotifier((BuildContext _) => [ - WoltModalSheetPage( - child: const Text('Initial Page'), - ), - ]); - - await tester.pumpWidget( - MaterialApp( - home: WoltModalSheet( - pageListBuilderNotifier: pageListBuilderNotifier, - pageIndexNotifier: ValueNotifier(0), - onModalDismissedWithBarrierTap: () {}, - onModalDismissedWithDrag: () {}, - pageContentDecorator: null, - modalDecorator: null, - modalTypeBuilder: (_) => WoltModalType.bottomSheet(), - transitionAnimationController: null, - route: WoltModalSheetRoute( - pageListBuilderNotifier: pageListBuilderNotifier), - enableDrag: null, - showDragHandle: null, - useSafeArea: false, - ), - ), - ); - - // Initial state check - expect(find.text('Initial Page'), findsOneWidget); - - // Update the notifier - pageListBuilderNotifier.value = (_) => [ - WoltModalSheetPage(child: const Text('Updated Page')), - ]; - - // Trigger the listener - pageListBuilderNotifier.notifyListeners(); - await tester.pumpAndSettle(); - - // Check if the UI is updated - expect(find.text('Updated Page'), findsOneWidget); - }); - - testWidgets('listener should be removed on dispose', - (WidgetTester tester) async { - final pageListBuilderNotifier = ValueNotifier((_) => [ - WoltModalSheetPage(child: const Text('Initial Page')), - ]); - - await tester.pumpWidget( - MaterialApp( - home: WoltModalSheet( - pageListBuilderNotifier: pageListBuilderNotifier, - pageIndexNotifier: ValueNotifier(0), - onModalDismissedWithBarrierTap: () {}, - onModalDismissedWithDrag: () {}, - pageContentDecorator: null, - modalDecorator: null, - modalTypeBuilder: (_) => WoltModalType.bottomSheet(), - transitionAnimationController: null, - route: WoltModalSheetRoute( - pageListBuilderNotifier: pageListBuilderNotifier), - enableDrag: null, - showDragHandle: null, - useSafeArea: false, - ), - ), - ); - - // Update the notifier after the widget is disposed - await tester.pumpWidget(Container()); // Dispose the widget - pageListBuilderNotifier.value = (_) => [ - WoltModalSheetPage(child: const Text('Should Not Update')), - ]; - - // Trigger the listener - pageListBuilderNotifier.notifyListeners(); - await tester.pumpAndSettle(); - - // Since the widget is disposed, this text should not be found - expect(find.text('Should Not Update'), findsNothing); - }); - }); - group('barrierDismissible', () { testWidgets( 'Does not dismiss on barrier tap if barrierDismissible is false', @@ -263,7 +177,7 @@ void main() { testWidgets('WoltModalSheet.modalTypeBuilder defaults - default window size', (tester) async { Size viewSize = const Size(800.0, 600.0); - Size sheetPageSize = const Size(400.0, 71.0); + Size sheetPageSize = const Size(524.0, 71.0); await tester.pumpWidget( MaterialApp(