Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix existing state tests #270

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/src/wolt_modal_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ class WoltModalSheetState extends State<WoltModalSheet> {
LayoutId(
id: barrierLayoutId,
child: WoltAnimatedModalBarrier(
animationController: widget.route.animationController!,
animationController: widget.route.animationController ??
widget.route.createAnimationController(),
barrierDismissible: widget.route.barrierDismissible,
onModalDismissedWithBarrierTap:
widget.onModalDismissedWithBarrierTap,
Expand Down
1 change: 1 addition & 0 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
103 changes: 103 additions & 0 deletions test/wolt_modal_sheet_state_test.dart
Original file line number Diff line number Diff line change
@@ -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<void>(
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<void>(
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);
});
}
88 changes: 1 addition & 87 deletions test/wolt_modal_sheet_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>(
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<void>(
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',
Expand Down Expand Up @@ -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(
Expand Down